forked from driftingly/rector-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinutesToSecondsInCacheRector.php
156 lines (132 loc) · 4.59 KB
/
MinutesToSecondsInCacheRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Mul;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\LNumber;
use Rector\Rector\AbstractRector;
use RectorLaravel\ValueObject\TypeToTimeMethodAndPosition;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/laravel/framework/pull/27276
* @changelog https://laravel.com/docs/5.8/upgrade#cache-ttl-in-seconds
*
* @see \RectorLaravel\Tests\Rector\StaticCall\MinutesToSecondsInCacheRector\MinutesToSecondsInCacheRectorTest
*/
final class MinutesToSecondsInCacheRector extends AbstractRector
{
/**
* @var string
*/
private const PUT = 'put';
/**
* @var string
*/
private const ADD = 'add';
/**
* @var string
*/
private const REMEMBER = 'remember';
/**
* @var TypeToTimeMethodAndPosition[]
*/
private array $typeToTimeMethodsAndPositions = [];
public function __construct()
{
$this->typeToTimeMethodsAndPositions = [
new TypeToTimeMethodAndPosition('Illuminate\Support\Facades\Cache', self::PUT, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Repository', self::PUT, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Store', self::PUT, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Repository', self::ADD, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Store', self::ADD, 2),
new TypeToTimeMethodAndPosition('Illuminate\Support\Facades\Cache', self::ADD, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Repository', self::REMEMBER, 2),
new TypeToTimeMethodAndPosition('Illuminate\Support\Facades\Cache', self::REMEMBER, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Store', self::REMEMBER, 2),
new TypeToTimeMethodAndPosition('Illuminate\Contracts\Cache\Store', 'putMany', 1),
];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change minutes argument to seconds in Illuminate\Contracts\Cache\Store and Illuminate\Support\Facades\Cache',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
Illuminate\Support\Facades\Cache::put('key', 'value', 60);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
Illuminate\Support\Facades\Cache::put('key', 'value', 60 * 60);
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class, MethodCall::class];
}
/**
* @param StaticCall|MethodCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->typeToTimeMethodsAndPositions as $typeToTimeMethodAndPosition) {
if (! $this->isObjectType(
$node instanceof MethodCall ? $node->var : $node->class,
$typeToTimeMethodAndPosition->getObjectType()
)) {
continue;
}
if (! $this->isName($node->name, $typeToTimeMethodAndPosition->getMethodName())) {
continue;
}
if (! isset($node->args[$typeToTimeMethodAndPosition->getPosition()])) {
continue;
}
if (! $node->args[$typeToTimeMethodAndPosition->getPosition()] instanceof Arg) {
continue;
}
$argValue = $node->args[$typeToTimeMethodAndPosition->getPosition()]->value;
return $this->processArgumentOnPosition($node, $argValue, $typeToTimeMethodAndPosition->getPosition());
}
return $node;
}
private function processArgumentOnPosition(
StaticCall|MethodCall $node,
Expr $argExpr,
int $argumentPosition
): StaticCall|MethodCall|null {
if (! $this->nodeTypeResolver->isNumberType($argExpr)) {
return null;
}
// already multiplied
if ($argExpr instanceof Mul) {
return null;
}
$mul = new Mul($argExpr, new LNumber(60));
$node->args[$argumentPosition] = new Arg($mul);
return $node;
}
}