diff --git a/config/set/php83.php b/config/set/php83.php index 357ec95be4e..91a264d0c02 100644 --- a/config/set/php83.php +++ b/config/set/php83.php @@ -6,11 +6,13 @@ use Rector\Php83\Rector\ClassConst\AddTypeToConstRector; use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector; +use Rector\Php83\Rector\FuncCall\RemoveGetClassGetParentClassNoArgsRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ AddOverrideAttributeToOverriddenMethodsRector::class, AddTypeToConstRector::class, CombineHostPortLdapUriRector::class, + RemoveGetClassGetParentClassNoArgsRector::class, ]); }; diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_class.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_class.php.inc new file mode 100644 index 00000000000..4102c849034 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_class.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_parent_class.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_parent_class.php.inc new file mode 100644 index 00000000000..daf6b9e8d83 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/fixture_get_parent_class.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/Fixture/skip_different_func_call.php.inc new file mode 100644 index 00000000000..cdc82280ef8 --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/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/RemoveGetClassGetParentClassNoArgsRector/config/configured_rule.php b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/config/configured_rule.php new file mode 100644 index 00000000000..67f3b08201c --- /dev/null +++ b/rules-tests/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector/config/configured_rule.php @@ -0,0 +1,13 @@ +rule(RemoveGetClassGetParentClassNoArgsRector::class); + + $rectorConfig->phpVersion(PhpVersion::PHP_83); +}; diff --git a/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php b/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php new file mode 100644 index 00000000000..4f4c011cb54 --- /dev/null +++ b/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php @@ -0,0 +1,88 @@ +> + */ + 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 (count($node->getArgs()) !== 0) { + return null; + } + + $target = null; + if ($this->isName($node, 'get_class')) { + $target = 'self'; + } + + if ($this->isName($node, 'get_parent_class')) { + $target = 'parent'; + } + + if ($target !== null) { + return new Node\Expr\ClassConstFetch(new Node\Name([$target]), new Node\VarLikeIdentifier('class')); + } + + return null; + } + + 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..9940a51c135 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -689,4 +689,11 @@ 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 + * @see https://php.watch/versions/8.3/get_class-get_parent_class-parameterless-deprecated + * @var int + */ + public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = PhpVersion::PHP_83; }