Skip to content

Commit

Permalink
fix: work correctly for a switch/case with ternary operator (#7756)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Jan 17, 2024
1 parent 1d34bb2 commit 3c27597
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Fixer/ListNotation/ListSyntaxFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function getPriority(): int
{
return 1;
return 2;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Operator/TernaryOperatorSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function getPriority(): int
{
return 0;
return 1;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Operator/TernaryToElvisOperatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function getPriority(): int
{
return 1;
return 2;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
8 changes: 4 additions & 4 deletions src/Tokenizer/Analyzer/SwitchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Integration of fixers: ternary_operator_spaces,operator_linebreak.
--RULESET--
{"ternary_operator_spaces": true, "operator_linebreak": true}
--EXPECT--
<?php
switch ($foo) {
case 1:
case 2:
return 3 ? '4' : ''; // only change is space before ":" in this line
case 5:
case 6:
return 'more';
}

--INPUT--
<?php
switch ($foo) {
case 1:
case 2:
return 3 ? '4': ''; // only change is space before ":" in this line
case 5:
case 6:
return 'more';
}
25 changes: 25 additions & 0 deletions tests/Tokenizer/Analyzer/SwitchAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,29 @@ public static function provideColonCases(): iterable
[7, 12, 20, 30],
];
}

public function testWithEmptyTokens(): void
{
$tokensWithEmpty = Tokens::fromCode(<<<'PHP'
<?php
switch/* to remove */($value1) {
case 1: return 2;
}
PHP);
$tokensWithEmpty->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));
}
}

0 comments on commit 3c27597

Please sign in to comment.