-
-
Notifications
You must be signed in to change notification settings - Fork 680
/
StringableForToStringRector.php
169 lines (167 loc) · 5.66 KB
/
StringableForToStringRector.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare (strict_types=1);
namespace Rector\Php80\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\String_ as CastString_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php80\Rector\Class_\StringableForToStringRector\StringableForToStringRectorTest
*/
final class StringableForToStringRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer
*/
private $familyRelationsAnalyzer;
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
*/
private $returnTypeInferer;
/**
* @readonly
* @var \Rector\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
/**
* @readonly
* @var \Rector\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @var string
*/
private const STRINGABLE = 'Stringable';
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(FamilyRelationsAnalyzer $familyRelationsAnalyzer, ReturnTypeInferer $returnTypeInferer, ClassAnalyzer $classAnalyzer, BetterNodeFinder $betterNodeFinder)
{
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
$this->returnTypeInferer = $returnTypeInferer;
$this->classAnalyzer = $classAnalyzer;
$this->betterNodeFinder = $betterNodeFinder;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::STRINGABLE;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add `Stringable` interface to classes with `__toString()` method', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function __toString()
{
return 'I can stringz';
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass implements Stringable
{
public function __toString(): string
{
return 'I can stringz';
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->classAnalyzer->isAnonymousClass($node)) {
return null;
}
$toStringClassMethod = $node->getMethod(MethodName::TO_STRING);
if (!$toStringClassMethod instanceof ClassMethod) {
return null;
}
$this->hasChanged = \false;
// warning, classes that implements __toString() will return Stringable interface even if they don't implemen it
// reflection cannot be used for real detection
$classLikeAncestorNames = $this->familyRelationsAnalyzer->getClassLikeAncestorNames($node);
$isAncestorHasStringable = \in_array(self::STRINGABLE, $classLikeAncestorNames, \true);
$returnType = $this->returnTypeInferer->inferFunctionLike($toStringClassMethod);
if (!$returnType->isString()->yes()) {
$this->processNotStringType($toStringClassMethod);
}
if (!$isAncestorHasStringable) {
// add interface
$node->implements[] = new FullyQualified(self::STRINGABLE);
$this->hasChanged = \true;
}
// add return type
if ($toStringClassMethod->returnType === null) {
$toStringClassMethod->returnType = new Identifier('string');
$this->hasChanged = \true;
}
if (!$this->hasChanged) {
return null;
}
return $node;
}
private function processNotStringType(ClassMethod $toStringClassMethod) : void
{
if ($toStringClassMethod->isAbstract()) {
return;
}
$hasReturn = $this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($toStringClassMethod, Return_::class);
if (!$hasReturn) {
$emptyStringReturn = new Return_(new String_(''));
$toStringClassMethod->stmts[] = $emptyStringReturn;
$this->hasChanged = \true;
return;
}
$this->traverseNodesWithCallable((array) $toStringClassMethod->stmts, function (Node $subNode) : ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (!$subNode instanceof Return_) {
return null;
}
if (!$subNode->expr instanceof Expr) {
$subNode->expr = new String_('');
return null;
}
$type = $this->nodeTypeResolver->getType($subNode->expr);
if ($type->isString()->yes()) {
return null;
}
$subNode->expr = new CastString_($subNode->expr);
$this->hasChanged = \true;
return null;
});
}
}