From 3c27597933d9ce0391c50c8b768ebb59b39c808b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Wed, 17 Jan 2024 11:43:55 +0100 Subject: [PATCH] fix: work correctly for a switch/case with ternary operator (#7756) --- src/Fixer/ListNotation/ListSyntaxFixer.php | 2 +- .../Operator/TernaryOperatorSpacesFixer.php | 2 +- .../Operator/TernaryToElvisOperatorFixer.php | 2 +- src/Tokenizer/Analyzer/SwitchAnalyzer.php | 8 +++--- ...ry_operator_spaces,operator_linebreak.test | 25 +++++++++++++++++++ .../Tokenizer/Analyzer/SwitchAnalyzerTest.php | 25 +++++++++++++++++++ 6 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 tests/Fixtures/Integration/misc/ternary_operator_spaces,operator_linebreak.test diff --git a/src/Fixer/ListNotation/ListSyntaxFixer.php b/src/Fixer/ListNotation/ListSyntaxFixer.php index f7f06bca574..3bc18cea7ec 100644 --- a/src/Fixer/ListNotation/ListSyntaxFixer.php +++ b/src/Fixer/ListNotation/ListSyntaxFixer.php @@ -69,7 +69,7 @@ public function getDefinition(): FixerDefinitionInterface */ public function getPriority(): int { - return 1; + return 2; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Fixer/Operator/TernaryOperatorSpacesFixer.php b/src/Fixer/Operator/TernaryOperatorSpacesFixer.php index a0b80a735bd..758c79393ba 100644 --- a/src/Fixer/Operator/TernaryOperatorSpacesFixer.php +++ b/src/Fixer/Operator/TernaryOperatorSpacesFixer.php @@ -44,7 +44,7 @@ public function getDefinition(): FixerDefinitionInterface */ public function getPriority(): int { - return 0; + return 1; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Fixer/Operator/TernaryToElvisOperatorFixer.php b/src/Fixer/Operator/TernaryToElvisOperatorFixer.php index 592b7cba15e..191d8bf9041 100644 --- a/src/Fixer/Operator/TernaryToElvisOperatorFixer.php +++ b/src/Fixer/Operator/TernaryToElvisOperatorFixer.php @@ -80,7 +80,7 @@ public function getDefinition(): FixerDefinitionInterface */ public function getPriority(): int { - return 1; + return 2; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Tokenizer/Analyzer/SwitchAnalyzer.php b/src/Tokenizer/Analyzer/SwitchAnalyzer.php index d57f580a0de..881b1cbaaef 100644 --- a/src/Tokenizer/Analyzer/SwitchAnalyzer.php +++ b/src/Tokenizer/Analyzer/SwitchAnalyzer.php @@ -31,13 +31,13 @@ public static function belongsToSwitch(Tokens $tokens, int $index): bool return false; } - $codeHash = $tokens->getCodeHash(); + $tokensHash = md5(serialize($tokens->toArray())); - if (!\array_key_exists($codeHash, self::$cache)) { - self::$cache[$codeHash] = self::getColonIndicesForSwitch(clone $tokens); + if (!\array_key_exists($tokensHash, self::$cache)) { + self::$cache[$tokensHash] = self::getColonIndicesForSwitch(clone $tokens); } - return \in_array($index, self::$cache[$codeHash], true); + return \in_array($index, self::$cache[$tokensHash], true); } /** diff --git a/tests/Fixtures/Integration/misc/ternary_operator_spaces,operator_linebreak.test b/tests/Fixtures/Integration/misc/ternary_operator_spaces,operator_linebreak.test new file mode 100644 index 00000000000..4e6798dc4de --- /dev/null +++ b/tests/Fixtures/Integration/misc/ternary_operator_spaces,operator_linebreak.test @@ -0,0 +1,25 @@ +--TEST-- +Integration of fixers: ternary_operator_spaces,operator_linebreak. +--RULESET-- +{"ternary_operator_spaces": true, "operator_linebreak": true} +--EXPECT-- +clearAt(2); + + $tokensWithoutEmpty = clone $tokensWithEmpty; + $tokensWithoutEmpty->clearEmptyTokens(); + + self::assertStringNotContainsString('to remove', $tokensWithEmpty->generateCode()); + self::assertStringNotContainsString('to remove', $tokensWithoutEmpty->generateCode()); + + self::assertSame( + $tokensWithEmpty->count(), + $tokensWithoutEmpty->count() + 1 + ); + + self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithEmpty, 12)); + self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithoutEmpty, 11)); + } }