Browse Source

bug #4073 IsNullFixer - fix function detection (kubawerlos)

This PR was squashed before being merged into the 2.12 branch (closes #4073).

Discussion
----------

IsNullFixer - fix function detection

Commits
-------

55e9195 IsNullFixer - fix function detection
SpacePossum 6 years ago
parent
commit
a6b0437630

+ 13 - 19
src/Fixer/LanguageConstruct/IsNullFixer.php

@@ -18,7 +18,7 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Tokenizer\CT;
+use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -73,6 +73,7 @@ final class IsNullFixer extends AbstractFixer implements ConfigurationDefinition
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         static $sequenceNeeded = [[T_STRING, 'is_null'], '('];
+        $functionsAnalyzer = new FunctionsAnalyzer();
 
         $currIndex = 0;
         while (null !== $currIndex) {
@@ -89,40 +90,33 @@ final class IsNullFixer extends AbstractFixer implements ConfigurationDefinition
             // move the cursor just after the sequence
             list($isNullIndex, $currIndex) = $matches;
 
-            $next = $tokens->getNextMeaningfulToken($currIndex);
-            if ($tokens[$next]->equals(')')) {
+            if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $matches[0])) {
                 continue;
             }
 
-            // skip all expressions which are not a function reference
-            $inversionCandidateIndex = $prevTokenIndex = $tokens->getPrevMeaningfulToken($matches[0]);
-            $prevToken = $tokens[$prevTokenIndex];
-            if ($prevToken->isGivenKind([T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION])) {
+            $next = $tokens->getNextMeaningfulToken($currIndex);
+            if ($tokens[$next]->equals(')')) {
                 continue;
             }
 
-            // handle function references with namespaces
-            if ($prevToken->isGivenKind(T_NS_SEPARATOR)) {
-                $inversionCandidateIndex = $twicePrevTokenIndex = $tokens->getPrevMeaningfulToken($prevTokenIndex);
-                /** @var Token $twicePrevToken */
-                $twicePrevToken = $tokens[$twicePrevTokenIndex];
-                if ($twicePrevToken->isGivenKind([T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION, T_STRING, CT::T_NAMESPACE_OPERATOR])) {
-                    continue;
-                }
+            $prevTokenIndex = $tokens->getPrevMeaningfulToken($matches[0]);
 
-                // get rid of the root namespace when it used and check if the inversion operator provided
+            // handle function references with namespaces
+            if ($tokens[$prevTokenIndex]->isGivenKind(T_NS_SEPARATOR)) {
                 $tokens->removeTrailingWhitespace($prevTokenIndex);
                 $tokens->clearAt($prevTokenIndex);
+
+                $prevTokenIndex = $tokens->getPrevMeaningfulToken($prevTokenIndex);
             }
 
             // check if inversion being used, text comparison is due to not existing constant
             $isInvertedNullCheck = false;
-            if ($tokens[$inversionCandidateIndex]->equals('!')) {
+            if ($tokens[$prevTokenIndex]->equals('!')) {
                 $isInvertedNullCheck = true;
 
                 // get rid of inverting for proper transformations
-                $tokens->removeTrailingWhitespace($inversionCandidateIndex);
-                $tokens->clearAt($inversionCandidateIndex);
+                $tokens->removeTrailingWhitespace($prevTokenIndex);
+                $tokens->clearAt($prevTokenIndex);
             }
 
             // before getting rind of `()` around a parameter, ensure it's not assignment/ternary invariant

+ 1 - 0
tests/Fixer/LanguageConstruct/IsNullFixerTest.php

@@ -107,6 +107,7 @@ FIXED;
 
             ['<?php is_nullSmth(json_decode($x));'],
             ['<?php smth_is_null(json_decode($x));'],
+            ['<?php namespace Foo; function &is_null($x) { return null === $x; }'],
 
             ['<?php "SELECT ... is_null(json_decode($x)) ...";'],
             ['<?php "SELECT ... is_null(json_decode($x)) ...";'],