-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DowngradePhp73JsonConstRector): throw behavior
* 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
Showing
4 changed files
with
338 additions
and
4 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...r/ConstFetch/DowngradePhp73JsonConstRector/Fixture/add_throw_on_json_decode_error.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
?> |
93 changes: 93 additions & 0 deletions
93
...r/ConstFetch/DowngradePhp73JsonConstRector/Fixture/add_throw_on_json_encode_error.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.