Skip to content

Commit

Permalink
fix: ConstantCaseFixer - do not change namespace (#8004)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed May 26, 2024
1 parent 8eaa894 commit efa07e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
79 changes: 20 additions & 59 deletions src/Fixer/Casing/ConstantCaseFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -81,77 +80,39 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$fixFunction = $this->fixFunction;

foreach ($tokens as $index => $token) {
if (!$token->isNativeConstant()) {
continue;
}

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

if (
$this->isNeighbourAccepted($tokens, $tokens->getPrevMeaningfulToken($index))
&& $this->isNeighbourAccepted($tokens, $nextIndex)
&& !$tokens[$nextIndex]->equals('=')
&& !$this->isEnumCaseName($tokens, $index)
) {
$tokens[$index] = new Token([$token->getId(), $fixFunction($token->getContent())]);
}
}
}

private function isNeighbourAccepted(Tokens $tokens, int $index): bool
{
static $forbiddenTokens = null;

if (null === $forbiddenTokens) {
$forbiddenTokens = [
T_AS,
T_CLASS,
static $forbiddenPrevKinds = null;
if (null === $forbiddenPrevKinds) {
$forbiddenPrevKinds = [
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_INTERFACE,
T_NAMESPACE,
T_NEW,
T_NS_SEPARATOR,
T_PAAMAYIM_NEKUDOTAYIM,
T_TRAIT,
T_USE,
CT::T_USE_TRAIT,
CT::T_USE_LAMBDA,
...Token::getObjectOperatorKinds(),
];
}

$token = $tokens[$index];

if ($token->equalsAny(['{', '}'])) {
return false;
}

return !$token->isGivenKind($forbiddenTokens);
}
foreach ($tokens as $index => $token) {
if (!$token->equalsAny([[T_STRING, 'true'], [T_STRING, 'false'], [T_STRING, 'null']], false)) {
continue;
}

private function isEnumCaseName(Tokens $tokens, int $index): bool
{
if (!\defined('T_ENUM') || !$tokens->isTokenKindFound(T_ENUM)) { // @TODO: drop condition when PHP 8.1+ is required
return false;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind($forbiddenPrevKinds)) {
continue;
}

$prevIndex = $tokens->getPrevMeaningfulToken($index);
$nextIndex = $tokens->getNextMeaningfulToken($index);
if ($tokens[$nextIndex]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM) || $tokens[$nextIndex]->equalsAny(['='], false)) {
continue;
}

if (null === $prevIndex || !$tokens[$prevIndex]->isGivenKind(T_CASE)) {
return false;
}
if ($tokens[$prevIndex]->isGivenKind(T_CASE) && $tokens[$nextIndex]->equals(';')) {
continue;
}

if (!$tokens->isTokenKindFound(T_SWITCH)) {
return true;
$tokens[$index] = new Token([$token->getId(), ($this->fixFunction)($token->getContent())]);
}

$prevIndex = $tokens->getPrevTokenOfKind($prevIndex, [[T_ENUM], [T_SWITCH]]);

return null !== $prevIndex && $tokens[$prevIndex]->isGivenKind(T_ENUM);
}
}
2 changes: 2 additions & 0 deletions tests/Fixer/Casing/ConstantCaseFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static function provideFixCases(): iterable

yield ['<?php $x = False::foo();'];

yield ['<?php namespace Null;'];

yield ['<?php namespace Foo\Null;'];

yield ['<?php class Foo extends True {}'];
Expand Down

0 comments on commit efa07e3

Please sign in to comment.