PHP_CodeSniffer_File::findFirstOnLine PHP Method

findFirstOnLine() public method

Returns false if no token can be found.
public findFirstOnLine ( integer | array $types, integer $start, boolean $exclude = false, string $value = null ) : 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. The first token matching on this line before this token will be returned.
$exclude boolean If true, find the token that is NOT of the types specified in $types.
$value string The value that the token must be equal to. If value is omitted, tokens with any value will be returned.
return integer | boolean | bool
    public function findFirstOnLine($types, $start, $exclude = false, $value = null)
    {
        if (is_array($types) === false) {
            $types = array($types);
        }
        $foundToken = false;
        for ($i = $start; $i >= 0; $i--) {
            if ($this->_tokens[$i]['line'] < $this->_tokens[$start]['line']) {
                break;
            }
            $found = $exclude;
            foreach ($types as $type) {
                if ($exclude === false) {
                    if ($this->_tokens[$i]['code'] === $type) {
                        $found = true;
                        break;
                    }
                } else {
                    if ($this->_tokens[$i]['code'] === $type) {
                        $found = false;
                        break;
                    }
                }
            }
            if ($found === true) {
                if ($value === null) {
                    $foundToken = $i;
                } else {
                    if ($this->_tokens[$i]['content'] === $value) {
                        $foundToken = $i;
                    }
                }
            }
        }
        //end for
        return $foundToken;
    }

Usage Example

 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
  * @param int                  $stackPtr  The position of the current token in the
  *                                        stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // If this is an inline condition (ie. there is no scope opener), then
     // return, as this is not a new scope.
     if (isset($tokens[$stackPtr]['scope_closer']) === false) {
         return;
     }
     // We need to actually find the first piece of content on this line,
     // as if this is a method with tokens before it (public, static etc)
     // or an if with an else before it, then we need to start the scope
     // checking from there, rather than the current token.
     $lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
     $startColumn = $tokens[$lineStart]['column'];
     $scopeStart = $tokens[$stackPtr]['scope_opener'];
     $scopeEnd = $tokens[$stackPtr]['scope_closer'];
     // Check that the closing brace is on it's own line.
     $lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_INLINE_HTML, T_OPEN_TAG), $scopeEnd - 1, $scopeStart, true);
     if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
         $error = 'Closing brace must be on a line by itself';
         $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
         if ($fix === true) {
             $phpcsFile->fixer->addNewlineBefore($scopeEnd);
         }
         return;
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::findFirstOnLine