diff --git a/config/set/common/namespaces.php b/config/set/common/namespaces.php index d2b2b2301d..4afcee6dec 100644 --- a/config/set/common/namespaces.php +++ b/config/set/common/namespaces.php @@ -6,6 +6,7 @@ use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer; use PhpCsFixer\Fixer\Import\OrderedImportsFixer; use PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer; +use Symplify\CodingStandard\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; return ECSConfig::configure() @@ -14,4 +15,5 @@ OrderedImportsFixer::class, SingleBlankLineBeforeNamespaceFixer::class, NoUnneededImportAliasFixer::class, + RemoveSlashFromNativeConstantFixer::class, ]); diff --git a/packages/coding-standard/src/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer.php b/packages/coding-standard/src/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer.php new file mode 100644 index 0000000000..948360181a --- /dev/null +++ b/packages/coding-standard/src/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer.php @@ -0,0 +1,77 @@ + $tokens + */ + public function isCandidate(Tokens $tokens): bool + { + return $tokens->isTokenKindFound(T_NS_SEPARATOR); + } + + /** + * @param Tokens $tokens + */ + public function fix(SplFileInfo $fileInfo, Tokens $tokens): void + { + foreach ($tokens as $index => $token) { + if (! $token->isGivenKind(T_NS_SEPARATOR)) { + continue; + } + + $nextIndex = $tokens->getNextMeaningfulToken($index); + if ($nextIndex === null) { + continue; + } + + if (! $tokens[$nextIndex]->isGivenKind(T_STRING)) { + continue; + } + + if (! in_array(strtolower($tokens[$nextIndex]->getContent()), self::KEYWORDS, true)) { + continue; + } + + // the keyword must be a standalone name, not a namespace segment like "Foo\true" or "\true\Bar" + $prevIndex = $tokens->getPrevMeaningfulToken($index); + if ($prevIndex !== null && $tokens[$prevIndex]->isGivenKind([T_STRING, T_NS_SEPARATOR])) { + continue; + } + + $afterKeywordIndex = $tokens->getNextMeaningfulToken($nextIndex); + if ($afterKeywordIndex !== null && $tokens[$afterKeywordIndex]->isGivenKind(T_NS_SEPARATOR)) { + continue; + } + + $tokens->clearAt($index); + } + } +} diff --git a/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/fix_bool_and_null.php.inc b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/fix_bool_and_null.php.inc new file mode 100644 index 0000000000..a73778414b --- /dev/null +++ b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/fix_bool_and_null.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/keep_casing.php.inc b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/keep_casing.php.inc new file mode 100644 index 0000000000..8b891f1874 --- /dev/null +++ b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/keep_casing.php.inc @@ -0,0 +1,17 @@ + +----- + diff --git a/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/skip_namespace_segment.php.inc b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/skip_namespace_segment.php.inc new file mode 100644 index 0000000000..043dcc82e8 --- /dev/null +++ b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/Fixture/skip_namespace_segment.php.inc @@ -0,0 +1,10 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFiles(__DIR__ . '/Fixture'); + } + + public function provideConfig(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/config/configured_rule.php b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/config/configured_rule.php new file mode 100644 index 0000000000..f18f957be6 --- /dev/null +++ b/packages/coding-standard/tests/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(RemoveSlashFromNativeConstantFixer::class); +};