PHP_CodeSniffer_File::findPrevious PHP Method

findPrevious() public method

If a value is specified, the previous token of the specified type(s) containing the specified value will be returned. Returns false if no token can be found.
See also: findNext()
public findPrevious ( integer | array $types, integer $start, integer $end = null, boolean $exclude = false, string $value = null, boolean $local = false ) : integer | boolean
$types integer | array The type(s) of tokens to search for.
$start integer The position to start searching from in the token stack.
$end integer The end position to fail if no token is found. if not specified or null, end will default to the start of the token stack.
$exclude boolean If true, find the previous token that are NOT of the types specified in $types.
$value string The value that the token(s) must be equal to. If value is omitted, tokens with any value will be returned.
$local boolean If true, tokens outside the current statement will not be checked. IE. checking will stop at the previous semi-colon found.
return integer | boolean
    public function findPrevious($types, $start, $end = null, $exclude = false, $value = null, $local = false)
    {
        $types = (array) $types;
        if ($end === null) {
            $end = 0;
        }
        for ($i = $start; $i >= $end; $i--) {
            $found = (bool) $exclude;
            foreach ($types as $type) {
                if ($this->_tokens[$i]['code'] === $type) {
                    $found = !$exclude;
                    break;
                }
            }
            if ($found === true) {
                if ($value === null) {
                    return $i;
                } else {
                    if ($this->_tokens[$i]['content'] === $value) {
                        return $i;
                    }
                }
            }
            if ($local === true) {
                if (isset($this->_tokens[$i]['scope_opener']) === true && $i === $this->_tokens[$i]['scope_closer']) {
                    $i = $this->_tokens[$i]['scope_opener'];
                } else {
                    if (isset($this->_tokens[$i]['bracket_opener']) === true && $i === $this->_tokens[$i]['bracket_closer']) {
                        $i = $this->_tokens[$i]['bracket_opener'];
                    } else {
                        if (isset($this->_tokens[$i]['parenthesis_opener']) === true && $i === $this->_tokens[$i]['parenthesis_closer']) {
                            $i = $this->_tokens[$i]['parenthesis_opener'];
                        } else {
                            if ($this->_tokens[$i]['code'] === T_SEMICOLON) {
                                break;
                            }
                        }
                    }
                }
            }
        }
        //end for
        return false;
    }

Usage Example

 public function process(CodeSnifferFile $file, $stackPtr)
 {
     $tokens = $file->getTokens();
     $fileName = $file->getFilename();
     // Collect use statement aliases
     if ($tokens[$stackPtr]['code'] === T_USE) {
         /** function () use ($var) {} */
         $previousPtr = $file->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
         if ($tokens[$previousPtr]['code'] === T_CLOSE_PARENTHESIS) {
             return;
         }
         /** use Trait; */
         if ($file->findPrevious([T_CLASS, T_TRAIT], $stackPtr)) {
             return;
         }
         list($stackPtr, $namespaceAlias, $fullyQualifiedNamespace) = $this->getNamespace($stackPtr + 1, $file);
         if (!isset(static::$aliases[$fileName])) {
             static::$aliases[$fileName] = [];
         }
         static::$aliases[$fileName][] = $namespaceAlias;
         return;
     }
     // Check if aliased exist for caught exceptions
     $catchPtr = $tokens[$stackPtr]['parenthesis_opener'] + 1;
     $exceptionPtr = $file->findNext([T_CLASS, T_INTERFACE], $catchPtr, $tokens[$stackPtr]['parenthesis_closer'], true);
     $exceptionName = $tokens[$exceptionPtr]['content'];
     if (!in_array($exceptionName, static::$aliases[$fileName], false)) {
         $file->addError(sprintf('Trying to catch an undefined exception. Please add use-statement for "%s"', $exceptionName), $exceptionPtr);
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::findPrevious