From e2cbed5e3f5fe38486972dbe9fa27f994b3c19e0 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Wed, 6 Aug 2025 18:45:14 +0200 Subject: [PATCH] [Php85] Replace null return with empty array in __debugInfo --- config/set/php85.php | 3 +- .../Fixture/explicit_null_return.php.inc | 31 ++++++ .../Fixture/implicit_null_return.php.inc | 31 ++++++ .../Fixture/skip_different_method.php.inc | 15 +++ .../NullDebugInfoReturnRectorTest.php | 28 +++++ .../config/configured_rule.php | 10 ++ .../ClassMethod/NullDebugInfoReturnRector.php | 105 ++++++++++++++++++ src/ValueObject/PhpVersionFeature.php | 6 + 8 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/explicit_null_return.php.inc create mode 100644 rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/implicit_null_return.php.inc create mode 100644 rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/skip_different_method.php.inc create mode 100644 rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/NullDebugInfoReturnRectorTest.php create mode 100644 rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/config/configured_rule.php create mode 100644 rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php diff --git a/config/set/php85.php b/config/set/php85.php index f228b79c8e4..00f11496108 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\Removing\Rector\FuncCall\RemoveFuncCallArgRector; use Rector\Removing\ValueObject\RemoveFuncCallArg; use Rector\Renaming\Rector\FuncCall\RenameFunctionRector; @@ -11,7 +12,7 @@ use Rector\Renaming\ValueObject\MethodCallRename; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ArrayFirstLastRector::class]); + $rectorConfig->rules([ArrayFirstLastRector::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 998219643c1..68ce3c8225b 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -768,4 +768,10 @@ final class PhpVersionFeature * @var int */ public const ARRAY_ANY = PhpVersion::PHP_84; + + /** + * @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; }