forked from driftingly/rector-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubStrToStartsWithOrEndsWithStaticMethodCallRector.php
113 lines (94 loc) · 3 KB
/
SubStrToStartsWithOrEndsWithStaticMethodCallRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\Expr\SubStrToStartsWithOrEndsWithStaticMethodCallRector;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\Expr\SubStrToStartsWithOrEndsWithStaticMethodCallRector\SubStrToStartsWithOrEndsWithStaticMethodCallRectorTest
*/
class SubStrToStartsWithOrEndsWithStaticMethodCallRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use Str::startsWith() or Str::endsWith() instead of substr() === $str', [
new CodeSample(
<<<'CODE_SAMPLE'
if (substr($str, 0, 3) === 'foo') {
// do something
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if (Str::startsWith($str, 'foo')) {
// do something
}
CODE_SAMPLE
,
),
]);
}
public function getNodeTypes(): array
{
return [Expr::class];
}
/**
* @param Expr $node
*/
public function refactor(Node $node): ?StaticCall
{
if (! $node instanceof Identical && ! $node instanceof Equal) {
return null;
}
/** @var Expr\FuncCall|null $functionCall */
$functionCall = array_values(
array_filter([$node->left, $node->right], fn ($node) => $node instanceof FuncCall && $this->isName(
$node,
'substr'
))
)[0] ?? null;
if (! $functionCall instanceof FuncCall) {
return null;
}
/** @var Expr $otherNode */
$otherNode = array_values(
array_filter([$node->left, $node->right], static fn ($node) => $node !== $functionCall)
)[0] ?? null;
// get the function call second argument value
if (count($functionCall->getArgs()) < 2) {
return null;
}
$secondArgument = $this->valueResolver->getValue($functionCall->getArgs()[1]->value);
if (! is_int($secondArgument)) {
return null;
}
if ($secondArgument < 0 && isset($functionCall->getArgs()[2])) {
return null;
}
$methodName = $this->getStaticMethodName($secondArgument);
if ($methodName === null) {
return null;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Str', $methodName, [
$functionCall->getArgs()[0]
->value,
$otherNode,
]);
}
protected function getStaticMethodName(int $secondArgument): ?string
{
if ($secondArgument === 0) {
return 'startsWith';
}
if ($secondArgument < 0) {
return 'endsWith';
}
return null;
}
}