Просмотр исходного кода

feat(EXPERIMENTAL): ClassKeywordFixer, part 2 (#7550)

Dariusz Rumiński 1 год назад
Родитель
Сommit
698a9098d9

+ 1 - 1
doc/rules/index.rst

@@ -460,7 +460,7 @@ Import
 Language Construct
 ------------------
 
-- `class_keyword <./language_construct/class_keyword.rst>`_
+- `class_keyword <./language_construct/class_keyword.rst>`_ *(risky)*
 
   EXPERIMENTAL: Converts FQCN strings to ``*::class`` keywords. Do not use it, unless you know what you are doing.
 - `class_keyword_remove <./language_construct/class_keyword_remove.rst>`_ *(deprecated)*

+ 16 - 0
doc/rules/language_construct/class_keyword.rst

@@ -5,6 +5,22 @@ Rule ``class_keyword``
 EXPERIMENTAL: Converts FQCN strings to ``*::class`` keywords. Do not use it,
 unless you know what you are doing.
 
+Description
+-----------
+
+This rule does not have an understanding of whether a class exists in the scope
+of the codebase or not, relying on run-time and autoloaded classes to determine
+it, which makes the rule useless when running on a single file out of codebase
+context.
+
+Warning
+-------
+
+Using this rule is risky
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+Risky as EXPERIMENTAL.
+
 Examples
 --------
 

+ 14 - 12
src/Fixer/LanguageConstruct/ClassKeywordFixer.php

@@ -37,7 +37,9 @@ $foo = \'PhpCsFixer\Tokenizer\Tokens\';
 $bar = "\PhpCsFixer\Tokenizer\Tokens";
 '
                 ),
-            ]
+            ],
+            'This rule does not have an understanding of whether a class exists in the scope of the codebase or not, relying on run-time and autoloaded classes to determine it, which makes the rule useless when running on a single file out of codebase context.',
+            'Risky as EXPERIMENTAL.'
         );
     }
 
@@ -46,6 +48,11 @@ $bar = "\PhpCsFixer\Tokenizer\Tokens";
         return true;
     }
 
+    public function isRisky(): bool
+    {
+        return true;
+    }
+
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         for ($index = $tokens->count() - 1; $index >= 0; --$index) {
@@ -57,18 +64,13 @@ $bar = "\PhpCsFixer\Tokenizer\Tokens";
                 $name = str_replace('\\\\', '\\', $name);
 
                 if ($this->exists($name)) {
-                    try {
-                        $substitution = Tokens::fromCode("<?php echo \\{$name}::class;");
-                        $substitution->clearRange(0, 2);
-                        $substitution->clearAt($substitution->getSize() - 1);
-                        $substitution->clearEmptyTokens();
+                    $substitution = Tokens::fromCode("<?php echo \\{$name}::class;");
+                    $substitution->clearRange(0, 2);
+                    $substitution->clearAt($substitution->getSize() - 1);
+                    $substitution->clearEmptyTokens();
 
-                        $tokens->clearAt($index);
-                        $tokens->insertAt($index, $substitution);
-                    } catch (\Error $e) {
-                        var_dump('error with parsing class', $name);
-                        var_dump($e->getMessage());
-                    }
+                    $tokens->clearAt($index);
+                    $tokens->insertAt($index, $substitution);
                 }
             }
         }