PHP_CodeSniffer_File::isReference PHP Method

isReference() public method

Returns true if the specified token position represents a reference. Returns false if the token represents a bitwise operator.
public isReference ( integer $stackPtr ) : boolean
$stackPtr integer The position of the T_BITWISE_AND token.
return boolean
    public function isReference($stackPtr)
    {
        if ($this->_tokens[$stackPtr]['code'] !== T_BITWISE_AND) {
            return false;
        }
        $tokenBefore = $this->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr - 1, null, true);
        if ($this->_tokens[$tokenBefore]['code'] === T_FUNCTION) {
            // Function returns a reference.
            return true;
        }
        if ($this->_tokens[$tokenBefore]['code'] === T_DOUBLE_ARROW) {
            // Inside a foreach loop, this is a reference.
            return true;
        }
        if ($this->_tokens[$tokenBefore]['code'] === T_AS) {
            // Inside a foreach loop, this is a reference.
            return true;
        }
        if ($this->_tokens[$tokenBefore]['code'] === T_OPEN_SHORT_ARRAY) {
            // Inside an array declaration, this is a reference.
            return true;
        }
        if (isset(PHP_CodeSniffer_Tokens::$assignmentTokens[$this->_tokens[$tokenBefore]['code']]) === true) {
            // This is directly after an assignment. It's a reference. Even if
            // it is part of an operation, the other tests will handle it.
            return true;
        }
        if (isset($this->_tokens[$stackPtr]['nested_parenthesis']) === true) {
            $brackets = $this->_tokens[$stackPtr]['nested_parenthesis'];
            $lastBracket = array_pop($brackets);
            if (isset($this->_tokens[$lastBracket]['parenthesis_owner']) === true) {
                $owner = $this->_tokens[$this->_tokens[$lastBracket]['parenthesis_owner']];
                if ($owner['code'] === T_FUNCTION || $owner['code'] === T_CLOSURE || $owner['code'] === T_ARRAY) {
                    // Inside a function or array declaration, this is a reference.
                    return true;
                }
            } else {
                $prev = false;
                for ($t = $this->_tokens[$lastBracket]['parenthesis_opener'] - 1; $t >= 0; $t--) {
                    if ($this->_tokens[$t]['code'] !== T_WHITESPACE) {
                        $prev = $t;
                        break;
                    }
                }
                if ($prev !== false && $this->_tokens[$prev]['code'] === T_USE) {
                    return true;
                }
            }
            //end if
        }
        //end if
        $tokenAfter = $this->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true);
        if ($this->_tokens[$tokenAfter]['code'] === T_VARIABLE && ($this->_tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS || $this->_tokens[$tokenBefore]['code'] === T_COMMA)) {
            return true;
        }
        return false;
    }

Usage Example

 /**
  * Processes the tokens that this sniff is interested in.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
  * @param int                  $stackPtr  The position in the stack where
  *                                        the token was found.
  *
  * @return void
  * @see PHP_CodeSniffer_Sniff::process()
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     // Ignore normal references
     if (true === $phpcsFile->isReference($stackPtr)) {
         return;
     }
     // Ignore all but variables
     $tokens = $phpcsFile->getTokens();
     $token = $tokens[$stackPtr + 1];
     if (T_VARIABLE !== $token['code']) {
         return;
     }
     $warning = sprintf('Use of deprecated runtime references for variable "%s"!', $token['content']);
     $phpcsFile->addWarning($warning, $stackPtr);
 }
All Usage Examples Of PHP_CodeSniffer_File::isReference