Skip to content

Commit

Permalink
Add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed May 11, 2024
1 parent 11f2cf9 commit 8b446a6
Showing 1 changed file with 20 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;

Check warning on line 103 in src/Fixer/Casing/ConstantCaseFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ } $prevIndex = $tokens->getPrevMeaningfulToken($index); if ($tokens[$prevIndex]->isGivenKind($forbiddenPrevKinds)) { - continue; + break; } $nextIndex = $tokens->getNextMeaningfulToken($index); if ($tokens[$nextIndex]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM) || $tokens[$nextIndex]->equalsAny(['='], false)) {
}

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

Check warning on line 107 in src/Fixer/Casing/ConstantCaseFixer.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 mutation tests

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ continue; } $nextIndex = $tokens->getNextMeaningfulToken($index); - if ($tokens[$nextIndex]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM) || $tokens[$nextIndex]->equalsAny(['='], false)) { + if ($tokens[$nextIndex]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM) || $tokens[$nextIndex]->equalsAny(['='], true)) { continue; } if ($tokens[$prevIndex]->isGivenKind(T_CASE) && $tokens[$nextIndex]->equals(';')) {
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);
}
}

0 comments on commit 8b446a6

Please sign in to comment.