-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(DowngradePhp73JsonConstRector): throw behavior #133
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3024cff
chore(DowngradePhp73JsonConstRector): add code sample
sylver 63abfb6
feat(DowngradePhp73JsonConstRector): throw behavior
sylver 82b1833
fixup! feat(DowngradePhp73JsonConstRector): throw behavior
sylver 603d0e6
fix(ArrayMergeFromArraySpreadFactory): unused `use`
sylver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is incorrect, as it previously on
\Exception
toreturn false
, so must be skipped instead to avoid regression, so basically if inside try catch, it must not add the if, here the example:https://3v4l.org/k3U5V
with new behaviour, it throw exception, which should not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samsonasik @TomasVotruba Not sure to follow you here, without the
if
statement nor theJSON_THROW_ON_ERROR
,json_encode()
/json_decode()
does not raise any error, so thecatch
is not executed, that's exactly why this PR was made, you're actually removing the precise purpose of it.The problem resides on the type of the exception thrown to match the one in the catch, you should not remove the
if
entirely or you defeat the purpose of this PR and we are back to square one.In PHP < 7.3 that does not throw (initial behavior of
DowngradePhp73JsonConstRector
) :https://3v4l.org/1Y2Bv#v7.2.34
While this replicate the same behavior as with
JSON_THROW_ON_ERROR
(goal of my PR) :https://3v4l.org/lb86Z#v7.2.34
Also the exception
JsonException
does not exist in PHP < 7.3 so your example is not valid in this context, if the catch specify aJsonException
, it should be replaced by a standardException
to be compatible with PHP < 7.3 anyway.I could have worked that out if you didn't act so fast, with your last merge this PR is basically useless.
It was just needed a little bit of improvement on the exception type (and actually also handle the downgrade of
JsonException
that I overlooked, thanks for pointing that out)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://3v4l.org/k3U5V vs https://3v4l.org/gJkgE
Better approach to use catch stmts instead probably the real solution, but the quick fix is not add if to avoid regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, I don't get it, it's like you didn't read what I just said.
The 2 examples you just provided are not valid PHP 7.2.x (since
JsonException
does not exist for that version)https://www.php.net/manual/en/class.jsonexception.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception can be used, but need to map for what defined inside catch, or it will fatal error that mean regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought a "Downgrade" rule could only be applied.. well, for a downgrade. That wouldn't make much sense to apply it the other way around during an upgrade, but again I don't know all the subtlety of Rector.
Would be curious to understand it more, when you'll have the time, I have a few other PRs coming up 😅
I'll go with a custom local rule for now, since with your last changes it does not fix my downgrade problem anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I didn't get it at first tbh, but thing is you're trying to avoid the throw/catch entirely, while this is the initial/expected behavior of the code that should be retained, no matter what, else you get a discrepancy between the versions of your code.
I'll try to come up with something else if I have the time, unless you do it first. Thanks for your help and the discussion/debate 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, improvement is welcome, just need more carefull approach to avoid regression ;)
I have on list of downgrade improvement, one of them is patch invalid the docblock during downgrade, see rectorphp/rector#8004 but that's not our priority right now as docblock doesn't cause fatal error ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, how could I have caught locally the regression issue you got before pushing the PR ?
Since all was green both locally and eventually in the CI, I don't know where to look for to reproduce it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this rule itself, for example, we have the following code:
Currently, in php 7.3, it output
something is wrong
, see https://3v4l.org/GkRKc#v7.3.33When downgraded to php 7.2, it should keep show
something is wrong
, see https://3v4l.org/XSJ4W#v7.2.34This should limit per use-case to avoid general change,eg: Try, then catch JsonException, no finally, no anything else, if that works, we can improve to another improvement :)