Skip to content

Commit e2cbed5

Browse files
committed
[Php85] Replace null return with empty array in __debugInfo
1 parent 6f3d502 commit e2cbed5

File tree

8 files changed

+228
-1
lines changed

8 files changed

+228
-1
lines changed

config/set/php85.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
7+
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
78
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
89
use Rector\Removing\ValueObject\RemoveFuncCallArg;
910
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
1011
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
1112
use Rector\Renaming\ValueObject\MethodCallRename;
1213

1314
return static function (RectorConfig $rectorConfig): void {
14-
$rectorConfig->rules([ArrayFirstLastRector::class]);
15+
$rectorConfig->rules([ArrayFirstLastRector::class, NullDebugInfoReturnRector::class]);
1516

1617
$rectorConfig->ruleWithConfiguration(
1718
RemoveFuncCallArgRector::class,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
6+
7+
final class ExplicitNullReturn
8+
{
9+
public function __debugInfo(): ?array
10+
{
11+
return null;
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
22+
23+
final class ExplicitNullReturn
24+
{
25+
public function __debugInfo(): ?array
26+
{
27+
return [];
28+
}
29+
}
30+
31+
?>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
6+
7+
final class ImplicitNullReturn
8+
{
9+
public function __debugInfo(): ?array
10+
{
11+
return;
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
22+
23+
final class ImplicitNullReturn
24+
{
25+
public function __debugInfo(): ?array
26+
{
27+
return [];
28+
}
29+
}
30+
31+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
6+
7+
final class SkipDifferentMethod
8+
{
9+
public function differentMethod()
10+
{
11+
return null;
12+
}
13+
}
14+
15+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NullDebugInfoReturnRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(NullDebugInfoReturnRector::class);
10+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php85\Rector\ClassMethod;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Array_;
10+
use PhpParser\Node\Expr\Closure;
11+
use PhpParser\Node\Stmt\Class_;
12+
use PhpParser\Node\Stmt\ClassMethod;
13+
use PhpParser\Node\Stmt\Function_;
14+
use PhpParser\Node\Stmt\Return_;
15+
use PhpParser\NodeVisitor;
16+
use Rector\PhpParser\Node\Value\ValueResolver;
17+
use Rector\Rector\AbstractRector;
18+
use Rector\ValueObject\PhpVersionFeature;
19+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
20+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
21+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
22+
23+
/**
24+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_debuginfo_returning_null
25+
* @see \Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\NullDebugInfoReturnRectorTest
26+
*/
27+
final class NullDebugInfoReturnRector extends AbstractRector implements MinPhpVersionInterface
28+
{
29+
public function __construct(
30+
private readonly ValueResolver $valueResolver,
31+
) {
32+
}
33+
34+
public function getRuleDefinition(): RuleDefinition
35+
{
36+
return new RuleDefinition('Replaces `null` return value with empty array in `__debugInfo` methods', [
37+
new CodeSample(
38+
<<<'CODE_SAMPLE'
39+
new class
40+
{
41+
public function __debugInfo() {
42+
return null;
43+
}
44+
};
45+
CODE_SAMPLE
46+
47+
,
48+
<<<'CODE_SAMPLE'
49+
new class
50+
{
51+
public function __debugInfo() {
52+
return [];
53+
}
54+
};
55+
CODE_SAMPLE
56+
),
57+
]);
58+
}
59+
60+
/**
61+
* @return array<class-string<Node>>
62+
*/
63+
public function getNodeTypes(): array
64+
{
65+
return [ClassMethod::class];
66+
}
67+
68+
/**
69+
* @param ClassMethod $node
70+
*/
71+
public function refactor(Node $node): ?Node
72+
{
73+
if (! $this->isName($node, '__debugInfo')) {
74+
return null;
75+
}
76+
77+
$hasChanged = \false;
78+
$this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (&$hasChanged) {
79+
if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) {
80+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
81+
}
82+
83+
if ($node instanceof Return_ && (! $node->expr instanceof Expr || $this->valueResolver->isNull(
84+
$node->expr
85+
))) {
86+
$hasChanged = \true;
87+
$node->expr = new Array_();
88+
return $node;
89+
}
90+
91+
return null;
92+
});
93+
94+
if ($hasChanged) {
95+
return $node;
96+
}
97+
98+
return null;
99+
}
100+
101+
public function provideMinPhpVersion(): int
102+
{
103+
return PhpVersionFeature::DEPRECATED_NULL_DEBUG_INFO_RETURN;
104+
}
105+
}

src/ValueObject/PhpVersionFeature.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,4 +768,10 @@ final class PhpVersionFeature
768768
* @var int
769769
*/
770770
public const ARRAY_ANY = PhpVersion::PHP_84;
771+
772+
/**
773+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_debuginfo_returning_null
774+
* @var int
775+
*/
776+
public const DEPRECATED_NULL_DEBUG_INFO_RETURN = PhpVersion::PHP_85;
771777
}

0 commit comments

Comments
 (0)