From eadea252dae87d8703ce171ec8fb2682f455c5b9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 19 Mar 2024 16:23:20 +0700 Subject: [PATCH] [DowngradePhp80] Fix downgrade PhpToken::tokenize() and call with ->id (#216) --- .../Fixture/name_id_to_integer_index.php.inc | 31 +++++++++++++++++++ .../StaticCall/DowngradePhpTokenRector.php | 8 +++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 rules-tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/Fixture/name_id_to_integer_index.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/Fixture/name_id_to_integer_index.php.inc b/rules-tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/Fixture/name_id_to_integer_index.php.inc new file mode 100644 index 00000000..5df6fcba --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector/Fixture/name_id_to_integer_index.php.inc @@ -0,0 +1,31 @@ +id) { + + } +} + +?> +----- + diff --git a/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php b/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php index 020fc3f1..b46d5974 100644 --- a/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php +++ b/rules/DowngradePhp80/Rector/StaticCall/DowngradePhpTokenRector.php @@ -111,7 +111,8 @@ private function refactorMethodCall(MethodCall $methodCall): ?Ternary private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?Ternary { - if (! $this->isName($propertyFetch->name, 'text')) { + $propertyFetchName = $this->getName($propertyFetch->name); + if (! in_array($propertyFetchName, ['id', 'text'], true)) { return null; } @@ -120,7 +121,10 @@ private function refactorPropertyFetch(PropertyFetch $propertyFetch): ?Ternary } $isArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($propertyFetch->var)]); - $arrayDimFetch = new ArrayDimFetch($propertyFetch->var, new LNumber(1)); + $arrayDimFetch = new ArrayDimFetch( + $propertyFetch->var, + $propertyFetchName === 'id' ? new LNumber(0) : new LNumber(1) + ); return new Ternary($isArrayFuncCall, $arrayDimFetch, $propertyFetch->var); }