Skip to content
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

[DowngradePhp72] Add DowngradeJsonDecodeNullAssociativeArgRector #1723

Merged
merged 8 commits into from
Jan 25, 2022

Conversation

samsonasik
Copy link
Member

Currect rector/rector dev-main on php 7.1 got broken build as nette/utils: 3.2.7 has code

$value = json_decode($json, null, 512, $flags | JSON_BIGINT_AS_STRING);

which make invalid on php 7.1, ref https://3v4l.org/qisBB#v7.1.33

This PR add DowngradeJsonDecodeNullAssociativeArgRector to make it false or cast to (bool) when not bool type.

Fixes rectorphp/rector#6967

@samsonasik
Copy link
Member Author

It seems make unnecessary cast when already a false value when used on downgrade combined with DowngradeAttributeToAnnotationRector:

➜  rector-src git:(downgrade-json-decode-null) bin/rector process vendor/nette/utils/src/Utils/Json.php --config build/config/config-downgrade.php --dry-run --clear-cache
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================

1) vendor/nette/utils/src/Utils/Json.php:52

    ---------- begin diff ----------
@@ @@
         */
        public static function decode(string $json, int $flags = 0)
        {
-               $value = json_decode($json, null, 512, $flags | JSON_BIGINT_AS_STRING);
+               $value = json_decode($json, (bool) false, 512, $flags | JSON_BIGINT_AS_STRING);
                if ($error = json_last_error()) {
                        throw new JsonException(json_last_error_msg(), $error);
                }
    ----------- end diff -----------

Applied rules:
 * DowngradeJsonDecodeNullAssociativeArgRector
 * DowngradeAttributeToAnnotationRector (https://php.watch/articles/php-attributes#syntax)


                                                                                                                        
 [OK] 1 file would have changed (dry-run) by Rector                                                                     
                                                                                                                        

@samsonasik
Copy link
Member Author

Resolved with check original node 7eec01f

➜  rector-src git:(downgrade-json-decode-null) bin/rector process vendor/nette/utils/src/Utils/Json.php --config build/config/config-downgrade.php --dry-run
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================

1) vendor/nette/utils/src/Utils/Json.php:52

    ---------- begin diff ----------
@@ @@
         */
        public static function decode(string $json, int $flags = 0)
        {
-               $value = json_decode($json, null, 512, $flags | JSON_BIGINT_AS_STRING);
+               $value = json_decode($json, false, 512, $flags | JSON_BIGINT_AS_STRING);
                if ($error = json_last_error()) {
                        throw new JsonException(json_last_error_msg(), $error);
                }
    ----------- end diff -----------

Applied rules:
 * DowngradeJsonDecodeNullAssociativeArgRector


                                                                                                                        
 [OK] 1 file would have changed (dry-run) by Rector                                                                     
                                                                                                                        

@samsonasik
Copy link
Member Author

The correct way to use created by rule attribute e9f89ce, solved 🎉

Let's give it a try

@samsonasik samsonasik enabled auto-merge (squash) January 25, 2022 09:55
@samsonasik samsonasik merged commit 7cf2421 into main Jan 25, 2022
@samsonasik samsonasik deleted the downgrade-json-decode-null branch January 25, 2022 09:58
@TomasVotruba
Copy link
Member

Thank you 👍

@samsonasik
Copy link
Member Author

It seems got different error now:

PHP Fatal error:  Uncaught TypeError: Return value of RectorPrefix20220125\Symplify\ComposerJsonManipulator\FileSystem\JsonFileManager::loadFromFilePath() must be of the type array, object returned in /home/runner/work/rector/rector/vendor/symplify/composer-json-manipulator/src/FileSystem/JsonFileManager.php:57

it possibly on casting (bool), when not a bool, should be negated of bool instead of direct bool. I will create new PR for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants