PHP_CodeSniffer_File::getTokensAsString PHP Method

getTokensAsString() public method

Returns the content of the tokens from the specified start position in the token stack for the specified length.
public getTokensAsString ( integer $start, integer $length ) : string
$start integer The position to start from in the token stack.
$length integer The length of tokens to traverse from the start pos.
return string The token contents.
    public function getTokensAsString($start, $length)
    {
        $str = '';
        $end = $start + $length;
        if ($end > $this->numTokens) {
            $end = $this->numTokens;
        }
        for ($i = $start; $i < $end; $i++) {
            $str .= $this->_tokens[$i]['content'];
        }
        return $str;
    }

Usage Example

 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @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)
 {
     if ($phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state[\'input\']' || $phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state["input"]') {
         $warning = 'Do not use the raw $form_state[\'input\'], use $form_state[\'values\'] instead where possible';
         $phpcsFile->addWarning($warning, $stackPtr, 'Input');
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getTokensAsString