Skip to content

Commit eebb66a

Browse files
committed
bug #6161 NoUnreachableDefaultArgumentValueFixer - fix for attributes (kubawerlos)
This PR was squashed before being merged into the master branch. Discussion ---------- NoUnreachableDefaultArgumentValueFixer - fix for attributes Commits ------- 328fcee NoUnreachableDefaultArgumentValueFixer - fix for attributes
2 parents 241b491 + 328fcee commit eebb66a

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

src/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixer.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ public function isRisky(): bool
8282
*/
8383
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
8484
{
85+
$functionKinds = [T_FUNCTION];
86+
if (\defined('T_FN')) {
87+
$functionKinds[] = T_FN;
88+
}
89+
8590
for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
86-
if (
87-
!$tokens[$i]->isGivenKind(T_FUNCTION)
88-
&& (\PHP_VERSION_ID < 70400 || !$tokens[$i]->isGivenKind(T_FN))
89-
) {
91+
if (!$tokens[$i]->isGivenKind($functionKinds)) {
9092
continue;
9193
}
9294

@@ -118,9 +120,7 @@ private function fixFunctionDefinition(Tokens $tokens, int $startIndex, int $end
118120
continue;
119121
}
120122

121-
$endIndex = $tokens->getPrevTokenOfKind($lastArgumentIndex, [',']);
122-
$endIndex = $tokens->getPrevMeaningfulToken($endIndex);
123-
$this->removeDefaultArgument($tokens, $i, $endIndex);
123+
$this->removeDefaultValue($tokens, $i, $this->getDefaultValueEndIndex($tokens, $lastArgumentIndex));
124124
}
125125
}
126126

@@ -148,7 +148,20 @@ private function isEllipsis(Tokens $tokens, int $variableIndex): bool
148148
return $tokens[$tokens->getPrevMeaningfulToken($variableIndex)]->isGivenKind(T_ELLIPSIS);
149149
}
150150

151-
private function removeDefaultArgument(Tokens $tokens, int $startIndex, int $endIndex): void
151+
private function getDefaultValueEndIndex(Tokens $tokens, int $index): int
152+
{
153+
do {
154+
$index = $tokens->getPrevMeaningfulToken($index);
155+
156+
if ($tokens[$index]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
157+
$index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ATTRIBUTE, $index);
158+
}
159+
} while (!$tokens[$index]->equals(','));
160+
161+
return $tokens->getPrevMeaningfulToken($index);
162+
}
163+
164+
private function removeDefaultValue(Tokens $tokens, int $startIndex, int $endIndex): void
152165
{
153166
for ($i = $startIndex; $i <= $endIndex;) {
154167
$tokens->clearTokenAndMergeSurroundingWhitespace($i);

tests/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ public function provideFix74Cases(): array
195195
];
196196
}
197197

198+
/**
199+
* @dataProvider provideFix80Cases
200+
* @requires PHP 8.0
201+
*/
202+
public function testFix80(string $expected, ?string $input = null): void
203+
{
204+
$this->doTest($expected, $input);
205+
}
206+
207+
public function provideFix80Cases(): \Generator
208+
{
209+
yield 'handle trailing comma' => [
210+
'<?php function foo($x, $y = 42, $z = 42 ) {}',
211+
];
212+
213+
yield 'handle attributes without arguments' => [
214+
'<?php function foo(
215+
#[Attribute1] $x,
216+
#[Attribute2] $y,
217+
#[Attribute3] $z
218+
) {}',
219+
'<?php function foo(
220+
#[Attribute1] $x,
221+
#[Attribute2] $y = 42,
222+
#[Attribute3] $z
223+
) {}',
224+
];
225+
226+
yield 'handle attributes with arguments' => [
227+
'<?php function foo(
228+
#[Attribute1(1, 2)] $x,
229+
#[Attribute2(3, 4)] $y,
230+
#[Attribute3(5, 6)] $z
231+
) {}',
232+
'<?php function foo(
233+
#[Attribute1(1, 2)] $x,
234+
#[Attribute2(3, 4)] $y = 42,
235+
#[Attribute3(5, 6)] $z
236+
) {}',
237+
];
238+
}
239+
198240
/**
199241
* @dataProvider provideFix81Cases
200242
* @requires PHP 8.1

0 commit comments

Comments
 (0)