forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrEndsWithRector.php
149 lines (127 loc) · 4.23 KB
/
StrEndsWithRector.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
<?php
declare(strict_types=1);
namespace Rector\Php80\Rector\Identical;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\UnaryMinus;
use Rector\Core\Rector\AbstractRector;
use Rector\Nette\NodeAnalyzer\BinaryOpAnalyzer;
use Rector\Nette\ValueObject\FuncCallAndExpr;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
*
* @see \Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\StrEndsWithRectorTest
*/
final class StrEndsWithRector extends AbstractRector
{
/**
* @var BinaryOpAnalyzer
*/
private $binaryOpAnalyzer;
public function __construct(BinaryOpAnalyzer $binaryOpAnalyzer)
{
$this->binaryOpAnalyzer = $binaryOpAnalyzer;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change helper functions to str_ends_with()', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$isMatch = substr($haystack, -strlen($needle)) === $needle;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$isMatch = str_ends_with($haystack, $needle);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Identical::class, NotIdentical::class];
}
/**
* @param Identical|NotIdentical $node
*/
public function refactor(Node $node): ?Node
{
return $this->refactorSubstr($node) ?? $this->refactorSubstrCompare($node);
}
/**
* Covers:
* $isMatch = substr($haystack, -strlen($needle)) === $needle;
*/
private function refactorSubstr(BinaryOp $binaryOp): ?FuncCall
{
if ($binaryOp->left instanceof FuncCall && $this->isName($binaryOp->left, 'substr')) {
$substrFuncCall = $binaryOp->left;
$comparedNeedleExpr = $binaryOp->right;
} elseif ($binaryOp->right instanceof FuncCall && $this->isName($binaryOp->right, 'substr')) {
$substrFuncCall = $binaryOp->right;
$comparedNeedleExpr = $binaryOp->left;
} else {
return null;
}
$haystack = $substrFuncCall->args[0]->value;
$needle = $this->matchUnaryMinusStrlenFuncCallArgValue($substrFuncCall->args[1]->value);
if (! $this->nodeComparator->areNodesEqual($needle, $comparedNeedleExpr)) {
return null;
}
return $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);
}
private function refactorSubstrCompare(BinaryOp $binaryOp): ?FuncCall
{
$funcCallAndExpr = $this->binaryOpAnalyzer->matchFuncCallAndOtherExpr($binaryOp, 'substr_compare');
if (! $funcCallAndExpr instanceof FuncCallAndExpr) {
return null;
}
$expr = $funcCallAndExpr->getExpr();
if (! $this->valueResolver->isValue($expr, 0)) {
return null;
}
$substrCompareFuncCall = $funcCallAndExpr->getFuncCall();
$haystack = $substrCompareFuncCall->args[0]->value;
$needle = $substrCompareFuncCall->args[1]->value;
$comparedNeedleExpr = $this->matchUnaryMinusStrlenFuncCallArgValue($substrCompareFuncCall->args[2]->value);
if (! $this->nodeComparator->areNodesEqual($needle, $comparedNeedleExpr)) {
return null;
}
return $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);
}
private function matchUnaryMinusStrlenFuncCallArgValue(Node $node): ?Expr
{
if (! $node instanceof UnaryMinus) {
return null;
}
if (! $node->expr instanceof FuncCall) {
return null;
}
if (! $this->nodeNameResolver->isName($node->expr, 'strlen')) {
return null;
}
/** @var FuncCall $funcCall */
$funcCall = $node->expr;
return $funcCall->args[0]->value;
}
}