forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetCookieOptionsArrayToArgumentsRector.php
180 lines (151 loc) · 4.42 KB
/
SetCookieOptionsArrayToArgumentsRector.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
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp73\Rector\FuncCall;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp73\Rector\FuncCall\SetCookieOptionsArrayToArgumentsRector\SetCookieOptionsArrayToArgumentsRectorTest
*/
final class SetCookieOptionsArrayToArgumentsRector extends AbstractRector
{
/**
* Conversion table from argument index to options name
* @var array<string, int>
*/
private const ARGUMENT_ORDER = [
'expires' => 2,
'path' => 3,
'domain' => 4,
'secure' => 5,
'httponly' => 6,
];
/**
* Conversion table from argument index to options name
* @var array<int, int|string|bool>
*/
private const ARGUMENT_DEFAULT_VALUES = [
2 => 0,
3 => '',
4 => '',
5 => false,
6 => false,
];
/**
* @var int
*/
private $highestIndex = 1;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Convert setcookie option array to arguments',
[
new CodeSample(
<<<'CODE_SAMPLE'
setcookie('name', $value, ['expires' => 360]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
setcookie('name', $value, 360);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
$node->args = $this->composeNewArgs($node);
return $node;
}
private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isNames($funcCall, ['setcookie', 'setrawcookie'])) {
return true;
}
$argsCount = count($funcCall->args);
if ($argsCount <= 2) {
return true;
}
return ! ($funcCall->args[2]->value instanceof Array_);
}
/**
* @return Arg[]
*/
private function composeNewArgs(FuncCall $funcCall): array
{
$this->highestIndex = 1;
$newArgs = [$funcCall->args[0], $funcCall->args[1]];
/** @var Array_ $optionsArray */
$optionsArray = $funcCall->args[2]->value;
/** @var ArrayItem|null $arrayItem */
foreach ($optionsArray->items as $arrayItem) {
if ($arrayItem === null) {
continue;
}
/** @var Arg $value */
$value = $arrayItem->value;
/** @var String_ $key */
$key = $arrayItem->key;
$name = $key->value;
if (! $this->isMappableArrayKey($name)) {
continue;
}
$order = self::ARGUMENT_ORDER[$name];
if ($order > $this->highestIndex) {
$this->highestIndex = $order;
}
$newArgs[$order] = $value;
}
$newArgs = $this->fillMissingArgumentsWithDefaultValues($newArgs);
ksort($newArgs);
return $newArgs;
}
private function isMappableArrayKey(string $key): bool
{
return isset(self::ARGUMENT_ORDER[$key]);
}
/**
* @param Arg[] $args
* @return Arg[]
*/
private function fillMissingArgumentsWithDefaultValues(array $args): array
{
for ($i = 1; $i < $this->highestIndex; ++$i) {
if (isset($args[$i])) {
continue;
}
$args[$i] = $this->createDefaultValueArg($i);
}
return $args;
}
private function createDefaultValueArg(int $argumentIndex): Arg
{
if (! array_key_exists($argumentIndex, self::ARGUMENT_DEFAULT_VALUES)) {
throw new ShouldNotHappenException();
}
$argumentDefaultValue = self::ARGUMENT_DEFAULT_VALUES[$argumentIndex];
$expr = BuilderHelpers::normalizeValue($argumentDefaultValue);
return new Arg($expr);
}
}