Skip to content

Commit

Permalink
fix: ConstantCaseFixer - do not touch typed constants (#7998)
Browse files Browse the repository at this point in the history
Co-authored-by: Greg Korba <greg@codito.dev>
  • Loading branch information
kubawerlos and Wirone committed May 9, 2024
1 parent efe7a39 commit c87c87a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Fixer/Casing/ConstantCaseFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$nextIndex = $tokens->getNextMeaningfulToken($index);

if (
$this->isNeighbourAccepted($tokens, $tokens->getPrevMeaningfulToken($index))
&& $this->isNeighbourAccepted($tokens, $tokens->getNextMeaningfulToken($index))
&& $this->isNeighbourAccepted($tokens, $nextIndex)
&& !$tokens[$nextIndex]->equals('=')
&& !$this->isEnumCaseName($tokens, $index)
) {
$tokens[$index] = new Token([$token->getId(), $fixFunction($token->getContent())]);
Expand All @@ -106,7 +109,6 @@ private function isNeighbourAccepted(Tokens $tokens, int $index): bool
$forbiddenTokens = [
T_AS,
T_CLASS,
T_CONST,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
Expand Down
45 changes: 45 additions & 0 deletions tests/Fixer/Casing/ConstantCaseFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,49 @@ enum Foo: string { case FALSE = "false"; }
',
];
}

/**
* @dataProvider provideFix83Cases
*
* @requires PHP 8.3
*/
public function testFix83(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

/**
* @return iterable<array{string, string}>
*/
public static function provideFix83Cases(): iterable
{
yield 'typed constant' => [
<<<'PHP'
<?php
class Foo1 { const array NULL = []; }
class Foo2 { const int NULL = 0; }
class Foo3 { const mixed NULL = 0; }
class Foo4 { const string NULL = ''; }
class Foo5 { const Foo|null NULL = null; }
class Foo6 { const null|Foo NULL = null; }
class Foo7 { const null|(Foo&Bar) NULL = null; }
class Foo8 { const bool TRUE = true; }
class Foo9 { const false FALSE = false; }
class Foo10 { const true TRUE = true; }
PHP,
<<<'PHP'
<?php
class Foo1 { const array NULL = []; }
class Foo2 { const int NULL = 0; }
class Foo3 { const mixed NULL = 0; }
class Foo4 { const string NULL = ''; }
class Foo5 { const Foo|NULL NULL = NULL; }
class Foo6 { const NULL|Foo NULL = NULL; }
class Foo7 { const NULL|(Foo&Bar) NULL = NULL; }
class Foo8 { const bool TRUE = TRUE; }
class Foo9 { const FALSE FALSE = FALSE; }
class Foo10 { const TRUE TRUE = TRUE; }
PHP,
];
}
}

0 comments on commit c87c87a

Please sign in to comment.