From 0940d8bd263e420b6d36668679eb5c6a454b478e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 10 May 2025 19:49:20 +0700 Subject: [PATCH 1/4] [DowngradePhp80] Add DowngradeInstanceofStringableRector --- ...owngradeInstanceofStringableRectorTest.php | 28 ++++++++ .../Fixture/fixture.php.inc | 27 +++++++ .../Fixture/skip_different_object.php.inc | 11 +++ .../config/configured_rule.php | 10 +++ .../DowngradeInstanceofStringableRector.php | 72 +++++++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/DowngradeInstanceofStringableRectorTest.php create mode 100644 rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/fixture.php.inc create mode 100644 rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/skip_different_object.php.inc create mode 100644 rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/config/configured_rule.php create mode 100644 rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php diff --git a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/DowngradeInstanceofStringableRectorTest.php b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/DowngradeInstanceofStringableRectorTest.php new file mode 100644 index 00000000..c4dceb88 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/DowngradeInstanceofStringableRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..4423ba92 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/skip_different_object.php.inc b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/skip_different_object.php.inc new file mode 100644 index 00000000..20f481ad --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector/Fixture/skip_different_object.php.inc @@ -0,0 +1,11 @@ +rule(DowngradeInstanceofStringableRector::class); +}; diff --git a/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php new file mode 100644 index 00000000..569ccd30 --- /dev/null +++ b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php @@ -0,0 +1,72 @@ +> + */ + public function getNodeTypes(): array + { + return [Instanceof_::class]; + } + + /** + * @param Instanceof_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $node->class instanceof FullyQualified) { + return null; + } + + if (! $this->isName($node->class, 'Stringable')) { + return null; + } + + return $this->nodeFactory->createFuncCall( + 'method_exists', + [ + $node->expr, + new String_('__toString'), + ] + ); + } +} From 328d024b28528e91e790798dd9dacf0b44f549f4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 10 May 2025 19:50:40 +0700 Subject: [PATCH 2/4] fix --- .../Instanceof_/DowngradeInstanceofStringableRector.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php index 569ccd30..f9191c3f 100644 --- a/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php +++ b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php @@ -8,7 +8,6 @@ use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; -use Rector\PhpParser\Node\NodeFactory; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -19,11 +18,6 @@ */ final class DowngradeInstanceofStringableRector extends AbstractRector { - public function __construct( - private readonly NodeFactory $nodeFactory - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('change instanceof Stringable to method_exists', [ From 7c7f7fd3a3a0b5fc00c5ce2a160d5ef5caf6309c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 10 May 2025 19:52:01 +0700 Subject: [PATCH 3/4] fix --- config/set/downgrade-php80.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/set/downgrade-php80.php b/config/set/downgrade-php80.php index 945745e9..ce89592e 100644 --- a/config/set/downgrade-php80.php +++ b/config/set/downgrade-php80.php @@ -25,6 +25,7 @@ use Rector\DowngradePhp80\Rector\FuncCall\DowngradeStrStartsWithRector; use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeMixedTypeDeclarationRector; use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector; +use Rector\DowngradePhp80\Rector\Instanceof_\DowngradeInstanceofStringableRector; use Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector; use Rector\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector; use Rector\DowngradePhp80\Rector\MethodCall\DowngradeReflectionClassGetConstantsFilterRector; @@ -89,5 +90,6 @@ DowngradeMixedTypeTypedPropertyRector::class, RemoveReturnTypeDeclarationFromCloneRector::class, DowngradeEnumToConstantListClassRector::class, + DowngradeInstanceofStringableRector::class, ]); }; From 51468e459d5ca2356bb7e50487ca29156fe84b23 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 10 May 2025 19:52:37 +0700 Subject: [PATCH 4/4] rectify --- .../Instanceof_/DowngradeInstanceofStringableRector.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php index f9191c3f..6627645e 100644 --- a/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php +++ b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php @@ -55,12 +55,6 @@ public function refactor(Node $node): ?Node return null; } - return $this->nodeFactory->createFuncCall( - 'method_exists', - [ - $node->expr, - new String_('__toString'), - ] - ); + return $this->nodeFactory->createFuncCall('method_exists', [$node->expr, new String_('__toString')]); } }