isBinaryOperator() public method

Checks if there is a binary operator under given index.
public isBinaryOperator ( integer $index ) : boolean
$index integer
return boolean
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if (!$tokensAnalyzer->isBinaryOperator($index)) {
             continue;
         }
         if (!$tokens[$index + 1]->isWhitespace()) {
             $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
         }
         if (!$tokens[$index - 1]->isWhitespace()) {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     // last and first tokens cannot be an operator
     for ($index = $tokens->count() - 2; $index >= 0; --$index) {
         if (!$tokensAnalyzer->isBinaryOperator($index)) {
             continue;
         }
         $isDeclare = $this->isDeclareStatement($tokens, $index);
         if (false !== $isDeclare) {
             $index = $isDeclare;
             // skip `declare(foo ==bar)`, see `declare_equal_normalize`
         } else {
             $this->fixWhiteSpaceAroundOperator($tokens, $index);
         }
         // previous of binary operator is now never an operator / previous of declare statement cannot be an operator
         --$index;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if (!$tokensAnalyzer->isBinaryOperator($index)) {
             continue;
         }
         // skip `declare(foo ==bar)`
         $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($index);
         if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_STRING)) {
             $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
             if ($tokens[$prevMeaningfulIndex]->equals('(')) {
                 $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
                 if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_DECLARE)) {
                     continue;
                 }
             }
         }
         // fix white space after operator
         if ($tokens[$index + 1]->isWhitespace()) {
             $content = $tokens[$index + 1]->getContent();
             if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
                 $tokens[$index + 1]->setContent(' ');
             }
         } else {
             $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
         }
         // fix white space before operator
         if ($tokens[$index - 1]->isWhitespace()) {
             $content = $tokens[$index - 1]->getContent();
             if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
                 $tokens[$index - 1]->setContent(' ');
             }
         } else {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
         }
         --$index;
         // skip check for binary operator on the whitespace token that is fixed.
     }
 }
Ejemplo n.º 4
0
 /**
  * @dataProvider provideIsBinaryOperator70
  * @requires PHP 7.0
  */
 public function testIsBinaryOperator70($source, array $expected)
 {
     $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
     foreach ($expected as $index => $isBinary) {
         $this->assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
         if ($isBinary) {
             $this->assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
             $this->assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
         }
     }
 }