Skip to content

Commit

Permalink
fix: error on argument split with default value as array
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Jun 14, 2023
1 parent 2039e38 commit 206ffee
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$openBraceIndex = $tokens->getNextMeaningfulToken($nextIndex);
$openBrace = $tokens[$openBraceIndex];

if (false === $openBrace->equals('(')) {
if ('(' !== $openBrace->getContent()) {
continue;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ private function splitArgs(Tokens $tokens, $index): void
return;
}

if ($tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)]->equals('{')) {
if ('{' === $tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)]->getContent()) {
$tokens->removeTrailingWhitespace($closeBraceIndex);
$tokens->ensureWhitespaceAtIndex($closeBraceIndex, 1, ' ');
}
Expand All @@ -182,15 +182,15 @@ private function splitArgs(Tokens $tokens, $index): void
$linebreaks = [$openBraceIndex, $closeBraceIndex - 1];

for ($i = $openBraceIndex + 1; $i < $closeBraceIndex; ++$i) {
if ($tokens[$i]->equals('(')) {
if ('(' === $tokens[$i]->getContent()) {
$i = $this->analyze($tokens)->getClosingParenthesis($i);
}

if ($tokens[$i]->equals('[')) {
if ('[' === $tokens[$i]->getContent()) {
$i = $this->analyze($tokens)->getClosingBracket($i);
}

if ($tokens[$i]->equals(',')) {
if (',' === $tokens[$i]->getContent()) {
$linebreaks[] = $i;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ private function mergeArgs(Tokens $tokens, $index): void

$end = $tokens->getNextTokenOfKind($closeBraceIndex, [';', '{']);

if ($tokens[$end]->equals('{')) {
if ('{' === $tokens[$end]->getContent()) {
$tokens->removeLeadingWhitespace($end);
$tokens->ensureWhitespaceAtIndex($end, -1, "\n".$this->analyze($tokens)->getLineIndentation($index));
}
Expand All @@ -247,11 +247,11 @@ private function localizeNextCloseBrace(Tokens $tokens, $index)
$opening = 0;

for ($i = $index + 1; $i < $tokens->count(); ++$i) {
if ($tokens[$i]->equals('(')) {
if ('(' === $tokens[$i]->getContent()) {
++$opening;
}

if ($tokens[$i]->equals(')')) {
if (')' === $tokens[$i]->getContent()) {
if ($opening > 0) {
--$opening;
}
Expand All @@ -268,11 +268,11 @@ private function localizeNextCloseBracket(Tokens $tokens, $index)
$opening = 0;

for ($i = $index + 1; $i < $tokens->count(); ++$i) {
if ($tokens[$i]->equals('[')) {
if ('[' === $tokens[$i]->getContent()) {
++$opening;
}

if ($tokens[$i]->equals(']')) {
if (']' === $tokens[$i]->getContent()) {
if ($opening > 0) {
--$opening;
}
Expand All @@ -292,23 +292,23 @@ private function getNumberOfArguments(Tokens $tokens, $index)

$open = $tokens->getNextTokenOfKind($index, ['(']);

if ($tokens[$tokens->getNextMeaningfulToken($open)]->equals(')')) {
if (')' === $tokens[$tokens->getNextMeaningfulToken($open)]->getContent()) {
return 0;
}

$close = $this->analyze($tokens)->getClosingParenthesis($open);
$arguments = 1;

for ($i = $open + 1; $i < $close; ++$i) {
if ($tokens[$i]->equals('(')) {
if ('(' === $tokens[$i]->getContent()) {
$i = $this->analyze($tokens)->getClosingParenthesis($i);
}

if ($tokens[$i]->equals('[')) {
if ('[' === $tokens[$i]->getContent()) {
$i = $this->analyze($tokens)->getClosingBracket($i);
}

if ($tokens[$i]->equals(',')) {
if (',' === $tokens[$i]->getContent()) {
++$arguments;
}
}
Expand Down
54 changes: 27 additions & 27 deletions src/PedroTroller/CS/Fixer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getMethodArguments($index)

$next = $this->tokens->getNextMeaningfulToken($argumentName);

if ($this->tokens[$next]->equals('=')) {
if ('=' === $this->tokens[$next]->getContent()) {
$argumentAsDefault = true;
$value = $this->tokens->getNextMeaningfulToken($next);
$argumentNullable = 'null' === $this->tokens[$value]->getContent();
Expand Down Expand Up @@ -120,25 +120,25 @@ public function getNextComma($index)
}

switch (true) {
case $this->tokens[$index]->equals('('):
case '(' === $this->tokens[$index]->getContent():
$index = $this->getClosingParenthesis($index);

break;

case $this->tokens[$index]->equals('['):
case '[' === $this->tokens[$index]->getContent():
$index = $this->getClosingBracket($index);

break;

case $this->tokens[$index]->equals('{'):
case '{' === $this->tokens[$index]->getContent():
$index = $this->getClosingCurlyBracket($index);

break;

case $this->tokens[$index]->equals(';'):
case ';' === $this->tokens[$index]->getContent():
return null;
}
} while (false === $this->tokens[$index]->equals(','));
} while (',' !== $this->tokens[$index]->getContent());

return $index;
}
Expand All @@ -158,22 +158,22 @@ public function getNextSemiColon($index)
}

switch (true) {
case $this->tokens[$index]->equals('('):
case '(' === $this->tokens[$index]->getContent():
$index = $this->getClosingParenthesis($index);

break;

case $this->tokens[$index]->equals('['):
case '[' === $this->tokens[$index]->getContent():
$index = $this->getClosingBracket($index);

break;

case $this->tokens[$index]->equals('{'):
case '{' === $this->tokens[$index]->getContent():
$index = $this->getClosingCurlyBracket($index);

break;
}
} while (false === $this->tokens[$index]->equals(';'));
} while (';' !== $this->tokens[$index]->getContent());

return $index;
}
Expand Down Expand Up @@ -219,12 +219,12 @@ public function getReturnedType($index)
$return = $this->tokens[$next]->getContent();
++$next;

if ($this->tokens[$next]->isWhitespace() || $this->tokens[$next]->equals(';')) {
if ($this->tokens[$next]->isWhitespace() || ';' === $this->tokens[$next]->getContent()) {
return $optionnal
? [$return, null]
: $return;
}
} while (false === $this->tokens[$index]->equals(['{', ';']));
} while (false === \in_array($this->tokens[$index]->getContent(), ['{', ';'], true));
}

/**
Expand Down Expand Up @@ -289,22 +289,22 @@ public function endOfTheStatement(int $index): ?int
}

switch (true) {
case $this->tokens[$index]->equals('('):
case '(' === $this->tokens[$index]->getContent():
$index = $this->getClosingParenthesis($index);

break;

case $this->tokens[$index]->equals('['):
case '[' === $this->tokens[$index]->getContent():
$index = $this->getClosingBracket($index);

break;

case $this->tokens[$index]->equals('{'):
case '{' === $this->tokens[$index]->getContent():
$index = $this->getClosingCurlyBracket($index);

break;
}
} while (false === $this->tokens[$index]->equals('}'));
} while ('}' !== $this->tokens[$index]->getContent());

return $index;
}
Expand All @@ -316,12 +316,12 @@ public function endOfTheStatement(int $index): ?int
*/
public function getClosingParenthesis($index)
{
if (false === $this->tokens[$index]->equals('(')) {
if ('(' !== $this->tokens[$index]->getContent()) {
throw new Exception(sprintf('Expected token: (. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('(')) {
if ('(' === $this->tokens[$i]->getContent()) {
$i = $this->getClosingParenthesis($i);

if (null === $i) {
Expand All @@ -331,7 +331,7 @@ public function getClosingParenthesis($index)
continue;
}

if ($this->tokens[$i]->equals(')')) {
if (')' === $this->tokens[$i]->getContent()) {
return $i;
}
}
Expand All @@ -344,12 +344,12 @@ public function getClosingParenthesis($index)
*/
public function getClosingBracket($index)
{
if (false === $this->tokens[$index]->equals('[')) {
if ('[' !== $this->tokens[$index]->getContent()) {
throw new Exception(sprintf('Expected token: [. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('[')) {
if ('[' === $this->tokens[$i]->getContent()) {
$i = $this->getClosingBracket($i);

if (null === $i) {
Expand All @@ -359,7 +359,7 @@ public function getClosingBracket($index)
continue;
}

if ($this->tokens[$i]->equals(']')) {
if (']' === $this->tokens[$i]->getContent()) {
return $i;
}
}
Expand All @@ -372,12 +372,12 @@ public function getClosingBracket($index)
*/
public function getClosingCurlyBracket($index)
{
if (false === $this->tokens[$index]->equals('{')) {
if ('{' !== $this->tokens[$index]->getContent()) {
throw new Exception(sprintf('Expected token: {. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('{')) {
if ('{' === $this->tokens[$i]->getContent()) {
$i = $this->getClosingCurlyBracket($i);

if (null === $i) {
Expand All @@ -387,7 +387,7 @@ public function getClosingCurlyBracket($index)
continue;
}

if ($this->tokens[$i]->equals('}')) {
if ('}' === $this->tokens[$i]->getContent()) {
return $i;
}
}
Expand Down Expand Up @@ -529,7 +529,7 @@ public function getElements($startIndex = null)
for ($i = $startIndex;; ++$i) {
$token = $this->tokens[$i];

if ($token->equals('}')) {
if ('}' === $token->getContent()) {
return $elements;
}

Expand Down Expand Up @@ -631,7 +631,7 @@ private function findElementEnd(Tokens $tokens, $index)
{
$index = $tokens->getNextTokenOfKind($index, ['{', ';']);

if ($tokens[$index]->equals('{')) {
if ('{' === $tokens[$index]->getContent()) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/TokensAnalyzerIntegration/NextComma.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace tests\TokensAnalyzerIntegration;

use PedroTroller\CS\Fixer\TokensAnalyzer;
use PhpCsFixer\Tokenizer\Tokens;
use tests\TokensAnalyzerIntegration;
use Webmozart\Assert\Assert;

final class NextComma extends TokensAnalyzerIntegration
{
public function getCode()
{
return <<<'PHP'
<?php
namespace TheNamespace;
class TheClass
{
public function __construct(
public string $theString = 'N/A',
public array $theArray = [1, 2, 3, 4]
) {
}
}
PHP;
}

public function assertions(TokensAnalyzer $analyzer, Tokens $tokens): void
{
Assert::eq(
$analyzer->getNextComma(
$this->tokenContaining($tokens, '$theString')
),
$this->tokenContaining($tokens, '$theString') + 5,
);

Assert::eq(
$analyzer->getNextComma(
$this->tokenContaining($tokens, '$theArray')
),
null,
);
}
}
Loading

0 comments on commit 206ffee

Please sign in to comment.