From eab1e98f58bd7809e052ef01cf8021fd57d48b5b Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Mon, 20 Dec 2021 09:18:38 +0000 Subject: [PATCH] clean ups --- src/Cache/Cache.php | 2 -- src/Fixer/Comment/HeaderCommentFixer.php | 4 +-- .../Operator/TernaryOperatorSpacesFixer.php | 25 +++---------------- .../Analyzer/AlternativeSyntaxAnalyzer.php | 2 +- .../AlternativeSyntaxAnalyzerTest.php | 9 ++++--- 5 files changed, 10 insertions(+), 32 deletions(-) diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 2dea02cc37c..28f1ee24447 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -88,8 +88,6 @@ public function toJson(): string /** * @throws \InvalidArgumentException - * - * @return Cache */ public static function fromJson(string $json): self { diff --git a/src/Fixer/Comment/HeaderCommentFixer.php b/src/Fixer/Comment/HeaderCommentFixer.php index 46c052ed6e3..d0ec571e11d 100644 --- a/src/Fixer/Comment/HeaderCommentFixer.php +++ b/src/Fixer/Comment/HeaderCommentFixer.php @@ -148,12 +148,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void if (!isset($locationIndices[$locationIndex]) || $possibleLocation === $location) { $locationIndices[$locationIndex] = $possibleLocation; - - continue; } } - foreach (array_values($locationIndices) as $possibleLocation) { + foreach ($locationIndices as $possibleLocation) { // figure out where the comment should be placed $headerNewIndex = $this->findHeaderCommentInsertionIndex($tokens, $possibleLocation); diff --git a/src/Fixer/Operator/TernaryOperatorSpacesFixer.php b/src/Fixer/Operator/TernaryOperatorSpacesFixer.php index ef98a2bf7f8..2bbff51b131 100644 --- a/src/Fixer/Operator/TernaryOperatorSpacesFixer.php +++ b/src/Fixer/Operator/TernaryOperatorSpacesFixer.php @@ -18,6 +18,7 @@ use PhpCsFixer\FixerDefinition\CodeSample; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; +use PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer; use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis; use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer; use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer; @@ -63,6 +64,7 @@ public function isCandidate(Tokens $tokens): bool */ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void { + $alternativeSyntaxAnalyzer = new AlternativeSyntaxAnalyzer(); $gotoLabelAnalyzer = new GotoLabelAnalyzer(); $ternaryOperatorIndices = []; $excludedIndices = $this->getColonIndicesForSwitch($tokens); @@ -76,7 +78,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void continue; } - if ($this->belongsToAlternativeSyntax($tokens, $index)) { + if ($alternativeSyntaxAnalyzer->belongsToAlternativeSyntax($tokens, $index)) { continue; } @@ -121,27 +123,6 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } } - private function belongsToAlternativeSyntax(Tokens $tokens, int $index): bool - { - if (!$tokens[$index]->equals(':')) { - return false; - } - - $closeParenthesisIndex = $tokens->getPrevMeaningfulToken($index); - if ($tokens[$closeParenthesisIndex]->isGivenKind(T_ELSE)) { - return true; - } - if (!$tokens[$closeParenthesisIndex]->equals(')')) { - return false; - } - - $openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $closeParenthesisIndex); - - $alternativeControlStructureIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex); - - return $tokens[$alternativeControlStructureIndex]->isGivenKind([T_DECLARE, T_ELSEIF, T_FOR, T_FOREACH, T_IF, T_SWITCH, T_WHILE]); - } - /** * @return int[] */ diff --git a/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php b/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php index cc3817ffdf8..cf60325fdce 100644 --- a/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php +++ b/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php @@ -39,11 +39,11 @@ public function belongsToAlternativeSyntax(Tokens $tokens, int $index): bool } $openParenthesisIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $prevIndex); - $beforeOpenParenthesisIndex = $tokens->getPrevMeaningfulToken($openParenthesisIndex); return $tokens[$beforeOpenParenthesisIndex]->isGivenKind([ T_DECLARE, + T_ELSEIF, T_FOR, T_FOREACH, T_IF, diff --git a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php index 551378ea382..e894cbe3ef8 100644 --- a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php +++ b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php @@ -37,7 +37,8 @@ public function testBelongsToAlternativeSyntax(array $expectedPositives, string for ($index = $tokens->count() - 1; $index >= 0; --$index) { static::assertSame( \in_array($index, $expectedPositives, true), - (new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index) + (new AlternativeSyntaxAnalyzer())->belongsToAlternativeSyntax($tokens, $index), + '@ index: '.$index ); } } @@ -59,9 +60,9 @@ public function provideBelongsToAlternativeSyntaxCases(): iterable ' [ - [6, 14], - ' [ + [6, 17, 25], + ' [