|
| 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 | +} |
0 commit comments