Chris@17: Chris@17: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@17: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@17: */ Chris@17: Chris@17: namespace PHP_CodeSniffer\Tokenizers; Chris@17: Chris@17: use PHP_CodeSniffer\Exceptions\RuntimeException; Chris@17: use PHP_CodeSniffer\Util; Chris@17: Chris@17: abstract class Tokenizer Chris@17: { Chris@17: Chris@17: /** Chris@17: * The config data for the run. Chris@17: * Chris@17: * @var \PHP_CodeSniffer\Config Chris@17: */ Chris@17: protected $config = null; Chris@17: Chris@17: /** Chris@17: * The EOL char used in the content. Chris@17: * Chris@17: * @var string Chris@17: */ Chris@17: protected $eolChar = []; Chris@17: Chris@17: /** Chris@17: * A token-based representation of the content. Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: protected $tokens = []; Chris@17: Chris@17: /** Chris@18: * The number of tokens in the tokens array. Chris@18: * Chris@18: * @var integer Chris@18: */ Chris@18: protected $numTokens = 0; Chris@18: Chris@18: /** Chris@18: * A list of tokens that are allowed to open a scope. Chris@18: * Chris@18: * @var array Chris@18: */ Chris@18: public $scopeOpeners = []; Chris@18: Chris@18: /** Chris@18: * A list of tokens that end the scope. Chris@18: * Chris@18: * @var array Chris@18: */ Chris@18: public $endScopeTokens = []; Chris@18: Chris@18: /** Chris@17: * Known lengths of tokens. Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: public $knownLengths = []; Chris@17: Chris@17: /** Chris@17: * A list of lines being ignored due to error suppression comments. Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: public $ignoredLines = []; Chris@17: Chris@17: Chris@17: /** Chris@17: * Initialise and run the tokenizer. Chris@17: * Chris@17: * @param string $content The content to tokenize, Chris@17: * @param \PHP_CodeSniffer\Config | null $config The config data for the run. Chris@17: * @param string $eolChar The EOL char used in the content. Chris@17: * Chris@17: * @return void Chris@18: * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the file appears to be minified. Chris@17: */ Chris@17: public function __construct($content, $config, $eolChar='\n') Chris@17: { Chris@17: $this->eolChar = $eolChar; Chris@17: Chris@17: $this->config = $config; Chris@17: $this->tokens = $this->tokenize($content); Chris@17: Chris@17: if ($config === null) { Chris@17: return; Chris@17: } Chris@17: Chris@17: $this->createPositionMap(); Chris@17: $this->createTokenMap(); Chris@17: $this->createParenthesisNestingMap(); Chris@17: $this->createScopeMap(); Chris@17: $this->createLevelMap(); Chris@17: Chris@17: // Allow the tokenizer to do additional processing if required. Chris@17: $this->processAdditional(); Chris@17: Chris@17: }//end __construct() Chris@17: Chris@17: Chris@17: /** Chris@17: * Checks the content to see if it looks minified. Chris@17: * Chris@17: * @param string $content The content to tokenize. Chris@17: * @param string $eolChar The EOL char used in the content. Chris@17: * Chris@17: * @return boolean Chris@17: */ Chris@17: protected function isMinifiedContent($content, $eolChar='\n') Chris@17: { Chris@17: // Minified files often have a very large number of characters per line Chris@17: // and cause issues when tokenizing. Chris@17: $numChars = strlen($content); Chris@17: $numLines = (substr_count($content, $eolChar) + 1); Chris@17: $average = ($numChars / $numLines); Chris@17: if ($average > 100) { Chris@17: return true; Chris@17: } Chris@17: Chris@17: return false; Chris@17: Chris@17: }//end isMinifiedContent() Chris@17: Chris@17: Chris@17: /** Chris@17: * Gets the array of tokens. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: public function getTokens() Chris@17: { Chris@17: return $this->tokens; Chris@17: Chris@17: }//end getTokens() Chris@17: Chris@17: Chris@17: /** Chris@17: * Creates an array of tokens when given some content. Chris@17: * Chris@17: * @param string $string The string to tokenize. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: abstract protected function tokenize($string); Chris@17: Chris@17: Chris@17: /** Chris@17: * Performs additional processing after main tokenizing. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: abstract protected function processAdditional(); Chris@17: Chris@17: Chris@17: /** Chris@17: * Sets token position information. Chris@17: * Chris@17: * Can also convert tabs into spaces. Each tab can represent between Chris@17: * 1 and $width spaces, so this cannot be a straight string replace. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: private function createPositionMap() Chris@17: { Chris@17: $currColumn = 1; Chris@17: $lineNumber = 1; Chris@17: $eolLen = strlen($this->eolChar); Chris@17: $ignoring = null; Chris@17: $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); Chris@17: Chris@17: $checkEncoding = false; Chris@17: if (function_exists('iconv_strlen') === true) { Chris@17: $checkEncoding = true; Chris@17: } Chris@17: Chris@17: $checkAnnotations = $this->config->annotations; Chris@17: $encoding = $this->config->encoding; Chris@17: $tabWidth = $this->config->tabWidth; Chris@17: Chris@18: $tokensWithTabs = [ Chris@17: T_WHITESPACE => true, Chris@17: T_COMMENT => true, Chris@17: T_DOC_COMMENT => true, Chris@17: T_DOC_COMMENT_WHITESPACE => true, Chris@17: T_DOC_COMMENT_STRING => true, Chris@17: T_CONSTANT_ENCAPSED_STRING => true, Chris@17: T_DOUBLE_QUOTED_STRING => true, Chris@17: T_HEREDOC => true, Chris@17: T_NOWDOC => true, Chris@17: T_INLINE_HTML => true, Chris@17: ]; Chris@17: Chris@17: $this->numTokens = count($this->tokens); Chris@17: for ($i = 0; $i < $this->numTokens; $i++) { Chris@17: $this->tokens[$i]['line'] = $lineNumber; Chris@17: $this->tokens[$i]['column'] = $currColumn; Chris@17: Chris@17: if (isset($this->knownLengths[$this->tokens[$i]['code']]) === true) { Chris@17: // There are no tabs in the tokens we know the length of. Chris@17: $length = $this->knownLengths[$this->tokens[$i]['code']]; Chris@17: $currColumn += $length; Chris@17: } else if ($tabWidth === 0 Chris@18: || isset($tokensWithTabs[$this->tokens[$i]['code']]) === false Chris@17: || strpos($this->tokens[$i]['content'], "\t") === false Chris@17: ) { Chris@17: // There are no tabs in this content, or we aren't replacing them. Chris@17: if ($checkEncoding === true) { Chris@17: // Not using the default encoding, so take a bit more care. Chris@17: $oldLevel = error_reporting(); Chris@17: error_reporting(0); Chris@17: $length = iconv_strlen($this->tokens[$i]['content'], $encoding); Chris@17: error_reporting($oldLevel); Chris@17: Chris@17: if ($length === false) { Chris@17: // String contained invalid characters, so revert to default. Chris@17: $length = strlen($this->tokens[$i]['content']); Chris@17: } Chris@17: } else { Chris@17: $length = strlen($this->tokens[$i]['content']); Chris@17: } Chris@17: Chris@17: $currColumn += $length; Chris@17: } else { Chris@17: $this->replaceTabsInToken($this->tokens[$i]); Chris@17: $length = $this->tokens[$i]['length']; Chris@17: $currColumn += $length; Chris@17: }//end if Chris@17: Chris@17: $this->tokens[$i]['length'] = $length; Chris@17: Chris@17: if (isset($this->knownLengths[$this->tokens[$i]['code']]) === false Chris@17: && strpos($this->tokens[$i]['content'], $this->eolChar) !== false Chris@17: ) { Chris@17: $lineNumber++; Chris@17: $currColumn = 1; Chris@17: Chris@17: // Newline chars are not counted in the token length. Chris@17: $this->tokens[$i]['length'] -= $eolLen; Chris@17: } Chris@17: Chris@17: if ($this->tokens[$i]['code'] === T_COMMENT Chris@17: || $this->tokens[$i]['code'] === T_DOC_COMMENT_STRING Chris@17: || $this->tokens[$i]['code'] === T_DOC_COMMENT_TAG Chris@17: || ($inTests === true && $this->tokens[$i]['code'] === T_INLINE_HTML) Chris@17: ) { Chris@17: $commentText = ltrim($this->tokens[$i]['content'], " \t/*"); Chris@17: $commentText = rtrim($commentText, " */\t\r\n"); Chris@17: $commentTextLower = strtolower($commentText); Chris@17: if (strpos($commentText, '@codingStandards') !== false) { Chris@17: // If this comment is the only thing on the line, it tells us Chris@17: // to ignore the following line. If the line contains other content Chris@17: // then we are just ignoring this one single line. Chris@17: $ownLine = false; Chris@17: if ($i > 0) { Chris@17: for ($prev = ($i - 1); $prev >= 0; $prev--) { Chris@17: if ($this->tokens[$prev]['code'] === T_WHITESPACE) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: break; Chris@17: } Chris@17: Chris@17: if ($this->tokens[$prev]['line'] !== $this->tokens[$i]['line']) { Chris@17: $ownLine = true; Chris@17: } Chris@17: } Chris@17: Chris@17: if ($ignoring === null Chris@17: && strpos($commentText, '@codingStandardsIgnoreStart') !== false Chris@17: ) { Chris@17: $ignoring = ['.all' => true]; Chris@17: if ($ownLine === true) { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: } Chris@17: } else if ($ignoring !== null Chris@17: && strpos($commentText, '@codingStandardsIgnoreEnd') !== false Chris@17: ) { Chris@17: if ($ownLine === true) { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; Chris@17: } else { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: } Chris@17: Chris@17: $ignoring = null; Chris@17: } else if ($ignoring === null Chris@17: && strpos($commentText, '@codingStandardsIgnoreLine') !== false Chris@17: ) { Chris@17: $ignoring = ['.all' => true]; Chris@17: if ($ownLine === true) { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: $this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $ignoring; Chris@17: } else { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: } Chris@17: Chris@17: $ignoring = null; Chris@17: }//end if Chris@17: } else if (substr($commentTextLower, 0, 6) === 'phpcs:' Chris@17: || substr($commentTextLower, 0, 7) === '@phpcs:' Chris@17: ) { Chris@17: // If the @phpcs: syntax is being used, strip the @ to make Chris@17: // comparisons easier. Chris@17: if ($commentText[0] === '@') { Chris@17: $commentText = substr($commentText, 1); Chris@17: $commentTextLower = strtolower($commentText); Chris@17: } Chris@17: Chris@17: // If there is a comment on the end, strip it off. Chris@17: $commentStart = strpos($commentTextLower, ' --'); Chris@17: if ($commentStart !== false) { Chris@17: $commentText = substr($commentText, 0, $commentStart); Chris@17: $commentTextLower = strtolower($commentText); Chris@17: } Chris@17: Chris@17: // If this comment is the only thing on the line, it tells us Chris@17: // to ignore the following line. If the line contains other content Chris@17: // then we are just ignoring this one single line. Chris@17: $lineHasOtherContent = false; Chris@17: $lineHasOtherTokens = false; Chris@17: if ($i > 0) { Chris@17: for ($prev = ($i - 1); $prev > 0; $prev--) { Chris@17: if ($this->tokens[$prev]['line'] !== $this->tokens[$i]['line']) { Chris@17: // Changed lines. Chris@17: break; Chris@17: } Chris@17: Chris@17: if ($this->tokens[$prev]['code'] === T_WHITESPACE Chris@17: || ($this->tokens[$prev]['code'] === T_INLINE_HTML Chris@17: && trim($this->tokens[$prev]['content']) === '') Chris@17: ) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $lineHasOtherTokens = true; Chris@17: Chris@17: if ($this->tokens[$prev]['code'] === T_OPEN_TAG) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $lineHasOtherContent = true; Chris@17: break; Chris@17: }//end for Chris@17: Chris@17: $changedLines = false; Chris@17: for ($next = $i; $next < $this->numTokens; $next++) { Chris@17: if ($changedLines === true) { Chris@17: // Changed lines. Chris@17: break; Chris@17: } Chris@17: Chris@17: if (isset($this->knownLengths[$this->tokens[$next]['code']]) === false Chris@17: && strpos($this->tokens[$next]['content'], $this->eolChar) !== false Chris@17: ) { Chris@17: // Last token on the current line. Chris@17: $changedLines = true; Chris@17: } Chris@17: Chris@17: if ($next === $i) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if ($this->tokens[$next]['code'] === T_WHITESPACE Chris@17: || ($this->tokens[$next]['code'] === T_INLINE_HTML Chris@17: && trim($this->tokens[$next]['content']) === '') Chris@17: ) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $lineHasOtherTokens = true; Chris@17: Chris@17: if ($this->tokens[$next]['code'] === T_CLOSE_TAG) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $lineHasOtherContent = true; Chris@17: break; Chris@17: }//end for Chris@17: }//end if Chris@17: Chris@17: if (substr($commentTextLower, 0, 9) === 'phpcs:set') { Chris@17: // Ignore standards for complete lines that change sniff settings. Chris@17: if ($lineHasOtherTokens === false) { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; Chris@17: } Chris@17: Chris@18: // Need to maintain case here, to get the correct sniff code. Chris@18: $parts = explode(' ', substr($commentText, 10)); Chris@18: if (count($parts) >= 2) { Chris@18: $sniffParts = explode('.', $parts[0]); Chris@18: if (count($sniffParts) >= 3) { Chris@18: $this->tokens[$i]['sniffCode'] = array_shift($parts); Chris@18: $this->tokens[$i]['sniffProperty'] = array_shift($parts); Chris@18: $this->tokens[$i]['sniffPropertyValue'] = rtrim(implode(' ', $parts), " */\r\n"); Chris@18: } Chris@18: } Chris@18: Chris@17: $this->tokens[$i]['code'] = T_PHPCS_SET; Chris@17: $this->tokens[$i]['type'] = 'T_PHPCS_SET'; Chris@17: } else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile') { Chris@17: // The whole file will be ignored, but at least set the correct token. Chris@17: $this->tokens[$i]['code'] = T_PHPCS_IGNORE_FILE; Chris@17: $this->tokens[$i]['type'] = 'T_PHPCS_IGNORE_FILE'; Chris@17: } else if (substr($commentTextLower, 0, 13) === 'phpcs:disable') { Chris@17: if ($lineHasOtherContent === false) { Chris@17: // Completely ignore the comment line. Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; Chris@17: } Chris@17: Chris@17: if ($ignoring === null) { Chris@17: $ignoring = []; Chris@17: } Chris@17: Chris@17: $disabledSniffs = []; Chris@17: Chris@17: $additionalText = substr($commentText, 14); Chris@17: if ($additionalText === false) { Chris@17: $ignoring = ['.all' => true]; Chris@17: } else { Chris@17: $parts = explode(',', substr($commentText, 13)); Chris@17: foreach ($parts as $sniffCode) { Chris@17: $sniffCode = trim($sniffCode); Chris@17: $disabledSniffs[$sniffCode] = true; Chris@17: $ignoring[$sniffCode] = true; Chris@17: Chris@17: // This newly disabled sniff might be disabling an existing Chris@17: // enabled exception that we are tracking. Chris@17: if (isset($ignoring['.except']) === true) { Chris@17: foreach (array_keys($ignoring['.except']) as $ignoredSniffCode) { Chris@17: if ($ignoredSniffCode === $sniffCode Chris@17: || strpos($ignoredSniffCode, $sniffCode.'.') === 0 Chris@17: ) { Chris@17: unset($ignoring['.except'][$ignoredSniffCode]); Chris@17: } Chris@17: } Chris@17: Chris@17: if (empty($ignoring['.except']) === true) { Chris@17: unset($ignoring['.except']); Chris@17: } Chris@17: } Chris@17: }//end foreach Chris@17: }//end if Chris@17: Chris@17: $this->tokens[$i]['code'] = T_PHPCS_DISABLE; Chris@17: $this->tokens[$i]['type'] = 'T_PHPCS_DISABLE'; Chris@17: $this->tokens[$i]['sniffCodes'] = $disabledSniffs; Chris@17: } else if (substr($commentTextLower, 0, 12) === 'phpcs:enable') { Chris@17: if ($ignoring !== null) { Chris@17: $enabledSniffs = []; Chris@17: Chris@17: $additionalText = substr($commentText, 13); Chris@17: if ($additionalText === false) { Chris@17: $ignoring = null; Chris@17: } else { Chris@17: $parts = explode(',', substr($commentText, 13)); Chris@17: foreach ($parts as $sniffCode) { Chris@17: $sniffCode = trim($sniffCode); Chris@17: $enabledSniffs[$sniffCode] = true; Chris@17: Chris@17: // This new enabled sniff might remove previously disabled Chris@17: // sniffs if it is actually a standard or category of sniffs. Chris@17: foreach (array_keys($ignoring) as $ignoredSniffCode) { Chris@17: if ($ignoredSniffCode === $sniffCode Chris@17: || strpos($ignoredSniffCode, $sniffCode.'.') === 0 Chris@17: ) { Chris@17: unset($ignoring[$ignoredSniffCode]); Chris@17: } Chris@17: } Chris@17: Chris@17: // This new enabled sniff might be able to clear up Chris@17: // previously enabled sniffs if it is actually a standard or Chris@17: // category of sniffs. Chris@17: if (isset($ignoring['.except']) === true) { Chris@17: foreach (array_keys($ignoring['.except']) as $ignoredSniffCode) { Chris@17: if ($ignoredSniffCode === $sniffCode Chris@17: || strpos($ignoredSniffCode, $sniffCode.'.') === 0 Chris@17: ) { Chris@17: unset($ignoring['.except'][$ignoredSniffCode]); Chris@17: } Chris@17: } Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: if (empty($ignoring) === true) { Chris@17: $ignoring = null; Chris@17: } else { Chris@17: if (isset($ignoring['.except']) === true) { Chris@17: $ignoring['.except'] += $enabledSniffs; Chris@17: } else { Chris@17: $ignoring['.except'] = $enabledSniffs; Chris@17: } Chris@17: } Chris@17: }//end if Chris@17: Chris@17: if ($lineHasOtherContent === false) { Chris@17: // Completely ignore the comment line. Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; Chris@17: } else { Chris@17: // The comment is on the same line as the code it is ignoring, Chris@17: // so respect the new ignore rules. Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: } Chris@17: Chris@17: $this->tokens[$i]['sniffCodes'] = $enabledSniffs; Chris@17: }//end if Chris@17: Chris@17: $this->tokens[$i]['code'] = T_PHPCS_ENABLE; Chris@17: $this->tokens[$i]['type'] = 'T_PHPCS_ENABLE'; Chris@17: } else if (substr($commentTextLower, 0, 12) === 'phpcs:ignore') { Chris@17: $ignoreRules = []; Chris@17: Chris@17: $additionalText = substr($commentText, 13); Chris@17: if ($additionalText === false) { Chris@17: $ignoreRules = ['.all' => true]; Chris@17: } else { Chris@17: $parts = explode(',', substr($commentText, 13)); Chris@17: foreach ($parts as $sniffCode) { Chris@17: $ignoreRules[trim($sniffCode)] = true; Chris@17: } Chris@17: } Chris@17: Chris@17: $this->tokens[$i]['code'] = T_PHPCS_IGNORE; Chris@17: $this->tokens[$i]['type'] = 'T_PHPCS_IGNORE'; Chris@17: $this->tokens[$i]['sniffCodes'] = $ignoreRules; Chris@17: Chris@17: if ($ignoring !== null) { Chris@17: $ignoreRules += $ignoring; Chris@17: } Chris@17: Chris@17: if ($lineHasOtherContent === false) { Chris@18: // Completely ignore the comment line, and set the following Chris@17: // line to include the ignore rules we've set. Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true]; Chris@17: $this->ignoredLines[($this->tokens[$i]['line'] + 1)] = $ignoreRules; Chris@17: } else { Chris@17: // The comment is on the same line as the code it is ignoring, Chris@17: // so respect the ignore rules it set. Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoreRules; Chris@17: } Chris@17: }//end if Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: if ($ignoring !== null && isset($this->ignoredLines[$this->tokens[$i]['line']]) === false) { Chris@17: $this->ignoredLines[$this->tokens[$i]['line']] = $ignoring; Chris@17: } Chris@17: }//end for Chris@17: Chris@17: // If annotations are being ignored, we clear out all the ignore rules Chris@17: // but leave the annotations tokenized as normal. Chris@17: if ($checkAnnotations === false) { Chris@17: $this->ignoredLines = []; Chris@17: } Chris@17: Chris@17: }//end createPositionMap() Chris@17: Chris@17: Chris@17: /** Chris@17: * Replaces tabs in original token content with spaces. Chris@17: * Chris@17: * Each tab can represent between 1 and $config->tabWidth spaces, Chris@17: * so this cannot be a straight string replace. The original content Chris@17: * is placed into an orig_content index and the new token length is also Chris@17: * set in the length index. Chris@17: * Chris@17: * @param array $token The token to replace tabs inside. Chris@17: * @param string $prefix The character to use to represent the start of a tab. Chris@17: * @param string $padding The character to use to represent the end of a tab. Chris@17: * @param int $tabWidth The number of spaces each tab represents. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: public function replaceTabsInToken(&$token, $prefix=' ', $padding=' ', $tabWidth=null) Chris@17: { Chris@17: $checkEncoding = false; Chris@17: if (function_exists('iconv_strlen') === true) { Chris@17: $checkEncoding = true; Chris@17: } Chris@17: Chris@17: $currColumn = $token['column']; Chris@17: if ($tabWidth === null) { Chris@17: $tabWidth = $this->config->tabWidth; Chris@17: if ($tabWidth === 0) { Chris@17: $tabWidth = 1; Chris@17: } Chris@17: } Chris@17: Chris@17: if (rtrim($token['content'], "\t") === '') { Chris@17: // String only contains tabs, so we can shortcut the process. Chris@17: $numTabs = strlen($token['content']); Chris@17: Chris@17: $firstTabSize = ($tabWidth - (($currColumn - 1) % $tabWidth)); Chris@17: $length = ($firstTabSize + ($tabWidth * ($numTabs - 1))); Chris@17: $newContent = $prefix.str_repeat($padding, ($length - 1)); Chris@17: } else { Chris@17: // We need to determine the length of each tab. Chris@17: $tabs = explode("\t", $token['content']); Chris@17: Chris@17: $numTabs = (count($tabs) - 1); Chris@17: $tabNum = 0; Chris@17: $newContent = ''; Chris@17: $length = 0; Chris@17: Chris@17: foreach ($tabs as $content) { Chris@17: if ($content !== '') { Chris@17: $newContent .= $content; Chris@17: if ($checkEncoding === true) { Chris@17: // Not using the default encoding, so take a bit more care. Chris@17: $oldLevel = error_reporting(); Chris@17: error_reporting(0); Chris@17: $contentLength = iconv_strlen($content, $this->config->encoding); Chris@17: error_reporting($oldLevel); Chris@17: if ($contentLength === false) { Chris@17: // String contained invalid characters, so revert to default. Chris@17: $contentLength = strlen($content); Chris@17: } Chris@17: } else { Chris@17: $contentLength = strlen($content); Chris@17: } Chris@17: Chris@17: $currColumn += $contentLength; Chris@17: $length += $contentLength; Chris@17: } Chris@17: Chris@17: // The last piece of content does not have a tab after it. Chris@17: if ($tabNum === $numTabs) { Chris@17: break; Chris@17: } Chris@17: Chris@17: // Process the tab that comes after the content. Chris@17: $lastCurrColumn = $currColumn; Chris@17: $tabNum++; Chris@17: Chris@17: // Move the pointer to the next tab stop. Chris@17: if (($currColumn % $tabWidth) === 0) { Chris@17: // This is the first tab, and we are already at a Chris@17: // tab stop, so this tab counts as a single space. Chris@17: $currColumn++; Chris@17: } else { Chris@17: $currColumn++; Chris@17: while (($currColumn % $tabWidth) !== 0) { Chris@17: $currColumn++; Chris@17: } Chris@17: Chris@17: $currColumn++; Chris@17: } Chris@17: Chris@17: $length += ($currColumn - $lastCurrColumn); Chris@17: $newContent .= $prefix.str_repeat($padding, ($currColumn - $lastCurrColumn - 1)); Chris@17: }//end foreach Chris@17: }//end if Chris@17: Chris@17: $token['orig_content'] = $token['content']; Chris@17: $token['content'] = $newContent; Chris@17: $token['length'] = $length; Chris@17: Chris@17: }//end replaceTabsInToken() Chris@17: Chris@17: Chris@17: /** Chris@17: * Creates a map of brackets positions. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: private function createTokenMap() Chris@17: { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** START TOKEN MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: $squareOpeners = []; Chris@17: $curlyOpeners = []; Chris@17: $this->numTokens = count($this->tokens); Chris@17: Chris@17: $openers = []; Chris@17: $openOwner = null; Chris@17: Chris@17: for ($i = 0; $i < $this->numTokens; $i++) { Chris@17: /* Chris@17: Parenthesis mapping. Chris@17: */ Chris@17: Chris@17: if (isset(Util\Tokens::$parenthesisOpeners[$this->tokens[$i]['code']]) === true) { Chris@17: $this->tokens[$i]['parenthesis_opener'] = null; Chris@17: $this->tokens[$i]['parenthesis_closer'] = null; Chris@17: $this->tokens[$i]['parenthesis_owner'] = $i; Chris@17: $openOwner = $i; Chris@17: } else if ($this->tokens[$i]['code'] === T_OPEN_PARENTHESIS) { Chris@17: $openers[] = $i; Chris@17: $this->tokens[$i]['parenthesis_opener'] = $i; Chris@17: if ($openOwner !== null) { Chris@17: $this->tokens[$openOwner]['parenthesis_opener'] = $i; Chris@17: $this->tokens[$i]['parenthesis_owner'] = $openOwner; Chris@17: $openOwner = null; Chris@17: } Chris@17: } else if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS) { Chris@17: // Did we set an owner for this set of parenthesis? Chris@17: $numOpeners = count($openers); Chris@17: if ($numOpeners !== 0) { Chris@17: $opener = array_pop($openers); Chris@17: if (isset($this->tokens[$opener]['parenthesis_owner']) === true) { Chris@17: $owner = $this->tokens[$opener]['parenthesis_owner']; Chris@17: Chris@17: $this->tokens[$owner]['parenthesis_closer'] = $i; Chris@17: $this->tokens[$i]['parenthesis_owner'] = $owner; Chris@17: } Chris@17: Chris@17: $this->tokens[$i]['parenthesis_opener'] = $opener; Chris@17: $this->tokens[$i]['parenthesis_closer'] = $i; Chris@17: $this->tokens[$opener]['parenthesis_closer'] = $i; Chris@17: } Chris@17: }//end if Chris@17: Chris@17: /* Chris@17: Bracket mapping. Chris@17: */ Chris@17: Chris@17: switch ($this->tokens[$i]['code']) { Chris@17: case T_OPEN_SQUARE_BRACKET: Chris@17: $squareOpeners[] = $i; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", count($squareOpeners)); Chris@17: echo str_repeat("\t", count($curlyOpeners)); Chris@17: echo "=> Found square bracket opener at $i".PHP_EOL; Chris@17: } Chris@17: break; Chris@17: case T_OPEN_CURLY_BRACKET: Chris@17: if (isset($this->tokens[$i]['scope_closer']) === false) { Chris@17: $curlyOpeners[] = $i; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", count($squareOpeners)); Chris@17: echo str_repeat("\t", count($curlyOpeners)); Chris@17: echo "=> Found curly bracket opener at $i".PHP_EOL; Chris@17: } Chris@17: } Chris@17: break; Chris@17: case T_CLOSE_SQUARE_BRACKET: Chris@17: if (empty($squareOpeners) === false) { Chris@17: $opener = array_pop($squareOpeners); Chris@17: $this->tokens[$i]['bracket_opener'] = $opener; Chris@17: $this->tokens[$i]['bracket_closer'] = $i; Chris@17: $this->tokens[$opener]['bracket_opener'] = $opener; Chris@17: $this->tokens[$opener]['bracket_closer'] = $i; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", count($squareOpeners)); Chris@17: echo str_repeat("\t", count($curlyOpeners)); Chris@17: echo "\t=> Found square bracket closer at $i for $opener".PHP_EOL; Chris@17: } Chris@17: } Chris@17: break; Chris@17: case T_CLOSE_CURLY_BRACKET: Chris@17: if (empty($curlyOpeners) === false Chris@17: && isset($this->tokens[$i]['scope_opener']) === false Chris@17: ) { Chris@17: $opener = array_pop($curlyOpeners); Chris@17: $this->tokens[$i]['bracket_opener'] = $opener; Chris@17: $this->tokens[$i]['bracket_closer'] = $i; Chris@17: $this->tokens[$opener]['bracket_opener'] = $opener; Chris@17: $this->tokens[$opener]['bracket_closer'] = $i; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", count($squareOpeners)); Chris@17: echo str_repeat("\t", count($curlyOpeners)); Chris@17: echo "\t=> Found curly bracket closer at $i for $opener".PHP_EOL; Chris@17: } Chris@17: } Chris@17: break; Chris@17: default: Chris@17: continue 2; Chris@17: }//end switch Chris@17: }//end for Chris@17: Chris@17: // Cleanup for any openers that we didn't find closers for. Chris@17: // This typically means there was a syntax error breaking things. Chris@17: foreach ($openers as $opener) { Chris@17: unset($this->tokens[$opener]['parenthesis_opener']); Chris@17: unset($this->tokens[$opener]['parenthesis_owner']); Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** END TOKEN MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: }//end createTokenMap() Chris@17: Chris@17: Chris@17: /** Chris@17: * Creates a map for the parenthesis tokens that surround other tokens. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: private function createParenthesisNestingMap() Chris@17: { Chris@17: $map = []; Chris@17: for ($i = 0; $i < $this->numTokens; $i++) { Chris@17: if (isset($this->tokens[$i]['parenthesis_opener']) === true Chris@17: && $i === $this->tokens[$i]['parenthesis_opener'] Chris@17: ) { Chris@17: if (empty($map) === false) { Chris@17: $this->tokens[$i]['nested_parenthesis'] = $map; Chris@17: } Chris@17: Chris@17: if (isset($this->tokens[$i]['parenthesis_closer']) === true) { Chris@17: $map[$this->tokens[$i]['parenthesis_opener']] Chris@17: = $this->tokens[$i]['parenthesis_closer']; Chris@17: } Chris@17: } else if (isset($this->tokens[$i]['parenthesis_closer']) === true Chris@17: && $i === $this->tokens[$i]['parenthesis_closer'] Chris@17: ) { Chris@17: array_pop($map); Chris@17: if (empty($map) === false) { Chris@17: $this->tokens[$i]['nested_parenthesis'] = $map; Chris@17: } Chris@17: } else { Chris@17: if (empty($map) === false) { Chris@17: $this->tokens[$i]['nested_parenthesis'] = $map; Chris@17: } Chris@17: }//end if Chris@17: }//end for Chris@17: Chris@17: }//end createParenthesisNestingMap() Chris@17: Chris@17: Chris@17: /** Chris@17: * Creates a scope map of tokens that open scopes. Chris@17: * Chris@17: * @return void Chris@17: * @see recurseScopeMap() Chris@17: */ Chris@17: private function createScopeMap() Chris@17: { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** START SCOPE MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: for ($i = 0; $i < $this->numTokens; $i++) { Chris@17: // Check to see if the current token starts a new scope. Chris@17: if (isset($this->scopeOpeners[$this->tokens[$i]['code']]) === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$i]['type']; Chris@17: $content = Util\Common::prepareForOutput($this->tokens[$i]['content']); Chris@17: echo "\tStart scope map at $i:$type => $content".PHP_EOL; Chris@17: } Chris@17: Chris@17: if (isset($this->tokens[$i]['scope_condition']) === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t* already processed, skipping *".PHP_EOL; Chris@17: } Chris@17: Chris@17: continue; Chris@17: } Chris@17: Chris@17: $i = $this->recurseScopeMap($i); Chris@17: }//end if Chris@17: }//end for Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** END SCOPE MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: }//end createScopeMap() Chris@17: Chris@17: Chris@17: /** Chris@17: * Recurses though the scope openers to build a scope map. Chris@17: * Chris@17: * @param int $stackPtr The position in the stack of the token that Chris@17: * opened the scope (eg. an IF token or FOR token). Chris@17: * @param int $depth How many scope levels down we are. Chris@17: * @param int $ignore How many curly braces we are ignoring. Chris@17: * Chris@17: * @return int The position in the stack that closed the scope. Chris@17: */ Chris@17: private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0) Chris@17: { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Begin scope map recursion at token $stackPtr with depth $depth".PHP_EOL; Chris@17: } Chris@17: Chris@17: $opener = null; Chris@17: $currType = $this->tokens[$stackPtr]['code']; Chris@17: $startLine = $this->tokens[$stackPtr]['line']; Chris@17: Chris@17: // We will need this to restore the value if we end up Chris@17: // returning a token ID that causes our calling function to go back Chris@17: // over already ignored braces. Chris@17: $originalIgnore = $ignore; Chris@17: Chris@17: // If the start token for this scope opener is the same as Chris@17: // the scope token, we have already found our opener. Chris@17: if (isset($this->scopeOpeners[$currType]['start'][$currType]) === true) { Chris@17: $opener = $stackPtr; Chris@17: } Chris@17: Chris@17: for ($i = ($stackPtr + 1); $i < $this->numTokens; $i++) { Chris@17: $tokenType = $this->tokens[$i]['code']; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$i]['type']; Chris@17: $line = $this->tokens[$i]['line']; Chris@17: $content = Util\Common::prepareForOutput($this->tokens[$i]['content']); Chris@17: Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "Process token $i on line $line ["; Chris@17: if ($opener !== null) { Chris@17: echo "opener:$opener;"; Chris@17: } Chris@17: Chris@17: if ($ignore > 0) { Chris@17: echo "ignore=$ignore;"; Chris@17: } Chris@17: Chris@17: echo "]: $type => $content".PHP_EOL; Chris@17: }//end if Chris@17: Chris@17: // Very special case for IF statements in PHP that can be defined without Chris@17: // scope tokens. E.g., if (1) 1; 1 ? (1 ? 1 : 1) : 1; Chris@17: // If an IF statement below this one has an opener but no Chris@17: // keyword, the opener will be incorrectly assigned to this IF statement. Chris@17: // The same case also applies to USE statements, which don't have to have Chris@17: // openers, so a following USE statement can cause an incorrect brace match. Chris@17: if (($currType === T_IF || $currType === T_ELSE || $currType === T_USE) Chris@17: && $opener === null Chris@17: && ($this->tokens[$i]['code'] === T_SEMICOLON Chris@17: || $this->tokens[$i]['code'] === T_CLOSE_TAG) Chris@17: ) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: if ($this->tokens[$i]['code'] === T_SEMICOLON) { Chris@17: $closerType = 'semicolon'; Chris@17: } else { Chris@17: $closerType = 'close tag'; Chris@17: } Chris@17: Chris@17: echo "=> Found $closerType before scope opener for $stackPtr:$type, bailing".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $i; Chris@17: } Chris@17: Chris@17: // Special case for PHP control structures that have no braces. Chris@17: // If we find a curly brace closer before we find the opener, Chris@17: // we're not going to find an opener. That closer probably belongs to Chris@17: // a control structure higher up. Chris@17: if ($opener === null Chris@17: && $ignore === 0 Chris@17: && $tokenType === T_CLOSE_CURLY_BRACKET Chris@17: && isset($this->scopeOpeners[$currType]['end'][$tokenType]) === true Chris@17: ) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found curly brace closer before scope opener for $stackPtr:$type, bailing".PHP_EOL; Chris@17: } Chris@17: Chris@17: return ($i - 1); Chris@17: } Chris@17: Chris@17: if ($opener !== null Chris@17: && (isset($this->tokens[$i]['scope_opener']) === false Chris@17: || $this->scopeOpeners[$this->tokens[$stackPtr]['code']]['shared'] === true) Chris@17: && isset($this->scopeOpeners[$currType]['end'][$tokenType]) === true Chris@17: ) { Chris@17: if ($ignore > 0 && $tokenType === T_CLOSE_CURLY_BRACKET) { Chris@17: // The last opening bracket must have been for a string Chris@17: // offset or alike, so let's ignore it. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* finished ignoring curly brace *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore--; Chris@17: continue; Chris@17: } else if ($this->tokens[$opener]['code'] === T_OPEN_CURLY_BRACKET Chris@17: && $tokenType !== T_CLOSE_CURLY_BRACKET Chris@17: ) { Chris@17: // The opener is a curly bracket so the closer must be a curly bracket as well. Chris@17: // We ignore this closer to handle cases such as T_ELSE or T_ELSEIF being considered Chris@17: // a closer of T_IF when it should not. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Ignoring non-curly scope closer for $stackPtr:$type".PHP_EOL; Chris@17: } Chris@17: } else { Chris@17: $scopeCloser = $i; Chris@17: $todo = [ Chris@17: $stackPtr, Chris@17: $opener, Chris@17: ]; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: $closerType = $this->tokens[$scopeCloser]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found scope closer ($scopeCloser:$closerType) for $stackPtr:$type".PHP_EOL; Chris@17: } Chris@17: Chris@17: $validCloser = true; Chris@17: if (($this->tokens[$stackPtr]['code'] === T_IF || $this->tokens[$stackPtr]['code'] === T_ELSEIF) Chris@17: && ($tokenType === T_ELSE || $tokenType === T_ELSEIF) Chris@17: ) { Chris@17: // To be a closer, this token must have an opener. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "* closer needs to be tested *".PHP_EOL; Chris@17: } Chris@17: Chris@17: $i = self::recurseScopeMap($i, ($depth + 1), $ignore); Chris@17: Chris@17: if (isset($this->tokens[$scopeCloser]['scope_opener']) === false) { Chris@17: $validCloser = false; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "* closer is not valid (no opener found) *".PHP_EOL; Chris@17: } Chris@17: } else if ($this->tokens[$this->tokens[$scopeCloser]['scope_opener']]['code'] !== $this->tokens[$opener]['code']) { Chris@17: $validCloser = false; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: $type = $this->tokens[$this->tokens[$scopeCloser]['scope_opener']]['type']; Chris@17: $openerType = $this->tokens[$opener]['type']; Chris@17: echo "* closer is not valid (mismatched opener type; $type != $openerType) *".PHP_EOL; Chris@17: } Chris@17: } else if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "* closer was valid *".PHP_EOL; Chris@17: } Chris@17: } else { Chris@17: // The closer was not processed, so we need to Chris@17: // complete that token as well. Chris@17: $todo[] = $scopeCloser; Chris@17: }//end if Chris@17: Chris@17: if ($validCloser === true) { Chris@17: foreach ($todo as $token) { Chris@17: $this->tokens[$token]['scope_condition'] = $stackPtr; Chris@17: $this->tokens[$token]['scope_opener'] = $opener; Chris@17: $this->tokens[$token]['scope_closer'] = $scopeCloser; Chris@17: } Chris@17: Chris@17: if ($this->scopeOpeners[$this->tokens[$stackPtr]['code']]['shared'] === true) { Chris@17: // As we are going back to where we started originally, restore Chris@17: // the ignore value back to its original value. Chris@17: $ignore = $originalIgnore; Chris@17: return $opener; Chris@17: } else if ($scopeCloser === $i Chris@17: && isset($this->scopeOpeners[$tokenType]) === true Chris@17: ) { Chris@17: // Unset scope_condition here or else the token will appear to have Chris@17: // already been processed, and it will be skipped. Normally we want that, Chris@17: // but in this case, the token is both a closer and an opener, so Chris@17: // it needs to act like an opener. This is also why we return the Chris@17: // token before this one; so the closer has a chance to be processed Chris@17: // a second time, but as an opener. Chris@17: unset($this->tokens[$scopeCloser]['scope_condition']); Chris@17: return ($i - 1); Chris@17: } else { Chris@17: return $i; Chris@17: } Chris@17: } else { Chris@17: continue; Chris@17: }//end if Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: // Is this an opening condition ? Chris@17: if (isset($this->scopeOpeners[$tokenType]) === true) { Chris@17: if ($opener === null) { Chris@17: if ($tokenType === T_USE) { Chris@17: // PHP use keywords are special because they can be Chris@17: // used as blocks but also inline in function definitions. Chris@17: // So if we find them nested inside another opener, just skip them. Chris@17: continue; Chris@17: } Chris@17: Chris@17: if ($tokenType === T_FUNCTION Chris@17: && $this->tokens[$stackPtr]['code'] !== T_FUNCTION Chris@17: ) { Chris@17: // Probably a closure, so process it manually. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found function before scope opener for $stackPtr:$type, processing manually".PHP_EOL; Chris@17: } Chris@17: Chris@17: if (isset($this->tokens[$i]['scope_closer']) === true) { Chris@17: // We've already processed this closure. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* already processed, skipping *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $i = $this->tokens[$i]['scope_closer']; Chris@17: continue; Chris@17: } Chris@17: Chris@17: $i = self::recurseScopeMap($i, ($depth + 1), $ignore); Chris@17: continue; Chris@17: }//end if Chris@17: Chris@17: if ($tokenType === T_CLASS) { Chris@17: // Probably an anonymous class inside another anonymous class, Chris@17: // so process it manually. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found class before scope opener for $stackPtr:$type, processing manually".PHP_EOL; Chris@17: } Chris@17: Chris@17: if (isset($this->tokens[$i]['scope_closer']) === true) { Chris@17: // We've already processed this anon class. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* already processed, skipping *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $i = $this->tokens[$i]['scope_closer']; Chris@17: continue; Chris@17: } Chris@17: Chris@17: $i = self::recurseScopeMap($i, ($depth + 1), $ignore); Chris@17: continue; Chris@17: }//end if Chris@17: Chris@17: // Found another opening condition but still haven't Chris@17: // found our opener, so we are never going to find one. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found new opening condition before scope opener for $stackPtr:$type, "; Chris@17: } Chris@17: Chris@17: if (($this->tokens[$stackPtr]['code'] === T_IF Chris@17: || $this->tokens[$stackPtr]['code'] === T_ELSEIF Chris@17: || $this->tokens[$stackPtr]['code'] === T_ELSE) Chris@17: && ($this->tokens[$i]['code'] === T_ELSE Chris@17: || $this->tokens[$i]['code'] === T_ELSEIF) Chris@17: ) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "continuing".PHP_EOL; Chris@17: } Chris@17: Chris@17: return ($i - 1); Chris@17: } else { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "backtracking".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $stackPtr; Chris@17: } Chris@17: }//end if Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* token is an opening condition *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $isShared = ($this->scopeOpeners[$tokenType]['shared'] === true); Chris@17: Chris@17: if (isset($this->tokens[$i]['scope_condition']) === true) { Chris@17: // We've been here before. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* already processed, skipping *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: if ($isShared === false Chris@17: && isset($this->tokens[$i]['scope_closer']) === true Chris@17: ) { Chris@17: $i = $this->tokens[$i]['scope_closer']; Chris@17: } Chris@17: Chris@17: continue; Chris@17: } else if ($currType === $tokenType Chris@17: && $isShared === false Chris@17: && $opener === null Chris@17: ) { Chris@17: // We haven't yet found our opener, but we have found another Chris@17: // scope opener which is the same type as us, and we don't Chris@17: // share openers, so we will never find one. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* it was another token\'s opener, bailing *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: return $stackPtr; Chris@17: } else { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* searching for opener *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: if (isset($this->scopeOpeners[$tokenType]['end'][T_CLOSE_CURLY_BRACKET]) === true) { Chris@17: $oldIgnore = $ignore; Chris@17: $ignore = 0; Chris@17: } Chris@17: Chris@17: // PHP has a max nesting level for functions. Stop before we hit that limit Chris@17: // because too many loops means we've run into trouble anyway. Chris@17: if ($depth > 50) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* reached maximum nesting level; aborting *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: throw new RuntimeException('Maximum nesting level reached; file could not be processed'); Chris@17: } Chris@17: Chris@17: $oldDepth = $depth; Chris@17: if ($isShared === true Chris@17: && isset($this->scopeOpeners[$tokenType]['with'][$currType]) === true Chris@17: ) { Chris@17: // Don't allow the depth to increment because this is Chris@17: // possibly not a true nesting if we are sharing our closer. Chris@17: // This can happen, for example, when a SWITCH has a large Chris@17: // number of CASE statements with the same shared BREAK. Chris@17: $depth--; Chris@17: } Chris@17: Chris@17: $i = self::recurseScopeMap($i, ($depth + 1), $ignore); Chris@17: $depth = $oldDepth; Chris@17: Chris@17: if (isset($this->scopeOpeners[$tokenType]['end'][T_CLOSE_CURLY_BRACKET]) === true) { Chris@17: $ignore = $oldIgnore; Chris@17: } Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: if (isset($this->scopeOpeners[$currType]['start'][$tokenType]) === true Chris@17: && $opener === null Chris@17: ) { Chris@17: if ($tokenType === T_OPEN_CURLY_BRACKET) { Chris@17: if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true Chris@17: && $i < $this->tokens[$stackPtr]['parenthesis_closer'] Chris@17: ) { Chris@17: // We found a curly brace inside the condition of the Chris@17: // current scope opener, so it must be a string offset. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* ignoring curly brace inside condition *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore++; Chris@17: } else { Chris@17: // Make sure this is actually an opener and not a Chris@17: // string offset (e.g., $var{0}). Chris@17: for ($x = ($i - 1); $x > 0; $x--) { Chris@17: if (isset(Util\Tokens::$emptyTokens[$this->tokens[$x]['code']]) === true) { Chris@17: continue; Chris@17: } else { Chris@17: // If the first non-whitespace/comment token looks like this Chris@17: // brace is a string offset, or this brace is mid-way through Chris@17: // a new statement, it isn't a scope opener. Chris@17: $disallowed = Util\Tokens::$assignmentTokens; Chris@17: $disallowed += [ Chris@17: T_DOLLAR => true, Chris@17: T_VARIABLE => true, Chris@17: T_OBJECT_OPERATOR => true, Chris@17: T_COMMA => true, Chris@17: T_OPEN_PARENTHESIS => true, Chris@17: ]; Chris@17: Chris@17: if (isset($disallowed[$this->tokens[$x]['code']]) === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* ignoring curly brace *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore++; Chris@17: } Chris@17: Chris@17: break; Chris@17: }//end if Chris@17: }//end for Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: if ($ignore === 0 || $tokenType !== T_OPEN_CURLY_BRACKET) { Chris@17: // We found the opening scope token for $currType. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found scope opener for $stackPtr:$type".PHP_EOL; Chris@17: } Chris@17: Chris@17: $opener = $i; Chris@17: } Chris@17: } else if ($tokenType === T_OPEN_PARENTHESIS) { Chris@17: if (isset($this->tokens[$i]['parenthesis_owner']) === true) { Chris@17: $owner = $this->tokens[$i]['parenthesis_owner']; Chris@17: if (isset(Util\Tokens::$scopeOpeners[$this->tokens[$owner]['code']]) === true Chris@17: && isset($this->tokens[$i]['parenthesis_closer']) === true Chris@17: ) { Chris@17: // If we get into here, then we opened a parenthesis for Chris@17: // a scope (eg. an if or else if) so we need to update the Chris@17: // start of the line so that when we check to see Chris@17: // if the closing parenthesis is more than 3 lines away from Chris@17: // the statement, we check from the closing parenthesis. Chris@17: $startLine = $this->tokens[$this->tokens[$i]['parenthesis_closer']]['line']; Chris@17: } Chris@17: } Chris@17: } else if ($tokenType === T_OPEN_CURLY_BRACKET && $opener !== null) { Chris@17: // We opened something that we don't have a scope opener for. Chris@17: // Examples of this are curly brackets for string offsets etc. Chris@17: // We want to ignore this so that we don't have an invalid scope Chris@17: // map. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* ignoring curly brace *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore++; Chris@17: } else if ($tokenType === T_CLOSE_CURLY_BRACKET && $ignore > 0) { Chris@17: // We found the end token for the opener we were ignoring. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* finished ignoring curly brace *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore--; Chris@17: } else if ($opener === null Chris@17: && isset($this->scopeOpeners[$currType]) === true Chris@17: ) { Chris@17: // If we still haven't found the opener after 30 lines, Chris@17: // we're not going to find it, unless we know it requires Chris@17: // an opener (in which case we better keep looking) or the last Chris@17: // token was empty (in which case we'll just confirm there is Chris@17: // more code in this file and not just a big comment). Chris@17: if ($this->tokens[$i]['line'] >= ($startLine + 30) Chris@17: && isset(Util\Tokens::$emptyTokens[$this->tokens[($i - 1)]['code']]) === false Chris@17: ) { Chris@17: if ($this->scopeOpeners[$currType]['strict'] === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: $lines = ($this->tokens[$i]['line'] - $startLine); Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Still looking for $stackPtr:$type scope opener after $lines lines".PHP_EOL; Chris@17: } Chris@17: } else { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Couldn't find scope opener for $stackPtr:$type, bailing".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $stackPtr; Chris@17: } Chris@17: } Chris@17: } else if ($opener !== null Chris@17: && $tokenType !== T_BREAK Chris@17: && isset($this->endScopeTokens[$tokenType]) === true Chris@17: ) { Chris@17: if (isset($this->tokens[$i]['scope_condition']) === false) { Chris@17: if ($ignore > 0) { Chris@17: // We found the end token for the opener we were ignoring. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo '* finished ignoring curly brace *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ignore--; Chris@17: } else { Chris@17: // We found a token that closes the scope but it doesn't Chris@17: // have a condition, so it belongs to another token and Chris@17: // our token doesn't have a closer, so pretend this is Chris@17: // the closer. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Found (unexpected) scope closer for $stackPtr:$type".PHP_EOL; Chris@17: } Chris@17: Chris@17: foreach ([$stackPtr, $opener] as $token) { Chris@17: $this->tokens[$token]['scope_condition'] = $stackPtr; Chris@17: $this->tokens[$token]['scope_opener'] = $opener; Chris@17: $this->tokens[$token]['scope_closer'] = $i; Chris@17: } Chris@17: Chris@17: return ($i - 1); Chris@17: }//end if Chris@17: }//end if Chris@17: }//end if Chris@17: }//end for Chris@17: Chris@17: return $stackPtr; Chris@17: Chris@17: }//end recurseScopeMap() Chris@17: Chris@17: Chris@17: /** Chris@17: * Constructs the level map. Chris@17: * Chris@17: * The level map adds a 'level' index to each token which indicates the Chris@17: * depth that a token within a set of scope blocks. It also adds a Chris@17: * 'conditions' index which is an array of the scope conditions that opened Chris@17: * each of the scopes - position 0 being the first scope opener. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: private function createLevelMap() Chris@17: { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** START LEVEL MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: $this->numTokens = count($this->tokens); Chris@17: $level = 0; Chris@17: $conditions = []; Chris@17: $lastOpener = null; Chris@17: $openers = []; Chris@17: Chris@17: for ($i = 0; $i < $this->numTokens; $i++) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$i]['type']; Chris@17: $line = $this->tokens[$i]['line']; Chris@17: $len = $this->tokens[$i]['length']; Chris@17: $col = $this->tokens[$i]['column']; Chris@17: Chris@17: $content = Util\Common::prepareForOutput($this->tokens[$i]['content']); Chris@17: Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "Process token $i on line $line [col:$col;len:$len;lvl:$level;"; Chris@17: if (empty($conditions) !== true) { Chris@17: $condString = 'conds;'; Chris@17: foreach ($conditions as $condition) { Chris@17: $condString .= Util\Tokens::tokenName($condition).','; Chris@17: } Chris@17: Chris@17: echo rtrim($condString, ',').';'; Chris@17: } Chris@17: Chris@17: echo "]: $type => $content".PHP_EOL; Chris@17: }//end if Chris@17: Chris@17: $this->tokens[$i]['level'] = $level; Chris@17: $this->tokens[$i]['conditions'] = $conditions; Chris@17: Chris@17: if (isset($this->tokens[$i]['scope_condition']) === true) { Chris@17: // Check to see if this token opened the scope. Chris@17: if ($this->tokens[$i]['scope_opener'] === $i) { Chris@17: $stackPtr = $this->tokens[$i]['scope_condition']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "=> Found scope opener for $stackPtr:$type".PHP_EOL; Chris@17: } Chris@17: Chris@17: $stackPtr = $this->tokens[$i]['scope_condition']; Chris@17: Chris@17: // If we find a scope opener that has a shared closer, Chris@17: // then we need to go back over the condition map that we Chris@17: // just created and fix ourselves as we just added some Chris@17: // conditions where there was none. This happens for T_CASE Chris@17: // statements that are using the same break statement. Chris@17: if ($lastOpener !== null && $this->tokens[$lastOpener]['scope_closer'] === $this->tokens[$i]['scope_closer']) { Chris@17: // This opener shares its closer with the previous opener, Chris@17: // but we still need to check if the two openers share their Chris@17: // closer with each other directly (like CASE and DEFAULT) Chris@17: // or if they are just sharing because one doesn't have a Chris@17: // closer (like CASE with no BREAK using a SWITCHes closer). Chris@17: $thisType = $this->tokens[$this->tokens[$i]['scope_condition']]['code']; Chris@17: $opener = $this->tokens[$lastOpener]['scope_condition']; Chris@17: Chris@17: $isShared = isset($this->scopeOpeners[$thisType]['with'][$this->tokens[$opener]['code']]); Chris@17: Chris@17: reset($this->scopeOpeners[$thisType]['end']); Chris@17: reset($this->scopeOpeners[$this->tokens[$opener]['code']]['end']); Chris@17: $sameEnd = (current($this->scopeOpeners[$thisType]['end']) === current($this->scopeOpeners[$this->tokens[$opener]['code']]['end'])); Chris@17: Chris@17: if ($isShared === true && $sameEnd === true) { Chris@17: $badToken = $opener; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$badToken]['type']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* shared closer, cleaning up $badToken:$type *".PHP_EOL; Chris@17: } Chris@17: Chris@17: for ($x = $this->tokens[$i]['scope_condition']; $x <= $i; $x++) { Chris@17: $oldConditions = $this->tokens[$x]['conditions']; Chris@17: $oldLevel = $this->tokens[$x]['level']; Chris@17: $this->tokens[$x]['level']--; Chris@17: unset($this->tokens[$x]['conditions'][$badToken]); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$x]['type']; Chris@17: $oldConds = ''; Chris@17: foreach ($oldConditions as $condition) { Chris@17: $oldConds .= Util\Tokens::tokenName($condition).','; Chris@17: } Chris@17: Chris@17: $oldConds = rtrim($oldConds, ','); Chris@17: Chris@17: $newConds = ''; Chris@17: foreach ($this->tokens[$x]['conditions'] as $condition) { Chris@17: $newConds .= Util\Tokens::tokenName($condition).','; Chris@17: } Chris@17: Chris@17: $newConds = rtrim($newConds, ','); Chris@17: Chris@17: $newLevel = $this->tokens[$x]['level']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* cleaned $x:$type *".PHP_EOL; Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo "=> level changed from $oldLevel to $newLevel".PHP_EOL; Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo "=> conditions changed from $oldConds to $newConds".PHP_EOL; Chris@17: }//end if Chris@17: }//end for Chris@17: Chris@17: unset($conditions[$badToken]); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$badToken]['type']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* token $badToken:$type removed from conditions array *".PHP_EOL; Chris@17: } Chris@17: Chris@17: unset($openers[$lastOpener]); Chris@17: Chris@17: $level--; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo '* level decreased *'.PHP_EOL; Chris@17: } Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: $level++; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo '* level increased *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $conditions[$stackPtr] = $this->tokens[$stackPtr]['code']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$stackPtr]['type']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* token $stackPtr:$type added to conditions array *".PHP_EOL; Chris@17: } Chris@17: Chris@17: $lastOpener = $this->tokens[$i]['scope_opener']; Chris@17: if ($lastOpener !== null) { Chris@17: $openers[$lastOpener] = $lastOpener; Chris@17: } Chris@17: } else if ($lastOpener !== null && $this->tokens[$lastOpener]['scope_closer'] === $i) { Chris@17: foreach (array_reverse($openers) as $opener) { Chris@17: if ($this->tokens[$opener]['scope_closer'] === $i) { Chris@17: $oldOpener = array_pop($openers); Chris@17: if (empty($openers) === false) { Chris@17: $lastOpener = array_pop($openers); Chris@17: $openers[$lastOpener] = $lastOpener; Chris@17: } else { Chris@17: $lastOpener = null; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$oldOpener]['type']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "=> Found scope closer for $oldOpener:$type".PHP_EOL; Chris@17: } Chris@17: Chris@17: $oldCondition = array_pop($conditions); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo '* token '.Util\Tokens::tokenName($oldCondition).' removed from conditions array *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: // Make sure this closer actually belongs to us. Chris@17: // Either the condition also has to think this is the Chris@17: // closer, or it has to allow sharing with us. Chris@17: $condition = $this->tokens[$this->tokens[$i]['scope_condition']]['code']; Chris@17: if ($condition !== $oldCondition) { Chris@17: if (isset($this->scopeOpeners[$oldCondition]['with'][$condition]) === false) { Chris@17: $badToken = $this->tokens[$oldOpener]['scope_condition']; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = Util\Tokens::tokenName($oldCondition); Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* scope closer was bad, cleaning up $badToken:$type *".PHP_EOL; Chris@17: } Chris@17: Chris@17: for ($x = ($oldOpener + 1); $x <= $i; $x++) { Chris@17: $oldConditions = $this->tokens[$x]['conditions']; Chris@17: $oldLevel = $this->tokens[$x]['level']; Chris@17: $this->tokens[$x]['level']--; Chris@17: unset($this->tokens[$x]['conditions'][$badToken]); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $type = $this->tokens[$x]['type']; Chris@17: $oldConds = ''; Chris@17: foreach ($oldConditions as $condition) { Chris@17: $oldConds .= Util\Tokens::tokenName($condition).','; Chris@17: } Chris@17: Chris@17: $oldConds = rtrim($oldConds, ','); Chris@17: Chris@17: $newConds = ''; Chris@17: foreach ($this->tokens[$x]['conditions'] as $condition) { Chris@17: $newConds .= Util\Tokens::tokenName($condition).','; Chris@17: } Chris@17: Chris@17: $newConds = rtrim($newConds, ','); Chris@17: Chris@17: $newLevel = $this->tokens[$x]['level']; Chris@17: echo str_repeat("\t", ($level + 1)); Chris@17: echo "* cleaned $x:$type *".PHP_EOL; Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo "=> level changed from $oldLevel to $newLevel".PHP_EOL; Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo "=> conditions changed from $oldConds to $newConds".PHP_EOL; Chris@17: }//end if Chris@17: }//end for Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: $level--; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", ($level + 2)); Chris@17: echo '* level decreased *'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $this->tokens[$i]['level'] = $level; Chris@17: $this->tokens[$i]['conditions'] = $conditions; Chris@17: }//end if Chris@17: }//end foreach Chris@17: }//end if Chris@17: }//end if Chris@17: }//end for Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** END LEVEL MAP ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: }//end createLevelMap() Chris@17: Chris@17: Chris@17: }//end class