From 6ad00a78fc51030e9d6c3fa0e20c3e1347bb2094 Mon Sep 17 00:00:00 2001 From: Chris Abbey Date: Wed, 30 Oct 2024 10:28:26 -0700 Subject: [PATCH] add new rectors for php8.3's deprecation of get_class()/get_parent_class() without arguments --- config/set/php83.php | 4 + .../Fixture/fixture.php.inc | 27 +++++++ .../Fixture/skip_different_func_call.php.inc | 11 +++ .../Fixture/skip_with_args.php.inc | 14 ++++ .../RemoveGetClassNoArgsRectorTest.php | 28 +++++++ .../config/configured_rule.php | 13 +++ .../Fixture/fixture.php.inc | 27 +++++++ .../Fixture/skip_different_func_call.php.inc | 11 +++ .../Fixture/skip_with_args.php.inc | 14 ++++ .../RemoveGetParentClassNoArgsRectorTest.php | 28 +++++++ .../config/configured_rule.php | 13 +++ .../FuncCall/RemoveGetClassNoArgsRector.php | 81 +++++++++++++++++++ .../RemoveGetParentClassNoArgsRector.php | 81 +++++++++++++++++++ src/ValueObject/PhpVersionFeature.php | 6 ++ 14 files changed, 358 insertions(+) create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/fixture.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_different_func_call.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_with_args.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/RemoveGetClassNoArgsRectorTest.php create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/config/configured_rule.php create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/fixture.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_with_args.php.inc create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/RemoveGetParentClassNoArgsRectorTest.php create mode 100644 rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/config/configured_rule.php create mode 100644 rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php create mode 100644 rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php diff --git a/config/set/php83.php b/config/set/php83.php index 357ec95be4e..ff9e27296bf 100644 --- a/config/set/php83.php +++ b/config/set/php83.php @@ -6,11 +6,15 @@ use Rector\Php83\Rector\ClassConst\AddTypeToConstRector; use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector; +use Rector\Php83\Rector\FuncCall\RemoveGetClassNoArgsRector; +use Rector\Php83\Rector\FuncCall\RemoveGetParentClassNoArgsRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ AddOverrideAttributeToOverriddenMethodsRector::class, AddTypeToConstRector::class, CombineHostPortLdapUriRector::class, + RemoveGetClassNoArgsRector::class, + RemoveGetParentClassNoArgsRector::class, ]); }; diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/fixture.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..2fe8c4d8c0b --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_different_func_call.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_different_func_call.php.inc new file mode 100644 index 00000000000..3c1f8228220 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/Fixture/skip_different_func_call.php.inc @@ -0,0 +1,11 @@ +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/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/config/configured_rule.php b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/config/configured_rule.php new file mode 100644 index 00000000000..0722362d32f --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector/config/configured_rule.php @@ -0,0 +1,13 @@ +rule(RemoveGetClassNoArgsRector::class); + + $rectorConfig->phpVersion(PhpVersion::PHP_83); +}; diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/fixture.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..47b761893b4 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc new file mode 100644 index 00000000000..55b6be57c0b --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc @@ -0,0 +1,11 @@ +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/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/config/configured_rule.php b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/config/configured_rule.php new file mode 100644 index 00000000000..c8c0ef74127 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector/config/configured_rule.php @@ -0,0 +1,13 @@ +rule(RemoveGetParentClassNoArgsRector::class); + + $rectorConfig->phpVersion(PhpVersion::PHP_83); +}; diff --git a/rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php b/rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php new file mode 100644 index 00000000000..7461de7e451 --- /dev/null +++ b/rules/Php83/Rector/FuncCall/RemoveGetClassNoArgsRector.php @@ -0,0 +1,81 @@ +> + */ + public function getNodeTypes(): array + { + return [Node\Expr\FuncCall::class]; + } + + /** + * @param Node\Expr\FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if ($node->isFirstClassCallable()) { + return null; + } + + if (! ($this->isName($node, 'get_class'))) { + return null; + } + + if (count($node->getArgs()) !== 0) { + return null; + } + + return new Node\Scalar\MagicConst\Class_(); + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS; + } +} diff --git a/rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php b/rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php new file mode 100644 index 00000000000..686b77e4a35 --- /dev/null +++ b/rules/Php83/Rector/FuncCall/RemoveGetParentClassNoArgsRector.php @@ -0,0 +1,81 @@ +> + */ + public function getNodeTypes(): array + { + return [Node\Expr\FuncCall::class]; + } + + /** + * @param Node\Expr\FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if ($node->isFirstClassCallable()) { + return null; + } + + if (! ($this->isName($node, 'get_parent_class'))) { + return null; + } + + if (count($node->getArgs()) !== 0) { + return null; + } + + return new Node\Expr\ClassConstFetch(new Node\Name(['parent']), new Node\VarLikeIdentifier('class')); + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::DEPRECATE_GET_CLASS_WITHOUT_ARGS; + } +} diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 9013dbe0bc6..d88378cd29f 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -689,4 +689,10 @@ final class PhpVersionFeature * @var int */ public const DEPRECATE_HOST_PORT_SEPARATE_ARGS = PhpVersion::PHP_83; + + /** + * @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.core.get-class + * @var int + */ + public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = PhpVersion::PHP_83; }