-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathSleepToSerializeRector.php
More file actions
142 lines (124 loc) · 3.89 KB
/
SleepToSerializeRector.php
File metadata and controls
142 lines (124 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Rector\Php85\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/51uu0
* @see https://3v4l.org/ktJnk
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods
* @see \Rector\Tests\Php85\Rector\Class_\SleepToSerializeRector\SleepToSerializeRectorTest
*/
final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReturnAnalyzer $returnAnalyzer
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATED_METHOD_SLEEP;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change __sleep() to __serialize() with correct return values',
[
new CodeSample(
<<<'CODE_SAMPLE'
class User {
private $id;
private $name;
public function __sleep() {
return ['id', 'name'];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class User {
private $id;
private $name;
public function __serialize(): array {
return [
'id' => $this->id,
'name' => $this->name,
];
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->getMethod('__serialize') instanceof ClassMethod) {
return null;
}
$classMethod = $node->getMethod('__sleep');
if (! $classMethod instanceof ClassMethod) {
return null;
}
if ($classMethod->returnType instanceof Identifier && $this->isName($classMethod->returnType, 'array')) {
return null;
}
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
return null;
}
$hasChanged = false;
foreach ($returns as $return) {
if (! $return->expr instanceof Array_) {
return null;
}
if ($return->expr->items !== []) {
$newItems = [];
foreach ($return->expr->items as $item) {
if ($item !== null && $item->value instanceof String_) {
$propName = $item->value->value;
$newItems[] = new ArrayItem(
new PropertyFetch(new Variable('this'), $propName),
$item->value
);
}
}
if ($newItems !== []) {
$hasChanged = true;
$return->expr->items = $newItems;
}
}
}
if ($hasChanged) {
$classMethod->name = new Identifier('__serialize');
$classMethod->returnType = new Identifier('array');
return $node;
}
return null;
}
}