Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[CodingStandard] apply recommendations to the php-cs-fixer fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 29, 2017
1 parent 30f8fac commit 818510a
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function isRisky(): bool

public function fix(SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
for ($index = count($tokens) - 1; $index > 1; --$index) {
$token = $tokens[$index];

if (! $token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
Expand All @@ -45,8 +47,15 @@ public function fix(SplFileInfo $file, Tokens $tokens): void
}

$semicolonOrDefaultValuePosition = $tokens->getNextMeaningfulToken($index + 4);
$variableToken = $tokens->getNextTokenOfKind($index, [T_VARIABLE]);
// use getNextTokenOfKind to search for T_VARIABLE
dump($semicolonOrDefaultValuePosition);
if ($variableToken === null) {
break;
}

$semicolonOrDefaultValueToken = $tokens[$semicolonOrDefaultValuePosition];
if ($semicolonOrDefaultValueToken->equals(';')) {
if (! $semicolonOrDefaultValueToken->equals(';')) {
$this->addDefaultValueForArrayProperty($tokens, $semicolonOrDefaultValuePosition);
}
}
Expand Down Expand Up @@ -90,11 +99,14 @@ private function isArrayPropertyDocComment(Token $token): bool

private function addDefaultValueForArrayProperty(Tokens $tokens, int $semicolonPosition): void
{
// todo: prepare nicer api
$tokens->insertAt($semicolonPosition, new Token([CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']']));
$tokens->insertAt($semicolonPosition, new Token([CT::T_ARRAY_SQUARE_BRACE_OPEN, '[']));
$tokens->insertAt($semicolonPosition, new Token([T_WHITESPACE, ' ']));
$tokens->insertAt($semicolonPosition, new Token('='));
$tokens->insertAt($semicolonPosition, new Token([T_WHITESPACE, ' ']));
$tokens[$semicolonPosition]->clear();

$tokens->insertAt($semicolonPosition, [
new Token([T_WHITESPACE, ' ']),
new Token('='),
new Token([T_WHITESPACE, ' ']),
new Token([CT::T_ARRAY_SQUARE_BRACE_OPEN, '[']),
new Token([CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']'])
]);
}
}

0 comments on commit 818510a

Please sign in to comment.