Skip to content

Commit

Permalink
feat(DowngradePhp73JsonConstRector): throw behavior
Browse files Browse the repository at this point in the history
* Try to properly downgrade the throw behavior removed from `json_encode` and `json_decode`
  with an alternative code snippet added after the function node.
* This is a partial solution that only handles when the flags are directly set in the function call.
  • Loading branch information
sylver committed Jun 20, 2023
1 parent 3024cff commit 63abfb6
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\Fixture;

// Class
class DowngradePhp73JsonConstRectorFixture
{
public function method_json_decode(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
return $payload;
} catch (\Exception $e) {
return false;
}
}

public function method_json_decode_multi_flags(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR);
return $payload;
} catch (\Exception $e) {
return false;
}
}
}

// Function
function func_json_decode(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
return $payload;
} catch (\Exception $e) {
return false;
}
}

// Standalone
$decode = json_decode($json, null, 512, JSON_THROW_ON_ERROR);

?>
-----
<?php

namespace Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\Fixture;

// Class
class DowngradePhp73JsonConstRectorFixture
{
public function method_json_decode(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
return $payload;
} catch (\Exception $e) {
return false;
}
}

public function method_json_decode_multi_flags(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, JSON_OBJECT_AS_ARRAY);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
return $payload;
} catch (\Exception $e) {
return false;
}
}
}

// Function
function func_json_decode(string $json): string | bool
{
try {
$payload = json_decode($json, null, 512, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
return $payload;
} catch (\Exception $e) {
return false;
}
}

// Standalone
$decode = json_decode($json, null, 512, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\Fixture;

// Class
class DowngradePhp73JsonConstRectorFixture
{
public function method_json_encode(string $json): string | bool
{
try {
json_encode($json, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
return false;
}
}

public function method_json_encode_multi_flags(string $json): string | bool
{
try {
json_encode($json, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
} catch (\Exception $e) {
return false;
}
}
}

// Function
function func_json_encode(string $json): string | bool
{
try {
json_encode($json, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
return false;
}
}

// Standalone
json_encode($json, JSON_THROW_ON_ERROR);

?>
-----
<?php

namespace Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\Fixture;

// Class
class DowngradePhp73JsonConstRectorFixture
{
public function method_json_encode(string $json): string | bool
{
try {
json_encode($json, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
} catch (\Exception $e) {
return false;
}
}

public function method_json_encode_multi_flags(string $json): string | bool
{
try {
json_encode($json, JSON_PRETTY_PRINT);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
} catch (\Exception $e) {
return false;
}
}
}

// Function
function func_json_encode(string $json): string | bool
{
try {
json_encode($json, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
} catch (\Exception $e) {
return false;
}
}

// Standalone
json_encode($json, 0);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class InBitwise
public function run($argument)
{
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception(json_last_error_msg());
}
}
}

Expand Down
Loading

0 comments on commit 63abfb6

Please sign in to comment.