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

FunctionToConstantFixer - bugfix for get_class with leading backslash

Kuba Werłos 7 лет назад
Родитель
Сommit
f24b90ca39

+ 7 - 0
src/Fixer/LanguageConstruct/FunctionToConstantFixer.php

@@ -159,6 +159,13 @@ final class FunctionToConstantFixer extends AbstractFixer implements Configurati
         $tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
         $tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
         $tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex);
         $tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex);
 
 
+        if ($replacementConst->isMagicConstant()) {
+            $prevIndex = $tokens->getPrevMeaningfulToken($index);
+            $prevToken = $tokens[$prevIndex];
+            if ($prevToken->isGivenKind(T_NS_SEPARATOR)) {
+                $tokens->clearAt($prevIndex);
+            }
+        }
         $tokens->clearAt($index);
         $tokens->clearAt($index);
         $tokens->insertAt($index, $replacementConst);
         $tokens->insertAt($index, $replacementConst);
     }
     }

+ 4 - 0
tests/Fixer/LanguageConstruct/FunctionToConstantFixerTest.php

@@ -181,6 +181,10 @@ $a =
                     }
                     }
                 ',
                 ',
             ],
             ],
+            'get_class with leading backslash' => [
+                '<?php __CLASS__;',
+                '<?php \get_class();',
+            ],
         ];
         ];
     }
     }