|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -namespace Rector\Php85\Rector\ClassMethod; |
| 5 | +namespace Rector\Php85\Rector\Class_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Expr\Array_; |
9 | 9 | use PhpParser\Node\Expr\ArrayItem; |
10 | 10 | use PhpParser\Node\Expr\PropertyFetch; |
11 | 11 | use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
12 | 13 | use PhpParser\Node\Stmt\ClassMethod; |
13 | 14 | use Rector\PhpParser\Node\BetterNodeFinder; |
14 | 15 | use Rector\Rector\AbstractRector; |
|
22 | 23 | * @see https://3v4l.org/51uu0 |
23 | 24 | * @see https://3v4l.org/ktJnk |
24 | 25 | * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods |
25 | | - * @see \Rector\Tests\Php85\Rector\MethodCall\SleepToSerializeRector\SleepToSerializeRectorTest |
| 26 | + * @see \Rector\Tests\Php85\Rector\Class_\SleepToSerializeRector\SleepToSerializeRectorTest |
26 | 27 | */ |
27 | 28 | final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface |
28 | 29 | { |
@@ -77,24 +78,29 @@ public function __serialize(): array { |
77 | 78 | */ |
78 | 79 | public function getNodeTypes(): array |
79 | 80 | { |
80 | | - return [ClassMethod::class]; |
| 81 | + return [Class_::class]; |
81 | 82 | } |
82 | 83 |
|
83 | 84 | /** |
84 | | - * @param ClassMethod $node |
| 85 | + * @param Class_ $node |
85 | 86 | */ |
86 | 87 | public function refactor(Node $node): ?Node |
87 | 88 | { |
88 | | - if (! $this->isName($node->name, '__sleep')) { |
| 89 | + if ($node->getMethod('__serialize') instanceof ClassMethod) { |
89 | 90 | return null; |
90 | 91 | } |
91 | 92 |
|
92 | | - if ($node->returnType instanceof Identifier && $this->isName($node->returnType, 'array')) { |
| 93 | + $classMethod = $node->getMethod('__sleep'); |
| 94 | + if (! $classMethod instanceof ClassMethod) { |
93 | 95 | return null; |
94 | 96 | } |
95 | 97 |
|
96 | | - $returns = $this->betterNodeFinder->findReturnsScoped($node); |
97 | | - if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) { |
| 98 | + if ($classMethod->returnType instanceof Identifier && $this->isName($classMethod->returnType, 'array')) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + $returns = $this->betterNodeFinder->findReturnsScoped($classMethod); |
| 103 | + if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) { |
98 | 104 | return null; |
99 | 105 | } |
100 | 106 |
|
@@ -123,8 +129,8 @@ public function refactor(Node $node): ?Node |
123 | 129 | } |
124 | 130 |
|
125 | 131 | if($hasChanged){ |
126 | | - $node->name = new Identifier('__serialize'); |
127 | | - $node->returnType = new Identifier('array'); |
| 132 | + $classMethod->name = new Identifier('__serialize'); |
| 133 | + $classMethod->returnType = new Identifier('array'); |
128 | 134 | return $node; |
129 | 135 | } |
130 | 136 |
|
|
0 commit comments