-
-
Notifications
You must be signed in to change notification settings - Fork 432
[Php85] Replace null return with empty array in __debugInfo #7124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samsonasik
merged 2 commits into
rectorphp:main
from
mttsch:feature/php85-__debugInfo-null
Aug 11, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ts/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/explicit_null_return.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
|
||
| final class ExplicitNullReturn | ||
| { | ||
| public function __debugInfo(): ?array | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
|
||
| final class ExplicitNullReturn | ||
| { | ||
| public function __debugInfo(): ?array | ||
| { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...ts/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/implicit_null_return.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
|
||
| final class ImplicitNullReturn | ||
| { | ||
| public function __debugInfo(): ?array | ||
| { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
|
||
| final class ImplicitNullReturn | ||
| { | ||
| public function __debugInfo(): ?array | ||
| { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| ?> |
15 changes: 15 additions & 0 deletions
15
...s/Php85/Rector/MethodCall/NullDebugInfoReturnRector/Fixture/skip_different_method.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
|
||
| final class SkipDifferentMethod | ||
| { | ||
| public function differentMethod() | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| ?> |
28 changes: 28 additions & 0 deletions
28
...tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/NullDebugInfoReturnRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class NullDebugInfoReturnRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php85/Rector/MethodCall/NullDebugInfoReturnRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(NullDebugInfoReturnRector::class); | ||
| }; |
105 changes: 105 additions & 0 deletions
105
rules/Php85/Rector/ClassMethod/NullDebugInfoReturnRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Php85\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\Array_; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Function_; | ||
| use PhpParser\Node\Stmt\Return_; | ||
| use PhpParser\NodeVisitor; | ||
| use Rector\PhpParser\Node\Value\ValueResolver; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\ValueObject\PhpVersionFeature; | ||
| use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_debuginfo_returning_null | ||
| * @see \Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\NullDebugInfoReturnRectorTest | ||
| */ | ||
| final class NullDebugInfoReturnRector extends AbstractRector implements MinPhpVersionInterface | ||
| { | ||
| public function __construct( | ||
| private readonly ValueResolver $valueResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Replaces `null` return value with empty array in `__debugInfo` methods', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| new class | ||
| { | ||
| public function __debugInfo() { | ||
| return null; | ||
| } | ||
| }; | ||
| CODE_SAMPLE | ||
|
|
||
| , | ||
| <<<'CODE_SAMPLE' | ||
| new class | ||
| { | ||
| public function __debugInfo() { | ||
| return []; | ||
| } | ||
| }; | ||
| CODE_SAMPLE | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.