-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathSubstrMatchAndRefactor.php
More file actions
130 lines (102 loc) · 4.14 KB
/
SubstrMatchAndRefactor.php
File metadata and controls
130 lines (102 loc) · 4.14 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
<?php
declare(strict_types=1);
namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php80\Contract\StrStartWithMatchAndRefactorInterface;
use Rector\Php80\NodeFactory\StrStartsWithFuncCallFactory;
use Rector\Php80\ValueObject\StrStartsWith;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\Value\ValueResolver;
final readonly class SubstrMatchAndRefactor implements StrStartWithMatchAndRefactorInterface
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ValueResolver $valueResolver,
private NodeComparator $nodeComparator,
private StrStartsWithFuncCallFactory $strStartsWithFuncCallFactory,
) {
}
public function match(Identical|NotIdentical|Equal|NotEqual $binaryOp): ?StrStartsWith
{
$isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal;
if ($binaryOp->left instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->left, 'substr')) {
$funcCall = $binaryOp->left;
$haystack = $funcCall->getArgs()[0]
->value;
return new StrStartsWith($funcCall, $haystack, $binaryOp->right, $isPositive);
}
if ($binaryOp->right instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->right, 'substr')) {
$funcCall = $binaryOp->right;
$haystack = $funcCall->getArgs()[0]
->value;
return new StrStartsWith($funcCall, $haystack, $binaryOp->left, $isPositive);
}
return null;
}
public function refactorStrStartsWith(StrStartsWith $strStartsWith): ?Node
{
if ($this->isStrlenWithNeedleExpr($strStartsWith)) {
return $this->strStartsWithFuncCallFactory->createStrStartsWith($strStartsWith);
}
if ($this->isHardcodedStringWithLNumberLength($strStartsWith)) {
return $this->strStartsWithFuncCallFactory->createStrStartsWith($strStartsWith);
}
return null;
}
private function isStrlenWithNeedleExpr(StrStartsWith $strStartsWith): bool
{
$substrFuncCall = $strStartsWith->getFuncCall();
if ($substrFuncCall->isFirstClassCallable()) {
return false;
}
$firstArg = $substrFuncCall->getArgs()[1];
if (! $this->valueResolver->isValue($firstArg->value, 0)) {
return false;
}
$secondFuncCallArgValue = $substrFuncCall->getArgs()[2]
->value;
if (! $secondFuncCallArgValue instanceof FuncCall) {
return false;
}
if (! $this->nodeNameResolver->isName($secondFuncCallArgValue, 'strlen')) {
return false;
}
$strlenFuncCall = $secondFuncCallArgValue;
$needleExpr = $strlenFuncCall->getArgs()[0]
->value;
$comparedNeedleExpr = $strStartsWith->getNeedleExpr();
return $this->nodeComparator->areNodesEqual($needleExpr, $comparedNeedleExpr);
}
private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith): bool
{
$substrFuncCall = $strStartsWith->getFuncCall();
if ($substrFuncCall->isFirstClassCallable()) {
return false;
}
$secondArg = $substrFuncCall->getArgs()[1];
if (! $this->valueResolver->isValue($secondArg->value, 0)) {
return false;
}
$expr = $strStartsWith->getNeedleExpr();
if (! $expr instanceof String_) {
return false;
}
if (count($substrFuncCall->getArgs()) < 3) {
return false;
}
$lNumberLength = $substrFuncCall->getArgs()[2]
->value;
if (! $lNumberLength instanceof Int_) {
return false;
}
return $lNumberLength->value === strlen($expr->value);
}
}