diff --git a/config/set/php85.php b/config/set/php85.php index 527d229a581..a3125b37f88 100644 --- a/config/set/php85.php +++ b/config/set/php85.php @@ -4,6 +4,7 @@ use Rector\Config\RectorConfig; use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector; +use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector; use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector; use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector; use Rector\Removing\ValueObject\RemoveFuncCallArg; @@ -14,7 +15,7 @@ use Rector\Renaming\ValueObject\RenameClassAndConstFetch; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class]); + $rectorConfig->rules([ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class, NullDebugInfoReturnRector::class]); $rectorConfig->ruleWithConfiguration( RemoveFuncCallArgRector::class, diff --git a/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/explicit_null_return.php.inc b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/explicit_null_return.php.inc new file mode 100644 index 00000000000..ff335572230 --- /dev/null +++ b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/explicit_null_return.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/implicit_null_return.php.inc b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/implicit_null_return.php.inc new file mode 100644 index 00000000000..0046487199b --- /dev/null +++ b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/implicit_null_return.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/skip_different_method.php.inc b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/skip_different_method.php.inc new file mode 100644 index 00000000000..9839326a596 --- /dev/null +++ b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/skip_different_method.php.inc @@ -0,0 +1,15 @@ + diff --git a/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/NullDebugInfoReturnRectorTest.php b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/NullDebugInfoReturnRectorTest.php new file mode 100644 index 00000000000..4458581f490 --- /dev/null +++ b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/NullDebugInfoReturnRectorTest.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/Php85/Rector/MethodCall/NullDebugInfoReturnRector/config/configured_rule.php b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/config/configured_rule.php new file mode 100644 index 00000000000..2f632ba1861 --- /dev/null +++ b/rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(NullDebugInfoReturnRector::class); +}; diff --git a/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php b/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php new file mode 100644 index 00000000000..65f5dbf88ba --- /dev/null +++ b/rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php @@ -0,0 +1,105 @@ +> + */ + public function getNodeTypes(): array + { + return [ClassMethod::class]; + } + + /** + * @param ClassMethod $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isName($node, '__debugInfo')) { + return null; + } + + $hasChanged = \false; + $this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (&$hasChanged) { + if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) { + return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + if ($node instanceof Return_ && (! $node->expr instanceof Expr || $this->valueResolver->isNull( + $node->expr + ))) { + $hasChanged = \true; + $node->expr = new Array_(); + return $node; + } + + return null; + }); + + if ($hasChanged) { + return $node; + } + + return null; + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::DEPRECATED_NULL_DEBUG_INFO_RETURN; + } +} diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index ff83bac1998..dcdbe245a2f 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -768,10 +768,16 @@ final class PhpVersionFeature * @var int */ public const ARRAY_ANY = PhpVersion::PHP_84; - + /** * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_context_parameter_for_finfo_buffer * @var int */ - public const DEPRECATE_FINFO_BUFFER_CONTEXT = PhpVersion::PHP_85; + public const DEPRECATE_FINFO_BUFFER_CONTEXT = PhpVersion::PHP_85; + + /** + * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_debuginfo_returning_null + * @var int + */ + public const DEPRECATED_NULL_DEBUG_INFO_RETURN = PhpVersion::PHP_85; }