forked from PHP-CS-Fixer/PHP-CS-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NoUnreachableDefaultArgumentValueFixer.php
191 lines (153 loc) · 5.75 KB
/
NoUnreachableDefaultArgumentValueFixer.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
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\FunctionNotation;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Mark Scherer
* @author Lucas Manzke <lmanzke@outlook.com>
* @author Gregor Harlan <gharlan@web.de>
*/
final class NoUnreachableDefaultArgumentValueFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'In function arguments there must not be arguments with default values before non-default ones.',
[
new CodeSample(
'<?php
function example($foo = "two words", $bar) {}
'
),
],
null,
'Modifies the signature of functions; therefore risky when using systems (such as some Symfony components) that rely on those (for example through reflection).'
);
}
/**
* {@inheritdoc}
*
* Must run after NullableTypeDeclarationForDefaultNullValueFixer.
*/
public function getPriority(): int
{
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
}
public function isRisky(): bool
{
return true;
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$functionKinds = [T_FUNCTION, T_FN];
for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
if (!$tokens[$i]->isGivenKind($functionKinds)) {
continue;
}
$startIndex = $tokens->getNextTokenOfKind($i, ['(']);
$i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
$this->fixFunctionDefinition($tokens, $startIndex, $i);
}
}
private function fixFunctionDefinition(Tokens $tokens, int $startIndex, int $endIndex): void
{
$lastArgumentIndex = $this->getLastNonDefaultArgumentIndex($tokens, $startIndex, $endIndex);
if (null === $lastArgumentIndex) {
return;
}
for ($i = $lastArgumentIndex; $i > $startIndex; --$i) {
$token = $tokens[$i];
if ($token->isGivenKind(T_VARIABLE)) {
$lastArgumentIndex = $i;
continue;
}
if (!$token->equals('=') || $this->isNonNullableTypehintedNullableVariable($tokens, $i)) {
continue;
}
$this->removeDefaultValue($tokens, $i, $this->getDefaultValueEndIndex($tokens, $lastArgumentIndex));
}
}
private function getLastNonDefaultArgumentIndex(Tokens $tokens, int $startIndex, int $endIndex): ?int
{
for ($i = $endIndex - 1; $i > $startIndex; --$i) {
$token = $tokens[$i];
if ($token->equals('=')) {
$i = $tokens->getPrevMeaningfulToken($i);
continue;
}
if ($token->isGivenKind(T_VARIABLE) && !$this->isEllipsis($tokens, $i)) {
return $i;
}
}
return null;
}
private function isEllipsis(Tokens $tokens, int $variableIndex): bool
{
return $tokens[$tokens->getPrevMeaningfulToken($variableIndex)]->isGivenKind(T_ELLIPSIS);
}
private function getDefaultValueEndIndex(Tokens $tokens, int $index): int
{
do {
$index = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$index]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
$index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);
}
} while (!$tokens[$index]->equals(','));
return $tokens->getPrevMeaningfulToken($index);
}
private function removeDefaultValue(Tokens $tokens, int $startIndex, int $endIndex): void
{
for ($i = $startIndex; $i <= $endIndex;) {
$tokens->clearTokenAndMergeSurroundingWhitespace($i);
$this->clearWhitespacesBeforeIndex($tokens, $i);
$i = $tokens->getNextMeaningfulToken($i);
}
}
/**
* @param int $index Index of "="
*/
private function isNonNullableTypehintedNullableVariable(Tokens $tokens, int $index): bool
{
$nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
if (!$nextToken->equals([T_STRING, 'null'], false)) {
return false;
}
$variableIndex = $tokens->getPrevMeaningfulToken($index);
$searchTokens = [',', '(', [T_STRING], [CT::T_ARRAY_TYPEHINT], [T_CALLABLE]];
$typehintKinds = [T_STRING, CT::T_ARRAY_TYPEHINT, T_CALLABLE];
$prevIndex = $tokens->getPrevTokenOfKind($variableIndex, $searchTokens);
if (!$tokens[$prevIndex]->isGivenKind($typehintKinds)) {
return false;
}
return !$tokens[$tokens->getPrevMeaningfulToken($prevIndex)]->isGivenKind(CT::T_NULLABLE_TYPE);
}
private function clearWhitespacesBeforeIndex(Tokens $tokens, int $index): void
{
$prevIndex = $tokens->getNonEmptySibling($index, -1);
if (!$tokens[$prevIndex]->isWhitespace()) {
return;
}
$prevNonWhiteIndex = $tokens->getPrevNonWhitespace($prevIndex);
if (null === $prevNonWhiteIndex || !$tokens[$prevNonWhiteIndex]->isComment()) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}
}
}