diff --git a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php index 655cd26..3c7115a 100644 --- a/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php +++ b/src/PedroTroller/CS/Fixer/CodingStyle/LineBreakBetweenMethodArgumentsFixer.php @@ -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; } @@ -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, ' '); } @@ -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; } @@ -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)); } @@ -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; } @@ -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; } @@ -292,7 +292,7 @@ private function getNumberOfArguments(Tokens $tokens, $index) $open = $tokens->getNextTokenOfKind($index, ['(']); - if ($tokens[$tokens->getNextMeaningfulToken($open)]->equals(')')) { + if (')' === $tokens[$tokens->getNextMeaningfulToken($open)]->getContent()) { return 0; } @@ -300,15 +300,15 @@ private function getNumberOfArguments(Tokens $tokens, $index) $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; } } diff --git a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php index eeb5ab3..f356f96 100644 --- a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php +++ b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php @@ -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(); @@ -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; } @@ -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; } @@ -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)); } /** @@ -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; } @@ -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) { @@ -331,7 +331,7 @@ public function getClosingParenthesis($index) continue; } - if ($this->tokens[$i]->equals(')')) { + if (')' === $this->tokens[$i]->getContent()) { return $i; } } @@ -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) { @@ -359,7 +359,7 @@ public function getClosingBracket($index) continue; } - if ($this->tokens[$i]->equals(']')) { + if (']' === $this->tokens[$i]->getContent()) { return $i; } } @@ -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) { @@ -387,7 +387,7 @@ public function getClosingCurlyBracket($index) continue; } - if ($this->tokens[$i]->equals('}')) { + if ('}' === $this->tokens[$i]->getContent()) { return $i; } } @@ -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; } @@ -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); } diff --git a/tests/TokensAnalyzerIntegration/NextComma.php b/tests/TokensAnalyzerIntegration/NextComma.php new file mode 100644 index 0000000..c6fa751 --- /dev/null +++ b/tests/TokensAnalyzerIntegration/NextComma.php @@ -0,0 +1,48 @@ +getNextComma( + $this->tokenContaining($tokens, '$theString') + ), + $this->tokenContaining($tokens, '$theString') + 5, + ); + + Assert::eq( + $analyzer->getNextComma( + $this->tokenContaining($tokens, '$theArray') + ), + null, + ); + } +} diff --git a/tests/UseCase/LineBreakBetweenMethods/Regression/Case9.php b/tests/UseCase/LineBreakBetweenMethods/Regression/Case9.php new file mode 100644 index 0000000..6776c4e --- /dev/null +++ b/tests/UseCase/LineBreakBetweenMethods/Regression/Case9.php @@ -0,0 +1,68 @@ + $pages list of {@link Crud} pages + */ + public function __construct( + public string $label = 'N/A', + public string|null $icon = null, + public string $detailAndEdit = '', + public string $index = '', + public string|null $permission = null, + public array $pages = [Crud::PAGE_INDEX, Crud::PAGE_DETAIL, Crud::PAGE_EDIT] + ) { + } + } + PHP; + } + + public function getExpectation(): string + { + return $this->getRawScript(); + } + + public function getMinSupportedPhpVersion(): int + { + return 80200; + } +}