Chris@0: Chris@0: * @author Marc McIntyre Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Tokenizes JS code. Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @version Release: @package_version@ Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: class PHP_CodeSniffer_Tokenizers_JS Chris@0: { Chris@0: Chris@0: /** Chris@0: * If TRUE, files that appear to be minified will not be processed. Chris@0: * Chris@0: * @var boolean Chris@0: */ Chris@0: public $skipMinified = true; Chris@0: Chris@0: /** Chris@0: * A list of tokens that are allowed to open a scope. Chris@0: * Chris@0: * This array also contains information about what kind of token the scope Chris@0: * opener uses to open and close the scope, if the token strictly requires Chris@0: * an opener, if the token can share a scope closer, and who it can be shared Chris@0: * with. An example of a token that shares a scope closer is a CASE scope. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public $scopeOpeners = array( Chris@0: T_IF => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => false, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_TRY => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => true, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_CATCH => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => true, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_ELSE => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => false, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_FOR => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => false, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_FUNCTION => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => false, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_WHILE => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => false, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_DO => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => true, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_SWITCH => array( Chris@0: 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET), Chris@0: 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET), Chris@0: 'strict' => true, Chris@0: 'shared' => false, Chris@0: 'with' => array(), Chris@0: ), Chris@0: T_CASE => array( Chris@0: 'start' => array(T_COLON => T_COLON), Chris@0: 'end' => array( Chris@0: T_BREAK => T_BREAK, Chris@0: T_RETURN => T_RETURN, Chris@0: T_CONTINUE => T_CONTINUE, Chris@0: T_THROW => T_THROW, Chris@0: ), Chris@0: 'strict' => true, Chris@0: 'shared' => true, Chris@0: 'with' => array( Chris@0: T_DEFAULT => T_DEFAULT, Chris@0: T_CASE => T_CASE, Chris@0: T_SWITCH => T_SWITCH, Chris@0: ), Chris@0: ), Chris@0: T_DEFAULT => array( Chris@0: 'start' => array(T_COLON => T_COLON), Chris@0: 'end' => array( Chris@0: T_BREAK => T_BREAK, Chris@0: T_RETURN => T_RETURN, Chris@0: T_CONTINUE => T_CONTINUE, Chris@0: T_THROW => T_THROW, Chris@0: ), Chris@0: 'strict' => true, Chris@0: 'shared' => true, Chris@0: 'with' => array( Chris@0: T_CASE => T_CASE, Chris@0: T_SWITCH => T_SWITCH, Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: /** Chris@0: * A list of tokens that end the scope. Chris@0: * Chris@0: * This array is just a unique collection of the end tokens Chris@0: * from the _scopeOpeners array. The data is duplicated here to Chris@0: * save time during parsing of the file. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public $endScopeTokens = array( Chris@0: T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET, Chris@0: T_BREAK => T_BREAK, Chris@0: ); Chris@0: Chris@0: /** Chris@0: * A list of special JS tokens and their types. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $tokenValues = array( Chris@0: 'function' => 'T_FUNCTION', Chris@0: 'prototype' => 'T_PROTOTYPE', Chris@0: 'try' => 'T_TRY', Chris@0: 'catch' => 'T_CATCH', Chris@0: 'return' => 'T_RETURN', Chris@0: 'throw' => 'T_THROW', Chris@0: 'break' => 'T_BREAK', Chris@0: 'switch' => 'T_SWITCH', Chris@0: 'continue' => 'T_CONTINUE', Chris@0: 'if' => 'T_IF', Chris@0: 'else' => 'T_ELSE', Chris@0: 'do' => 'T_DO', Chris@0: 'while' => 'T_WHILE', Chris@0: 'for' => 'T_FOR', Chris@0: 'var' => 'T_VAR', Chris@0: 'case' => 'T_CASE', Chris@0: 'default' => 'T_DEFAULT', Chris@0: 'true' => 'T_TRUE', Chris@0: 'false' => 'T_FALSE', Chris@0: 'null' => 'T_NULL', Chris@0: 'this' => 'T_THIS', Chris@0: 'typeof' => 'T_TYPEOF', Chris@0: '(' => 'T_OPEN_PARENTHESIS', Chris@0: ')' => 'T_CLOSE_PARENTHESIS', Chris@0: '{' => 'T_OPEN_CURLY_BRACKET', Chris@0: '}' => 'T_CLOSE_CURLY_BRACKET', Chris@0: '[' => 'T_OPEN_SQUARE_BRACKET', Chris@0: ']' => 'T_CLOSE_SQUARE_BRACKET', Chris@0: '?' => 'T_INLINE_THEN', Chris@0: '.' => 'T_OBJECT_OPERATOR', Chris@0: '+' => 'T_PLUS', Chris@0: '-' => 'T_MINUS', Chris@0: '*' => 'T_MULTIPLY', Chris@0: '%' => 'T_MODULUS', Chris@0: '/' => 'T_DIVIDE', Chris@0: '^' => 'T_LOGICAL_XOR', Chris@0: ',' => 'T_COMMA', Chris@0: ';' => 'T_SEMICOLON', Chris@0: ':' => 'T_COLON', Chris@0: '<' => 'T_LESS_THAN', Chris@0: '>' => 'T_GREATER_THAN', Chris@0: '<<' => 'T_SL', Chris@0: '>>' => 'T_SR', Chris@0: '>>>' => 'T_ZSR', Chris@0: '<<=' => 'T_SL_EQUAL', Chris@0: '>>=' => 'T_SR_EQUAL', Chris@0: '>>>=' => 'T_ZSR_EQUAL', Chris@0: '<=' => 'T_IS_SMALLER_OR_EQUAL', Chris@0: '>=' => 'T_IS_GREATER_OR_EQUAL', Chris@0: '=>' => 'T_DOUBLE_ARROW', Chris@0: '!' => 'T_BOOLEAN_NOT', Chris@0: '||' => 'T_BOOLEAN_OR', Chris@0: '&&' => 'T_BOOLEAN_AND', Chris@0: '|' => 'T_BITWISE_OR', Chris@0: '&' => 'T_BITWISE_AND', Chris@0: '!=' => 'T_IS_NOT_EQUAL', Chris@0: '!==' => 'T_IS_NOT_IDENTICAL', Chris@0: '=' => 'T_EQUAL', Chris@0: '==' => 'T_IS_EQUAL', Chris@0: '===' => 'T_IS_IDENTICAL', Chris@0: '-=' => 'T_MINUS_EQUAL', Chris@0: '+=' => 'T_PLUS_EQUAL', Chris@0: '*=' => 'T_MUL_EQUAL', Chris@0: '/=' => 'T_DIV_EQUAL', Chris@0: '%=' => 'T_MOD_EQUAL', Chris@0: '++' => 'T_INC', Chris@0: '--' => 'T_DEC', Chris@0: '//' => 'T_COMMENT', Chris@0: '/*' => 'T_COMMENT', Chris@0: '/**' => 'T_DOC_COMMENT', Chris@0: '*/' => 'T_COMMENT', Chris@0: ); Chris@0: Chris@0: /** Chris@0: * A list string delimiters. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $stringTokens = array( Chris@0: '\'' => '\'', Chris@0: '"' => '"', Chris@0: ); Chris@0: Chris@0: /** Chris@0: * A list tokens that start and end comments. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $commentTokens = array( Chris@0: '//' => null, Chris@0: '/*' => '*/', Chris@0: '/**' => '*/', Chris@0: ); Chris@0: Chris@0: Chris@0: /** Chris@0: * Creates an array of tokens when given some JS code. Chris@0: * Chris@0: * @param string $string The string to tokenize. Chris@0: * @param string $eolChar The EOL character to use for splitting strings. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function tokenizeString($string, $eolChar='\n') Chris@0: { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t*** START JS TOKENIZING ***".PHP_EOL; Chris@0: } Chris@0: Chris@0: $maxTokenLength = 0; Chris@0: foreach ($this->tokenValues as $token => $values) { Chris@0: if (strlen($token) > $maxTokenLength) { Chris@0: $maxTokenLength = strlen($token); Chris@0: } Chris@0: } Chris@0: Chris@0: $tokens = array(); Chris@0: $inString = ''; Chris@0: $stringChar = null; Chris@0: $inComment = ''; Chris@0: $buffer = ''; Chris@0: $preStringBuffer = ''; Chris@0: $cleanBuffer = false; Chris@0: Chris@0: $commentTokenizer = new PHP_CodeSniffer_Tokenizers_Comment(); Chris@0: Chris@0: $tokens[] = array( Chris@0: 'code' => T_OPEN_TAG, Chris@0: 'type' => 'T_OPEN_TAG', Chris@0: 'content' => '', Chris@0: ); Chris@0: Chris@0: // Convert newlines to single characters for ease of Chris@0: // processing. We will change them back later. Chris@0: $string = str_replace($eolChar, "\n", $string); Chris@0: Chris@0: $chars = str_split($string); Chris@0: $numChars = count($chars); Chris@0: for ($i = 0; $i < $numChars; $i++) { Chris@0: $char = $chars[$i]; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($char); Chris@0: $bufferContent = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: Chris@0: if ($inString !== '') { Chris@0: echo "\t"; Chris@0: } Chris@0: Chris@0: if ($inComment !== '') { Chris@0: echo "\t"; Chris@0: } Chris@0: Chris@0: echo "\tProcess char $i => $content (buffer: $bufferContent)".PHP_EOL; Chris@0: }//end if Chris@0: Chris@0: if ($inString === '' && $inComment === '' && $buffer !== '') { Chris@0: // If the buffer only has whitespace and we are about to Chris@0: // add a character, store the whitespace first. Chris@0: if (trim($char) !== '' && trim($buffer) === '') { Chris@0: $tokens[] = array( Chris@0: 'code' => T_WHITESPACE, Chris@0: 'type' => 'T_WHITESPACE', Chris@0: 'content' => str_replace("\n", $eolChar, $buffer), Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $buffer = ''; Chris@0: } Chris@0: Chris@0: // If the buffer is not whitespace and we are about to Chris@0: // add a whitespace character, store the content first. Chris@0: if ($inString === '' Chris@0: && $inComment === '' Chris@0: && trim($char) === '' Chris@0: && trim($buffer) !== '' Chris@0: ) { Chris@0: $tokens[] = array( Chris@0: 'code' => T_STRING, Chris@0: 'type' => 'T_STRING', Chris@0: 'content' => str_replace("\n", $eolChar, $buffer), Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: echo "\t=> Added token T_STRING ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $buffer = ''; Chris@0: } Chris@0: }//end if Chris@0: Chris@0: // Process strings. Chris@0: if ($inComment === '' && isset($this->stringTokens[$char]) === true) { Chris@0: if ($inString === $char) { Chris@0: // This could be the end of the string, but make sure it Chris@0: // is not escaped first. Chris@0: $escapes = 0; Chris@0: for ($x = ($i - 1); $x >= 0; $x--) { Chris@0: if ($chars[$x] !== '\\') { Chris@0: break; Chris@0: } Chris@0: Chris@0: $escapes++; Chris@0: } Chris@0: Chris@0: if ($escapes === 0 || ($escapes % 2) === 0) { Chris@0: // There is an even number escape chars, Chris@0: // so this is not escaped, it is the end of the string. Chris@0: $tokens[] = array( Chris@0: 'code' => T_CONSTANT_ENCAPSED_STRING, Chris@0: 'type' => 'T_CONSTANT_ENCAPSED_STRING', Chris@0: 'content' => str_replace("\n", $eolChar, $buffer).$char, Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* found end of string *".PHP_EOL; Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer.$char); Chris@0: echo "\t=> Added token T_CONSTANT_ENCAPSED_STRING ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $buffer = ''; Chris@0: $preStringBuffer = ''; Chris@0: $inString = ''; Chris@0: $stringChar = null; Chris@0: continue; Chris@0: }//end if Chris@0: } else if ($inString === '') { Chris@0: $inString = $char; Chris@0: $stringChar = $i; Chris@0: $preStringBuffer = $buffer; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* looking for string closer *".PHP_EOL; Chris@0: } Chris@0: }//end if Chris@0: }//end if Chris@0: Chris@0: if ($inString !== '' && $char === "\n") { Chris@0: // Unless this newline character is escaped, the string did not Chris@0: // end before the end of the line, which means it probably Chris@0: // wasn't a string at all (maybe a regex). Chris@0: if ($chars[($i - 1)] !== '\\') { Chris@0: $i = $stringChar; Chris@0: $buffer = $preStringBuffer; Chris@0: $preStringBuffer = ''; Chris@0: $inString = ''; Chris@0: $stringChar = null; Chris@0: $char = $chars[$i]; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* found newline before end of string, bailing *".PHP_EOL; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $buffer .= $char; Chris@0: Chris@0: // We don't look for special tokens inside strings, Chris@0: // so if we are in a string, we can continue here now Chris@0: // that the current char is in the buffer. Chris@0: if ($inString !== '') { Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Special case for T_DIVIDE which can actually be Chris@0: // the start of a regular expression. Chris@0: if ($buffer === $char && $char === '/' && $chars[($i + 1)] !== '*') { Chris@0: $regex = $this->getRegexToken( Chris@0: $i, Chris@0: $string, Chris@0: $chars, Chris@0: $tokens, Chris@0: $eolChar Chris@0: ); Chris@0: Chris@0: if ($regex !== null) { Chris@0: $tokens[] = array( Chris@0: 'code' => T_REGULAR_EXPRESSION, Chris@0: 'type' => 'T_REGULAR_EXPRESSION', Chris@0: 'content' => $regex['content'], Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($regex['content']); Chris@0: echo "\t=> Added token T_REGULAR_EXPRESSION ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $i = $regex['end']; Chris@0: $buffer = ''; Chris@0: $cleanBuffer = false; Chris@0: continue; Chris@0: }//end if Chris@0: }//end if Chris@0: Chris@0: // Check for known tokens, but ignore tokens found that are not at Chris@0: // the end of a string, like FOR and this.FORmat. Chris@0: if (isset($this->tokenValues[strtolower($buffer)]) === true Chris@0: && (preg_match('|[a-zA-z0-9_]|', $char) === 0 Chris@0: || isset($chars[($i + 1)]) === false Chris@0: || preg_match('|[a-zA-z0-9_]|', $chars[($i + 1)]) === 0) Chris@0: ) { Chris@0: $matchedToken = false; Chris@0: $lookAheadLength = ($maxTokenLength - strlen($buffer)); Chris@0: Chris@0: if ($lookAheadLength > 0) { Chris@0: // The buffer contains a token type, but we need Chris@0: // to look ahead at the next chars to see if this is Chris@0: // actually part of a larger token. For example, Chris@0: // FOR and FOREACH. Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* buffer possibly contains token, looking ahead $lookAheadLength chars *".PHP_EOL; Chris@0: } Chris@0: Chris@0: $charBuffer = $buffer; Chris@0: for ($x = 1; $x <= $lookAheadLength; $x++) { Chris@0: if (isset($chars[($i + $x)]) === false) { Chris@0: break; Chris@0: } Chris@0: Chris@0: $charBuffer .= $chars[($i + $x)]; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($charBuffer); Chris@0: echo "\t\t=> Looking ahead $x chars => $content".PHP_EOL; Chris@0: } Chris@0: Chris@0: if (isset($this->tokenValues[strtolower($charBuffer)]) === true) { Chris@0: // We've found something larger that matches Chris@0: // so we can ignore this char. Except for 1 very specific Chris@0: // case where a comment like /**/ needs to tokenize as Chris@0: // T_COMMENT and not T_DOC_COMMENT. Chris@0: $oldType = $this->tokenValues[strtolower($buffer)]; Chris@0: $newType = $this->tokenValues[strtolower($charBuffer)]; Chris@0: if ($oldType === 'T_COMMENT' Chris@0: && $newType === 'T_DOC_COMMENT' Chris@0: && $chars[($i + $x + 1)] === '/' Chris@0: ) { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* look ahead ignored T_DOC_COMMENT, continuing *".PHP_EOL; Chris@0: } Chris@0: } else { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* look ahead found more specific token ($newType), ignoring $i *".PHP_EOL; Chris@0: } Chris@0: Chris@0: $matchedToken = true; Chris@0: break; Chris@0: } Chris@0: }//end if Chris@0: }//end for Chris@0: }//end if Chris@0: Chris@0: if ($matchedToken === false) { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1 && $lookAheadLength > 0) { Chris@0: echo "\t\t* look ahead found nothing *".PHP_EOL; Chris@0: } Chris@0: Chris@0: $value = $this->tokenValues[strtolower($buffer)]; Chris@0: Chris@0: if ($value === 'T_FUNCTION' && $buffer !== 'function') { Chris@0: // The function keyword needs to be all lowercase or else Chris@0: // it is just a function called "Function". Chris@0: $value = 'T_STRING'; Chris@0: } Chris@0: Chris@0: $tokens[] = array( Chris@0: 'code' => constant($value), Chris@0: 'type' => $value, Chris@0: 'content' => $buffer, Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: echo "\t=> Added token $value ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $cleanBuffer = true; Chris@0: }//end if Chris@0: } else if (isset($this->tokenValues[strtolower($char)]) === true) { Chris@0: // No matter what token we end up using, we don't Chris@0: // need the content in the buffer any more because we have Chris@0: // found a valid token. Chris@0: $newContent = substr(str_replace("\n", $eolChar, $buffer), 0, -1); Chris@0: if ($newContent !== '') { Chris@0: $tokens[] = array( Chris@0: 'code' => T_STRING, Chris@0: 'type' => 'T_STRING', Chris@0: 'content' => $newContent, Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput(substr($buffer, 0, -1)); Chris@0: echo "\t=> Added token T_STRING ($content)".PHP_EOL; Chris@0: } Chris@0: } Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* char is token, looking ahead ".($maxTokenLength - 1).' chars *'.PHP_EOL; Chris@0: } Chris@0: Chris@0: // The char is a token type, but we need to look ahead at the Chris@0: // next chars to see if this is actually part of a larger token. Chris@0: // For example, = and ===. Chris@0: $charBuffer = $char; Chris@0: $matchedToken = false; Chris@0: for ($x = 1; $x <= $maxTokenLength; $x++) { Chris@0: if (isset($chars[($i + $x)]) === false) { Chris@0: break; Chris@0: } Chris@0: Chris@0: $charBuffer .= $chars[($i + $x)]; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($charBuffer); Chris@0: echo "\t\t=> Looking ahead $x chars => $content".PHP_EOL; Chris@0: } Chris@0: Chris@0: if (isset($this->tokenValues[strtolower($charBuffer)]) === true) { Chris@0: // We've found something larger that matches Chris@0: // so we can ignore this char. Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $type = $this->tokenValues[strtolower($charBuffer)]; Chris@0: echo "\t\t* look ahead found more specific token ($type), ignoring $i *".PHP_EOL; Chris@0: } Chris@0: Chris@0: $matchedToken = true; Chris@0: break; Chris@0: } Chris@0: }//end for Chris@0: Chris@0: if ($matchedToken === false) { Chris@0: $value = $this->tokenValues[strtolower($char)]; Chris@0: $tokens[] = array( Chris@0: 'code' => constant($value), Chris@0: 'type' => $value, Chris@0: 'content' => $char, Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* look ahead found nothing *".PHP_EOL; Chris@0: $content = PHP_CodeSniffer::prepareForOutput($char); Chris@0: echo "\t=> Added token $value ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $cleanBuffer = true; Chris@0: } else { Chris@0: $buffer = $char; Chris@0: }//end if Chris@0: }//end if Chris@0: Chris@0: // Keep track of content inside comments. Chris@0: if ($inComment === '' Chris@0: && array_key_exists($buffer, $this->commentTokens) === true Chris@0: ) { Chris@0: // This is not really a comment if the content Chris@0: // looks like \// (i.e., it is escaped). Chris@0: if (isset($chars[($i - 2)]) === true && $chars[($i - 2)] === '\\') { Chris@0: $lastToken = array_pop($tokens); Chris@0: $lastContent = $lastToken['content']; Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $value = $this->tokenValues[strtolower($lastContent)]; Chris@0: $content = PHP_CodeSniffer::prepareForOutput($lastContent); Chris@0: echo "\t=> Removed token $value ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $lastChars = str_split($lastContent); Chris@0: $lastNumChars = count($lastChars); Chris@0: for ($x = 0; $x < $lastNumChars; $x++) { Chris@0: $lastChar = $lastChars[$x]; Chris@0: $value = $this->tokenValues[strtolower($lastChar)]; Chris@0: $tokens[] = array( Chris@0: 'code' => constant($value), Chris@0: 'type' => $value, Chris@0: 'content' => $lastChar, Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($lastChar); Chris@0: echo "\t=> Added token $value ($content)".PHP_EOL; Chris@0: } Chris@0: } Chris@0: } else { Chris@0: // We have started a comment. Chris@0: $inComment = $buffer; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t\t* looking for end of comment *".PHP_EOL; Chris@0: } Chris@0: }//end if Chris@0: } else if ($inComment !== '') { Chris@0: if ($this->commentTokens[$inComment] === null) { Chris@0: // Comment ends at the next newline. Chris@0: if (strpos($buffer, "\n") !== false) { Chris@0: $inComment = ''; Chris@0: } Chris@0: } else { Chris@0: if ($this->commentTokens[$inComment] === $buffer) { Chris@0: $inComment = ''; Chris@0: } Chris@0: } Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: if ($inComment === '') { Chris@0: echo "\t\t* found end of comment *".PHP_EOL; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($inComment === '' && $cleanBuffer === false) { Chris@0: $tokens[] = array( Chris@0: 'code' => T_STRING, Chris@0: 'type' => 'T_STRING', Chris@0: 'content' => str_replace("\n", $eolChar, $buffer), Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: echo "\t=> Added token T_STRING ($content)".PHP_EOL; Chris@0: } Chris@0: Chris@0: $buffer = ''; Chris@0: } Chris@0: }//end if Chris@0: Chris@0: if ($cleanBuffer === true) { Chris@0: $buffer = ''; Chris@0: $cleanBuffer = false; Chris@0: } Chris@0: }//end for Chris@0: Chris@0: if (empty($buffer) === false) { Chris@0: // Buffer contains whitespace from the end of the file. Chris@0: $tokens[] = array( Chris@0: 'code' => T_WHITESPACE, Chris@0: 'type' => 'T_WHITESPACE', Chris@0: 'content' => str_replace("\n", $eolChar, $buffer), Chris@0: ); Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $content = PHP_CodeSniffer::prepareForOutput($buffer); Chris@0: echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; Chris@0: } Chris@0: } Chris@0: Chris@0: $tokens[] = array( Chris@0: 'code' => T_CLOSE_TAG, Chris@0: 'type' => 'T_CLOSE_TAG', Chris@0: 'content' => '', Chris@0: ); Chris@0: Chris@0: /* Chris@0: Now that we have done some basic tokenizing, we need to Chris@0: modify the tokens to join some together and split some apart Chris@0: so they match what the PHP tokenizer does. Chris@0: */ Chris@0: Chris@0: $finalTokens = array(); Chris@0: $newStackPtr = 0; Chris@0: $numTokens = count($tokens); Chris@0: for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) { Chris@0: $token = $tokens[$stackPtr]; Chris@0: Chris@0: /* Chris@0: Look for comments and join the tokens together. Chris@0: */ Chris@0: Chris@0: if ($token['code'] === T_COMMENT || $token['code'] === T_DOC_COMMENT) { Chris@0: $newContent = ''; Chris@0: $tokenContent = $token['content']; Chris@0: Chris@0: $endContent = null; Chris@0: if (isset($this->commentTokens[$tokenContent]) === true) { Chris@0: $endContent = $this->commentTokens[$tokenContent]; Chris@0: } Chris@0: Chris@0: while ($tokenContent !== $endContent) { Chris@0: if ($endContent === null Chris@0: && strpos($tokenContent, $eolChar) !== false Chris@0: ) { Chris@0: // A null end token means the comment ends at the end of Chris@0: // the line so we look for newlines and split the token. Chris@0: $tokens[$stackPtr]['content'] = substr( Chris@0: $tokenContent, Chris@0: (strpos($tokenContent, $eolChar) + strlen($eolChar)) Chris@0: ); Chris@0: Chris@0: $tokenContent = substr( Chris@0: $tokenContent, Chris@0: 0, Chris@0: (strpos($tokenContent, $eolChar) + strlen($eolChar)) Chris@0: ); Chris@0: Chris@0: // If the substr failed, skip the token as the content Chris@0: // will now be blank. Chris@0: if ($tokens[$stackPtr]['content'] !== false Chris@0: && $tokens[$stackPtr]['content'] !== '' Chris@0: ) { Chris@0: $stackPtr--; Chris@0: } Chris@0: Chris@0: break; Chris@0: }//end if Chris@0: Chris@0: $stackPtr++; Chris@0: $newContent .= $tokenContent; Chris@0: if (isset($tokens[$stackPtr]) === false) { Chris@0: break; Chris@0: } Chris@0: Chris@0: $tokenContent = $tokens[$stackPtr]['content']; Chris@0: }//end while Chris@0: Chris@0: if ($token['code'] === T_DOC_COMMENT) { Chris@0: $commentTokens = $commentTokenizer->tokenizeString($newContent.$tokenContent, $eolChar, $newStackPtr); Chris@0: foreach ($commentTokens as $commentToken) { Chris@0: $finalTokens[$newStackPtr] = $commentToken; Chris@0: $newStackPtr++; Chris@0: } Chris@0: Chris@0: continue; Chris@0: } else { Chris@0: // Save the new content in the current token so Chris@0: // the code below can chop it up on newlines. Chris@0: $token['content'] = $newContent.$tokenContent; Chris@0: } Chris@0: }//end if Chris@0: Chris@0: /* Chris@0: If this token has newlines in its content, split each line up Chris@0: and create a new token for each line. We do this so it's easier Chris@0: to ascertain where errors occur on a line. Chris@0: Note that $token[1] is the token's content. Chris@0: */ Chris@0: Chris@0: if (strpos($token['content'], $eolChar) !== false) { Chris@0: $tokenLines = explode($eolChar, $token['content']); Chris@0: $numLines = count($tokenLines); Chris@0: Chris@0: for ($i = 0; $i < $numLines; $i++) { Chris@0: $newToken['content'] = $tokenLines[$i]; Chris@0: if ($i === ($numLines - 1)) { Chris@0: if ($tokenLines[$i] === '') { Chris@0: break; Chris@0: } Chris@0: } else { Chris@0: $newToken['content'] .= $eolChar; Chris@0: } Chris@0: Chris@0: $newToken['type'] = $token['type']; Chris@0: $newToken['code'] = $token['code']; Chris@0: $finalTokens[$newStackPtr] = $newToken; Chris@0: $newStackPtr++; Chris@0: } Chris@0: } else { Chris@0: $finalTokens[$newStackPtr] = $token; Chris@0: $newStackPtr++; Chris@0: }//end if Chris@0: Chris@0: // Convert numbers, including decimals. Chris@0: if ($token['code'] === T_STRING Chris@0: || $token['code'] === T_OBJECT_OPERATOR Chris@0: ) { Chris@0: $newContent = ''; Chris@0: $oldStackPtr = $stackPtr; Chris@0: while (preg_match('|^[0-9\.]+$|', $tokens[$stackPtr]['content']) !== 0) { Chris@0: $newContent .= $tokens[$stackPtr]['content']; Chris@0: $stackPtr++; Chris@0: } Chris@0: Chris@0: if ($newContent !== '' && $newContent !== '.') { Chris@0: $finalTokens[($newStackPtr - 1)]['content'] = $newContent; Chris@0: if (ctype_digit($newContent) === true) { Chris@0: $finalTokens[($newStackPtr - 1)]['code'] = constant('T_LNUMBER'); Chris@0: $finalTokens[($newStackPtr - 1)]['type'] = 'T_LNUMBER'; Chris@0: } else { Chris@0: $finalTokens[($newStackPtr - 1)]['code'] = constant('T_DNUMBER'); Chris@0: $finalTokens[($newStackPtr - 1)]['type'] = 'T_DNUMBER'; Chris@0: } Chris@0: Chris@0: $stackPtr--; Chris@0: continue; Chris@0: } else { Chris@0: $stackPtr = $oldStackPtr; Chris@0: } Chris@0: }//end if Chris@0: Chris@0: // Convert the token after an object operator into a string, in most cases. Chris@0: if ($token['code'] === T_OBJECT_OPERATOR) { Chris@0: for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { Chris@0: if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ($tokens[$i]['code'] !== T_PROTOTYPE Chris@0: && $tokens[$i]['code'] !== T_LNUMBER Chris@0: && $tokens[$i]['code'] !== T_DNUMBER Chris@0: ) { Chris@0: $tokens[$i]['code'] = T_STRING; Chris@0: $tokens[$i]['type'] = 'T_STRING'; Chris@0: } Chris@0: Chris@0: break; Chris@0: } Chris@0: } Chris@0: }//end for Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t*** END TOKENIZING ***".PHP_EOL; Chris@0: } Chris@0: Chris@0: return $finalTokens; Chris@0: Chris@0: }//end tokenizeString() Chris@0: Chris@0: Chris@0: /** Chris@0: * Tokenizes a regular expression if one is found. Chris@0: * Chris@0: * If a regular expression is not found, NULL is returned. Chris@0: * Chris@0: * @param string $char The index of the possible regex start character. Chris@0: * @param string $string The complete content of the string being tokenized. Chris@0: * @param string $chars An array of characters being tokenized. Chris@0: * @param string $tokens The current array of tokens found in the string. Chris@0: * @param string $eolChar The EOL character to use for splitting strings. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function getRegexToken($char, $string, $chars, $tokens, $eolChar) Chris@0: { Chris@0: $beforeTokens = array( Chris@0: T_EQUAL => true, Chris@0: T_IS_NOT_EQUAL => true, Chris@0: T_IS_IDENTICAL => true, Chris@0: T_IS_NOT_IDENTICAL => true, Chris@0: T_OPEN_PARENTHESIS => true, Chris@0: T_OPEN_SQUARE_BRACKET => true, Chris@0: T_RETURN => true, Chris@0: T_BOOLEAN_OR => true, Chris@0: T_BOOLEAN_AND => true, Chris@0: T_BITWISE_OR => true, Chris@0: T_BITWISE_AND => true, Chris@0: T_COMMA => true, Chris@0: T_COLON => true, Chris@0: T_TYPEOF => true, Chris@0: T_INLINE_THEN => true, Chris@0: T_INLINE_ELSE => true, Chris@0: ); Chris@0: Chris@0: $afterTokens = array( Chris@0: ',' => true, Chris@0: ')' => true, Chris@0: ']' => true, Chris@0: ';' => true, Chris@0: ' ' => true, Chris@0: '.' => true, Chris@0: ':' => true, Chris@0: $eolChar => true, Chris@0: ); Chris@0: Chris@0: // Find the last non-whitespace token that was added Chris@0: // to the tokens array. Chris@0: $numTokens = count($tokens); Chris@0: for ($prev = ($numTokens - 1); $prev >= 0; $prev--) { Chris@0: if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$prev]['code']]) === false) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($beforeTokens[$tokens[$prev]['code']]) === false) { Chris@0: return null; Chris@0: } Chris@0: Chris@0: // This is probably a regular expression, so look for the end of it. Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t* token possibly starts a regular expression *".PHP_EOL; Chris@0: } Chris@0: Chris@0: $numChars = count($chars); Chris@0: for ($next = ($char + 1); $next < $numChars; $next++) { Chris@0: if ($chars[$next] === '/') { Chris@0: // Just make sure this is not escaped first. Chris@0: if ($chars[($next - 1)] !== '\\') { Chris@0: // In the simple form: /.../ so we found the end. Chris@0: break; Chris@0: } else if ($chars[($next - 2)] === '\\') { Chris@0: // In the form: /...\\/ so we found the end. Chris@0: break; Chris@0: } Chris@0: } else { Chris@0: $possibleEolChar = substr($string, $next, strlen($eolChar)); Chris@0: if ($possibleEolChar === $eolChar) { Chris@0: // This is the last token on the line and regular Chris@0: // expressions need to be defined on a single line, Chris@0: // so this is not a regular expression. Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($chars[$next] !== '/') { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t* could not find end of regular expression *".PHP_EOL; Chris@0: } Chris@0: Chris@0: return null; Chris@0: } Chris@0: Chris@0: while (preg_match('|[a-zA-Z]|', $chars[($next + 1)]) !== 0) { Chris@0: // The token directly after the end of the regex can Chris@0: // be modifiers like global and case insensitive Chris@0: // (.e.g, /pattern/gi). Chris@0: $next++; Chris@0: } Chris@0: Chris@0: $regexEnd = $next; Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t* found end of regular expression at token $regexEnd *".PHP_EOL; Chris@0: } Chris@0: Chris@0: for ($next = ($next + 1); $next < $numChars; $next++) { Chris@0: if ($chars[$next] !== ' ') { Chris@0: break; Chris@0: } else { Chris@0: $possibleEolChar = substr($string, $next, strlen($eolChar)); Chris@0: if ($possibleEolChar === $eolChar) { Chris@0: // This is the last token on the line. Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($afterTokens[$chars[$next]]) === false) { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t* tokens after regular expression do not look correct *".PHP_EOL; Chris@0: } Chris@0: Chris@0: return null; Chris@0: } Chris@0: Chris@0: // This is a regular expression, so join all the tokens together. Chris@0: $content = ''; Chris@0: for ($x = $char; $x <= $regexEnd; $x++) { Chris@0: $content .= $chars[$x]; Chris@0: } Chris@0: Chris@0: $token = array( Chris@0: 'start' => $char, Chris@0: 'end' => $regexEnd, Chris@0: 'content' => $content, Chris@0: ); Chris@0: Chris@0: return $token; Chris@0: Chris@0: }//end getRegexToken() Chris@0: Chris@0: Chris@0: /** Chris@0: * Performs additional processing after main tokenizing. Chris@0: * Chris@0: * This additional processing looks for properties, closures, labels and objects. Chris@0: * Chris@0: * @param array $tokens The array of tokens to process. Chris@0: * @param string $eolChar The EOL character to use for splitting strings. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function processAdditional(&$tokens, $eolChar) Chris@0: { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t*** START ADDITIONAL JS PROCESSING ***".PHP_EOL; Chris@0: } Chris@0: Chris@0: $numTokens = count($tokens); Chris@0: $classStack = array(); Chris@0: Chris@0: for ($i = 0; $i < $numTokens; $i++) { Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $type = $tokens[$i]['type']; Chris@0: $content = PHP_CodeSniffer::prepareForOutput($tokens[$i]['content']); Chris@0: Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\tProcess token $i: $type => $content".PHP_EOL; Chris@0: } Chris@0: Chris@0: // Looking for functions that are actually closures. Chris@0: if ($tokens[$i]['code'] === T_FUNCTION && isset($tokens[$i]['scope_opener']) === true) { Chris@0: for ($x = ($i + 1); $x < $numTokens; $x++) { Chris@0: if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($tokens[$x]['code'] === T_OPEN_PARENTHESIS) { Chris@0: $tokens[$i]['code'] = T_CLOSURE; Chris@0: $tokens[$i]['type'] = 'T_CLOSURE'; Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $line = $tokens[$i]['line']; Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $i on line $line changed from T_FUNCTION to T_CLOSURE".PHP_EOL; Chris@0: } Chris@0: Chris@0: for ($x = ($tokens[$i]['scope_opener'] + 1); $x < $tokens[$i]['scope_closer']; $x++) { Chris@0: if (isset($tokens[$x]['conditions'][$i]) === false) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $tokens[$x]['conditions'][$i] = T_CLOSURE; Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $type = $tokens[$x]['type']; Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t\t* cleaned $x ($type) *".PHP_EOL; Chris@0: } Chris@0: } Chris@0: }//end if Chris@0: Chris@0: continue; Chris@0: } else if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET Chris@0: && isset($tokens[$i]['scope_condition']) === false Chris@0: && isset($tokens[$i]['bracket_closer']) === true Chris@0: ) { Chris@0: $classStack[] = $i; Chris@0: Chris@0: $closer = $tokens[$i]['bracket_closer']; Chris@0: $tokens[$i]['code'] = T_OBJECT; Chris@0: $tokens[$i]['type'] = 'T_OBJECT'; Chris@0: $tokens[$closer]['code'] = T_CLOSE_OBJECT; Chris@0: $tokens[$closer]['type'] = 'T_CLOSE_OBJECT'; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $i converted from T_OPEN_CURLY_BRACKET to T_OBJECT *".PHP_EOL; Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $closer converted from T_CLOSE_CURLY_BRACKET to T_CLOSE_OBJECT *".PHP_EOL; Chris@0: } Chris@0: Chris@0: for ($x = ($i + 1); $x < $closer; $x++) { Chris@0: $tokens[$x]['conditions'][$i] = T_OBJECT; Chris@0: ksort($tokens[$x]['conditions'], SORT_NUMERIC); Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: $type = $tokens[$x]['type']; Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t\t* added T_OBJECT condition to $x ($type) *".PHP_EOL; Chris@0: } Chris@0: } Chris@0: } else if ($tokens[$i]['code'] === T_CLOSE_OBJECT) { Chris@0: $opener = array_pop($classStack); Chris@0: } else if ($tokens[$i]['code'] === T_COLON) { Chris@0: // If it is a scope opener, it belongs to a Chris@0: // DEFAULT or CASE statement. Chris@0: if (isset($tokens[$i]['scope_condition']) === true) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Make sure this is not part of an inline IF statement. Chris@0: for ($x = ($i - 1); $x >= 0; $x--) { Chris@0: if ($tokens[$x]['code'] === T_INLINE_THEN) { Chris@0: $tokens[$i]['code'] = T_INLINE_ELSE; Chris@0: $tokens[$i]['type'] = 'T_INLINE_ELSE'; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $i converted from T_COLON to T_INLINE_THEN *".PHP_EOL; Chris@0: } Chris@0: Chris@0: continue(2); Chris@0: } else if ($tokens[$x]['line'] < $tokens[$i]['line']) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: // The string to the left of the colon is either a property or label. Chris@0: for ($label = ($i - 1); $label >= 0; $label--) { Chris@0: if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$label]['code']]) === false) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($tokens[$label]['code'] !== T_STRING Chris@0: && $tokens[$label]['code'] !== T_CONSTANT_ENCAPSED_STRING Chris@0: ) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (empty($classStack) === false) { Chris@0: $tokens[$label]['code'] = T_PROPERTY; Chris@0: $tokens[$label]['type'] = 'T_PROPERTY'; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $label converted from T_STRING to T_PROPERTY *".PHP_EOL; Chris@0: } Chris@0: } else { Chris@0: $tokens[$label]['code'] = T_LABEL; Chris@0: $tokens[$label]['type'] = 'T_LABEL'; Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo str_repeat("\t", count($classStack)); Chris@0: echo "\t* token $label converted from T_STRING to T_LABEL *".PHP_EOL; Chris@0: } Chris@0: }//end if Chris@0: }//end if Chris@0: }//end for Chris@0: Chris@0: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@0: echo "\t*** END ADDITIONAL JS PROCESSING ***".PHP_EOL; Chris@0: } Chris@0: Chris@0: }//end processAdditional() Chris@0: Chris@0: Chris@0: }//end class