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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/*
* 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\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
{
/**
* {@inheritdoc}
*/
public function getDefinition()
{
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}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_FUNCTION);
}
/**
* {@inheritdoc}
*/
public function isRisky()
{
return true;
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
if (!$tokens[$i]->isGivenKind(T_FUNCTION)) {
continue;
}
$startIndex = $tokens->getNextTokenOfKind($i, ['(']);
$i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
$this->fixFunctionDefinition($tokens, $startIndex, $i);
}
}
/**
* @param Tokens $tokens
* @param int $startIndex
* @param int $endIndex
*/
private function fixFunctionDefinition(Tokens $tokens, $startIndex, $endIndex)
{
$lastArgumentIndex = $this->getLastNonDefaultArgumentIndex($tokens, $startIndex, $endIndex);
if (!$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;
}
$endIndex = $tokens->getPrevTokenOfKind($lastArgumentIndex, [',']);
$endIndex = $tokens->getPrevMeaningfulToken($endIndex);
$this->removeDefaultArgument($tokens, $i, $endIndex);
}
}
/**
* @param Tokens $tokens
* @param int $startIndex
* @param int $endIndex
*
* @return null|int
*/
private function getLastNonDefaultArgumentIndex(Tokens $tokens, $startIndex, $endIndex)
{
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;
}
}
}
/**
* @param Tokens $tokens
* @param int $variableIndex
*
* @return bool
*/
private function isEllipsis(Tokens $tokens, $variableIndex)
{
return $tokens[$tokens->getPrevMeaningfulToken($variableIndex)]->isGivenKind(T_ELLIPSIS);
}
/**
* @param Tokens $tokens
* @param int $startIndex
* @param int $endIndex
*/
private function removeDefaultArgument(Tokens $tokens, $startIndex, $endIndex)
{
for ($i = $startIndex; $i <= $endIndex;) {
$tokens->clearTokenAndMergeSurroundingWhitespace($i);
$this->clearWhitespacesBeforeIndex($tokens, $i);
$i = $tokens->getNextMeaningfulToken($i);
}
}
/**
* @param Tokens $tokens
* @param int $index Index of "="
*
* @return bool
*/
private function isNonNullableTypehintedNullableVariable(Tokens $tokens, $index)
{
$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);
}
/**
* @param Tokens $tokens
* @param int $index
*/
private function clearWhitespacesBeforeIndex(Tokens $tokens, $index)
{
$prevIndex = $tokens->getNonEmptySibling($index, -1);
if (!$tokens[$prevIndex]->isWhitespace()) {
return;
}
$prevNonWhiteIndex = $tokens->getPrevNonWhitespace($prevIndex);
if (null === $prevNonWhiteIndex || !$tokens[$prevNonWhiteIndex]->isComment()) {
$tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
}
}
}