Skip to content

Commit

Permalink
NoUnreachableDefaultArgumentValueFixer - fix for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and keradus committed Dec 11, 2021
1 parent 241b491 commit 328fcee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ public function isRisky(): bool
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$functionKinds = [T_FUNCTION];
if (\defined('T_FN')) {
$functionKinds[] = T_FN;
}

for ($i = 0, $l = $tokens->count(); $i < $l; ++$i) {
if (
!$tokens[$i]->isGivenKind(T_FUNCTION)
&& (\PHP_VERSION_ID < 70400 || !$tokens[$i]->isGivenKind(T_FN))
) {
if (!$tokens[$i]->isGivenKind($functionKinds)) {
continue;
}

Expand Down Expand Up @@ -118,9 +120,7 @@ private function fixFunctionDefinition(Tokens $tokens, int $startIndex, int $end
continue;
}

$endIndex = $tokens->getPrevTokenOfKind($lastArgumentIndex, [',']);
$endIndex = $tokens->getPrevMeaningfulToken($endIndex);
$this->removeDefaultArgument($tokens, $i, $endIndex);
$this->removeDefaultValue($tokens, $i, $this->getDefaultValueEndIndex($tokens, $lastArgumentIndex));
}
}

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

private function removeDefaultArgument(Tokens $tokens, int $startIndex, int $endIndex): void
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,48 @@ public function provideFix74Cases(): array
];
}

/**
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFix80Cases(): \Generator
{
yield 'handle trailing comma' => [
'<?php function foo($x, $y = 42, $z = 42 ) {}',
];

yield 'handle attributes without arguments' => [
'<?php function foo(
#[Attribute1] $x,
#[Attribute2] $y,
#[Attribute3] $z
) {}',
'<?php function foo(
#[Attribute1] $x,
#[Attribute2] $y = 42,
#[Attribute3] $z
) {}',
];

yield 'handle attributes with arguments' => [
'<?php function foo(
#[Attribute1(1, 2)] $x,
#[Attribute2(3, 4)] $y,
#[Attribute3(5, 6)] $z
) {}',
'<?php function foo(
#[Attribute1(1, 2)] $x,
#[Attribute2(3, 4)] $y = 42,
#[Attribute3(5, 6)] $z
) {}',
];
}

/**
* @dataProvider provideFix81Cases
* @requires PHP 8.1
Expand Down

0 comments on commit 328fcee

Please sign in to comment.