private function checkBitwiseOperation() { if (!NodeUtil::isEqualityExpression($this->stream->node->left)) { return; } /* * When this method is called, the token stream points to the bitwise operation already. * * 0 === foo() & 5; * ^ * -----------(1) * * (1) The position of the token stream when this method is called. * * We now check that the equality expression on the left side (in the case above, it is an Identical node) ends * with a parenthesis, and the ending parenthesis is started somewhere on the left side side of the equality * operator (===). */ /** @var AbstractToken $closingParenthesis */ $closingParenthesis = $this->stream->token->findPreviousToken('NO_WHITESPACE_OR_COMMENT')->get(); if ($closingParenthesis->matches(')')) { $startToken = $this->stream->node->left->left->getAttribute('start_token'); // The left operator of the equality sign is not assigned an explicit start token, so we do not know where // it started. We can just bail out here, and hope that the closing parenthesis is indeed started on the left // side. This needs to be improved in the SimultaneousTokenAndAstStream over time so that we ideally always // have a start token available. if (null === $startToken) { return; } // The closing parenthesis was started somewhere on the left side, good ! if ($closingParenthesis->isClosing($startToken->findPreviousToken('NO_WHITESPACE_OR_COMMENT')->get())) { return; } } $this->phpFile->addComment($this->stream->node->getLine(), Comment::warning('precedence.possibly_wrong_comparison_of_bitop_result', 'Consider adding parentheses for clarity. Current Interpretation: ``%current_compare%``, Probably Intended Meaning: ``%alternative_compare%``', array('current_compare' => '(' . self::$prettyPrinter->prettyPrintExpr($this->stream->node->left) . ') ' . NodeUtil::getBitOp($this->stream->node) . ' ' . self::$prettyPrinter->prettyPrintExpr($this->stream->node->right), 'alternative_compare' => self::$prettyPrinter->prettyPrintExpr($this->stream->node->left->left) . ' ' . NodeUtil::getEqualOp($this->stream->node->left) . ' (' . self::$prettyPrinter->prettyPrintExpr($this->stream->node->left->right) . ' ' . NodeUtil::getBitOp($this->stream->node) . ' ' . self::$prettyPrinter->prettyPrintExpr($this->stream->node->right) . ')'))); }