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, ]); }; 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..6627645e --- /dev/null +++ b/rules/DowngradePhp80/Rector/Instanceof_/DowngradeInstanceofStringableRector.php @@ -0,0 +1,60 @@ +> + */ + 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')]); + } +}