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\Files;
Chris@17:
Chris@17: use PHP_CodeSniffer\Ruleset;
Chris@17: use PHP_CodeSniffer\Config;
Chris@17: use PHP_CodeSniffer\Fixer;
Chris@17: use PHP_CodeSniffer\Util;
Chris@17: use PHP_CodeSniffer\Exceptions\RuntimeException;
Chris@17: use PHP_CodeSniffer\Exceptions\TokenizerException;
Chris@17:
Chris@17: class File
Chris@17: {
Chris@17:
Chris@17: /**
Chris@17: * The absolute path to the file associated with this object.
Chris@17: *
Chris@17: * @var string
Chris@17: */
Chris@17: public $path = '';
Chris@17:
Chris@17: /**
Chris@18: * The content of the file.
Chris@17: *
Chris@17: * @var string
Chris@17: */
Chris@17: protected $content = '';
Chris@17:
Chris@17: /**
Chris@17: * The config data for the run.
Chris@17: *
Chris@17: * @var \PHP_CodeSniffer\Config
Chris@17: */
Chris@17: public $config = null;
Chris@17:
Chris@17: /**
Chris@17: * The ruleset used for the run.
Chris@17: *
Chris@17: * @var \PHP_CodeSniffer\Ruleset
Chris@17: */
Chris@17: public $ruleset = null;
Chris@17:
Chris@17: /**
Chris@17: * If TRUE, the entire file is being ignored.
Chris@17: *
Chris@17: * @var boolean
Chris@17: */
Chris@17: public $ignored = false;
Chris@17:
Chris@17: /**
Chris@17: * The EOL character this file uses.
Chris@17: *
Chris@17: * @var string
Chris@17: */
Chris@17: public $eolChar = '';
Chris@17:
Chris@17: /**
Chris@17: * The Fixer object to control fixing errors.
Chris@17: *
Chris@17: * @var \PHP_CodeSniffer\Fixer
Chris@17: */
Chris@17: public $fixer = null;
Chris@17:
Chris@17: /**
Chris@17: * The tokenizer being used for this file.
Chris@17: *
Chris@17: * @var \PHP_CodeSniffer\Tokenizers\Tokenizer
Chris@17: */
Chris@17: public $tokenizer = null;
Chris@17:
Chris@17: /**
Chris@17: * The name of the tokenizer being used for this file.
Chris@17: *
Chris@17: * @var string
Chris@17: */
Chris@17: public $tokenizerType = 'PHP';
Chris@17:
Chris@17: /**
Chris@17: * Was the file loaded from cache?
Chris@17: *
Chris@17: * If TRUE, the file was loaded from a local cache.
Chris@17: * If FALSE, the file was tokenized and processed fully.
Chris@17: *
Chris@17: * @var boolean
Chris@17: */
Chris@17: public $fromCache = false;
Chris@17:
Chris@17: /**
Chris@17: * The number of tokens in this file.
Chris@17: *
Chris@17: * Stored here to save calling count() everywhere.
Chris@17: *
Chris@17: * @var integer
Chris@17: */
Chris@17: public $numTokens = 0;
Chris@17:
Chris@17: /**
Chris@17: * The tokens stack map.
Chris@17: *
Chris@17: * @var array
Chris@17: */
Chris@17: protected $tokens = [];
Chris@17:
Chris@17: /**
Chris@17: * The errors raised from sniffs.
Chris@17: *
Chris@17: * @var array
Chris@17: * @see getErrors()
Chris@17: */
Chris@17: protected $errors = [];
Chris@17:
Chris@17: /**
Chris@17: * The warnings raised from sniffs.
Chris@17: *
Chris@17: * @var array
Chris@17: * @see getWarnings()
Chris@17: */
Chris@17: protected $warnings = [];
Chris@17:
Chris@17: /**
Chris@17: * The metrics recorded by sniffs.
Chris@17: *
Chris@17: * @var array
Chris@17: * @see getMetrics()
Chris@17: */
Chris@17: protected $metrics = [];
Chris@17:
Chris@17: /**
Chris@17: * The metrics recorded for each token.
Chris@17: *
Chris@17: * Stops the same metric being recorded for the same token twice.
Chris@17: *
Chris@17: * @var array
Chris@17: * @see getMetrics()
Chris@17: */
Chris@17: private $metricTokens = [];
Chris@17:
Chris@17: /**
Chris@17: * The total number of errors raised.
Chris@17: *
Chris@17: * @var integer
Chris@17: */
Chris@17: protected $errorCount = 0;
Chris@17:
Chris@17: /**
Chris@17: * The total number of warnings raised.
Chris@17: *
Chris@17: * @var integer
Chris@17: */
Chris@17: protected $warningCount = 0;
Chris@17:
Chris@17: /**
Chris@17: * The total number of errors and warnings that can be fixed.
Chris@17: *
Chris@17: * @var integer
Chris@17: */
Chris@17: protected $fixableCount = 0;
Chris@17:
Chris@17: /**
Chris@17: * The total number of errors and warnings that were fixed.
Chris@17: *
Chris@17: * @var integer
Chris@17: */
Chris@17: protected $fixedCount = 0;
Chris@17:
Chris@17: /**
Chris@17: * An array of sniffs that are being ignored.
Chris@17: *
Chris@17: * @var array
Chris@17: */
Chris@17: protected $ignoredListeners = [];
Chris@17:
Chris@17: /**
Chris@17: * An array of message codes that are being ignored.
Chris@17: *
Chris@17: * @var array
Chris@17: */
Chris@17: protected $ignoredCodes = [];
Chris@17:
Chris@17: /**
Chris@17: * An array of sniffs listening to this file's processing.
Chris@17: *
Chris@17: * @var \PHP_CodeSniffer\Sniffs\Sniff[]
Chris@17: */
Chris@17: protected $listeners = [];
Chris@17:
Chris@17: /**
Chris@17: * The class name of the sniff currently processing the file.
Chris@17: *
Chris@17: * @var string
Chris@17: */
Chris@17: protected $activeListener = '';
Chris@17:
Chris@17: /**
Chris@17: * An array of sniffs being processed and how long they took.
Chris@17: *
Chris@17: * @var array
Chris@17: */
Chris@17: protected $listenerTimes = [];
Chris@17:
Chris@17: /**
Chris@17: * A cache of often used config settings to improve performance.
Chris@17: *
Chris@17: * Storing them here saves 10k+ calls to __get() in the Config class.
Chris@17: *
Chris@17: * @var array
Chris@17: */
Chris@17: protected $configCache = [];
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Constructs a file.
Chris@17: *
Chris@17: * @param string $path The absolute path to the file to process.
Chris@17: * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
Chris@17: * @param \PHP_CodeSniffer\Config $config The config data for the run.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function __construct($path, Ruleset $ruleset, Config $config)
Chris@17: {
Chris@17: $this->path = $path;
Chris@17: $this->ruleset = $ruleset;
Chris@17: $this->config = $config;
Chris@17: $this->fixer = new Fixer();
Chris@17:
Chris@17: $parts = explode('.', $path);
Chris@17: $extension = array_pop($parts);
Chris@17: if (isset($config->extensions[$extension]) === true) {
Chris@17: $this->tokenizerType = $config->extensions[$extension];
Chris@17: } else {
Chris@17: // Revert to default.
Chris@17: $this->tokenizerType = 'PHP';
Chris@17: }
Chris@17:
Chris@17: $this->configCache['cache'] = $this->config->cache;
Chris@17: $this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs);
Chris@17: $this->configCache['exclude'] = array_map('strtolower', $this->config->exclude);
Chris@17: $this->configCache['errorSeverity'] = $this->config->errorSeverity;
Chris@17: $this->configCache['warningSeverity'] = $this->config->warningSeverity;
Chris@17: $this->configCache['recordErrors'] = $this->config->recordErrors;
Chris@17: $this->configCache['ignorePatterns'] = $this->ruleset->ignorePatterns;
Chris@17: $this->configCache['includePatterns'] = $this->ruleset->includePatterns;
Chris@17:
Chris@17: }//end __construct()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Set the content of the file.
Chris@17: *
Chris@17: * Setting the content also calculates the EOL char being used.
Chris@17: *
Chris@17: * @param string $content The file content.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function setContent($content)
Chris@17: {
Chris@17: $this->content = $content;
Chris@17: $this->tokens = [];
Chris@17:
Chris@17: try {
Chris@17: $this->eolChar = Util\Common::detectLineEndings($content);
Chris@17: } catch (RuntimeException $e) {
Chris@17: $this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings');
Chris@17: return;
Chris@17: }
Chris@17:
Chris@17: }//end setContent()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Reloads the content of the file.
Chris@17: *
Chris@17: * By default, we have no idea where our content comes from,
Chris@17: * so we can't do anything.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function reloadContent()
Chris@17: {
Chris@17:
Chris@17: }//end reloadContent()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Disables caching of this file.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function disableCaching()
Chris@17: {
Chris@17: $this->configCache['cache'] = false;
Chris@17:
Chris@17: }//end disableCaching()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Starts the stack traversal and tells listeners when tokens are found.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function process()
Chris@17: {
Chris@17: if ($this->ignored === true) {
Chris@17: return;
Chris@17: }
Chris@17:
Chris@17: $this->errors = [];
Chris@17: $this->warnings = [];
Chris@17: $this->errorCount = 0;
Chris@17: $this->warningCount = 0;
Chris@17: $this->fixableCount = 0;
Chris@17:
Chris@17: $this->parse();
Chris@17:
Chris@17: // Check if tokenizer errors cause this file to be ignored.
Chris@17: if ($this->ignored === true) {
Chris@17: return;
Chris@17: }
Chris@17:
Chris@17: $this->fixer->startFile($this);
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) {
Chris@17: echo "\t*** START TOKEN PROCESSING ***".PHP_EOL;
Chris@17: }
Chris@17:
Chris@17: $foundCode = false;
Chris@17: $listenerIgnoreTo = [];
Chris@17: $inTests = defined('PHP_CODESNIFFER_IN_TESTS');
Chris@17: $checkAnnotations = $this->config->annotations;
Chris@17:
Chris@17: // Foreach of the listeners that have registered to listen for this
Chris@17: // token, get them to process it.
Chris@17: foreach ($this->tokens as $stackPtr => $token) {
Chris@17: // Check for ignored lines.
Chris@17: if ($checkAnnotations === true
Chris@17: && ($token['code'] === T_COMMENT
Chris@17: || $token['code'] === T_PHPCS_IGNORE_FILE
Chris@17: || $token['code'] === T_PHPCS_SET
Chris@17: || $token['code'] === T_DOC_COMMENT_STRING
Chris@17: || $token['code'] === T_DOC_COMMENT_TAG
Chris@17: || ($inTests === true && $token['code'] === T_INLINE_HTML))
Chris@17: ) {
Chris@17: $commentText = ltrim($this->tokens[$stackPtr]['content'], ' /*');
Chris@17: $commentTextLower = strtolower($commentText);
Chris@17: if (strpos($commentText, '@codingStandards') !== false) {
Chris@17: if (strpos($commentText, '@codingStandardsIgnoreFile') !== false) {
Chris@17: // Ignoring the whole file, just a little late.
Chris@17: $this->errors = [];
Chris@17: $this->warnings = [];
Chris@17: $this->errorCount = 0;
Chris@17: $this->warningCount = 0;
Chris@17: $this->fixableCount = 0;
Chris@17: return;
Chris@17: } else if (strpos($commentText, '@codingStandardsChangeSetting') !== false) {
Chris@17: $start = strpos($commentText, '@codingStandardsChangeSetting');
Chris@17: $comment = substr($commentText, ($start + 30));
Chris@17: $parts = explode(' ', $comment);
Chris@17: if (count($parts) >= 2) {
Chris@17: $sniffParts = explode('.', $parts[0]);
Chris@17: if (count($sniffParts) >= 3) {
Chris@17: // If the sniff code is not known to us, it has not been registered in this run.
Chris@17: // But don't throw an error as it could be there for a different standard to use.
Chris@17: if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) {
Chris@17: $listenerCode = array_shift($parts);
Chris@17: $propertyCode = array_shift($parts);
Chris@17: $propertyValue = rtrim(implode(' ', $parts), " */\r\n");
Chris@17: $listenerClass = $this->ruleset->sniffCodes[$listenerCode];
Chris@17: $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue);
Chris@17: }
Chris@17: }
Chris@17: }
Chris@17: }//end if
Chris@17: } else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile'
Chris@17: || substr($commentTextLower, 0, 17) === '@phpcs:ignorefile'
Chris@17: ) {
Chris@17: // Ignoring the whole file, just a little late.
Chris@17: $this->errors = [];
Chris@17: $this->warnings = [];
Chris@17: $this->errorCount = 0;
Chris@17: $this->warningCount = 0;
Chris@17: $this->fixableCount = 0;
Chris@17: return;
Chris@17: } else if (substr($commentTextLower, 0, 9) === 'phpcs:set'
Chris@17: || substr($commentTextLower, 0, 10) === '@phpcs:set'
Chris@17: ) {
Chris@18: if (isset($token['sniffCode']) === true) {
Chris@18: $listenerCode = $token['sniffCode'];
Chris@18: if (isset($this->ruleset->sniffCodes[$listenerCode]) === true) {
Chris@18: $propertyCode = $token['sniffProperty'];
Chris@18: $propertyValue = $token['sniffPropertyValue'];
Chris@18: $listenerClass = $this->ruleset->sniffCodes[$listenerCode];
Chris@18: $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue);
Chris@17: }
Chris@17: }
Chris@17: }//end if
Chris@17: }//end if
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) {
Chris@17: $type = $token['type'];
Chris@17: $content = Util\Common::prepareForOutput($token['content']);
Chris@17: echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL;
Chris@17: }
Chris@17:
Chris@17: if ($token['code'] !== T_INLINE_HTML) {
Chris@17: $foundCode = true;
Chris@17: }
Chris@17:
Chris@17: if (isset($this->ruleset->tokenListeners[$token['code']]) === false) {
Chris@17: continue;
Chris@17: }
Chris@17:
Chris@17: foreach ($this->ruleset->tokenListeners[$token['code']] as $listenerData) {
Chris@17: if (isset($this->ignoredListeners[$listenerData['class']]) === true
Chris@17: || (isset($listenerIgnoreTo[$listenerData['class']]) === true
Chris@17: && $listenerIgnoreTo[$listenerData['class']] > $stackPtr)
Chris@17: ) {
Chris@17: // This sniff is ignoring past this token, or the whole file.
Chris@17: continue;
Chris@17: }
Chris@17:
Chris@17: // Make sure this sniff supports the tokenizer
Chris@17: // we are currently using.
Chris@17: $class = $listenerData['class'];
Chris@17:
Chris@17: if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) {
Chris@17: continue;
Chris@17: }
Chris@17:
Chris@17: // If the file path matches one of our ignore patterns, skip it.
Chris@17: // While there is support for a type of each pattern
Chris@17: // (absolute or relative) we don't actually support it here.
Chris@17: foreach ($listenerData['ignore'] as $pattern) {
Chris@17: // We assume a / directory separator, as do the exclude rules
Chris@17: // most developers write, so we need a special case for any system
Chris@17: // that is different.
Chris@17: if (DIRECTORY_SEPARATOR === '\\') {
Chris@17: $pattern = str_replace('/', '\\\\', $pattern);
Chris@17: }
Chris@17:
Chris@17: $pattern = '`'.$pattern.'`i';
Chris@17: if (preg_match($pattern, $this->path) === 1) {
Chris@17: $this->ignoredListeners[$class] = true;
Chris@17: continue(2);
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: // If the file path does not match one of our include patterns, skip it.
Chris@17: // While there is support for a type of each pattern
Chris@17: // (absolute or relative) we don't actually support it here.
Chris@17: if (empty($listenerData['include']) === false) {
Chris@17: $included = false;
Chris@17: foreach ($listenerData['include'] as $pattern) {
Chris@17: // We assume a / directory separator, as do the exclude rules
Chris@17: // most developers write, so we need a special case for any system
Chris@17: // that is different.
Chris@17: if (DIRECTORY_SEPARATOR === '\\') {
Chris@17: $pattern = str_replace('/', '\\\\', $pattern);
Chris@17: }
Chris@17:
Chris@17: $pattern = '`'.$pattern.'`i';
Chris@17: if (preg_match($pattern, $this->path) === 1) {
Chris@17: $included = true;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($included === false) {
Chris@17: $this->ignoredListeners[$class] = true;
Chris@17: continue;
Chris@17: }
Chris@17: }//end if
Chris@17:
Chris@17: $this->activeListener = $class;
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) {
Chris@17: $startTime = microtime(true);
Chris@17: echo "\t\t\tProcessing ".$this->activeListener.'... ';
Chris@17: }
Chris@17:
Chris@17: $ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr);
Chris@17: if ($ignoreTo !== null) {
Chris@17: $listenerIgnoreTo[$this->activeListener] = $ignoreTo;
Chris@17: }
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) {
Chris@17: $timeTaken = (microtime(true) - $startTime);
Chris@17: if (isset($this->listenerTimes[$this->activeListener]) === false) {
Chris@17: $this->listenerTimes[$this->activeListener] = 0;
Chris@17: }
Chris@17:
Chris@17: $this->listenerTimes[$this->activeListener] += $timeTaken;
Chris@17:
Chris@17: $timeTaken = round(($timeTaken), 4);
Chris@17: echo "DONE in $timeTaken seconds".PHP_EOL;
Chris@17: }
Chris@17:
Chris@17: $this->activeListener = '';
Chris@17: }//end foreach
Chris@17: }//end foreach
Chris@17:
Chris@17: // If short open tags are off but the file being checked uses
Chris@17: // short open tags, the whole content will be inline HTML
Chris@17: // and nothing will be checked. So try and handle this case.
Chris@17: // We don't show this error for STDIN because we can't be sure the content
Chris@17: // actually came directly from the user. It could be something like
Chris@17: // refs from a Git pre-push hook.
Chris@17: if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') {
Chris@17: $shortTags = (bool) ini_get('short_open_tag');
Chris@17: if ($shortTags === false) {
Chris@17: $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
Chris@17: $this->addWarning($error, null, 'Internal.NoCodeFound');
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) {
Chris@17: echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
Chris@17: echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
Chris@17:
Chris@17: asort($this->listenerTimes, SORT_NUMERIC);
Chris@17: $this->listenerTimes = array_reverse($this->listenerTimes, true);
Chris@17: foreach ($this->listenerTimes as $listener => $timeTaken) {
Chris@17: echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
Chris@17: }
Chris@17:
Chris@17: echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL;
Chris@17: }
Chris@17:
Chris@17: $this->fixedCount += $this->fixer->getFixCount();
Chris@17:
Chris@17: }//end process()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Tokenizes the file and prepares it for the test run.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function parse()
Chris@17: {
Chris@17: if (empty($this->tokens) === false) {
Chris@17: // File has already been parsed.
Chris@17: return;
Chris@17: }
Chris@17:
Chris@17: try {
Chris@17: $tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType;
Chris@17: $this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar);
Chris@17: $this->tokens = $this->tokenizer->getTokens();
Chris@17: } catch (TokenizerException $e) {
Chris@17: $this->ignored = true;
Chris@17: $this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) {
Chris@17: echo "[$this->tokenizerType => tokenizer error]... ";
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) {
Chris@17: echo PHP_EOL;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return;
Chris@17: }
Chris@17:
Chris@17: $this->numTokens = count($this->tokens);
Chris@17:
Chris@17: // Check for mixed line endings as these can cause tokenizer errors and we
Chris@17: // should let the user know that the results they get may be incorrect.
Chris@17: // This is done by removing all backslashes, removing the newline char we
Chris@17: // detected, then converting newlines chars into text. If any backslashes
Chris@17: // are left at the end, we have additional newline chars in use.
Chris@17: $contents = str_replace('\\', '', $this->content);
Chris@17: $contents = str_replace($this->eolChar, '', $contents);
Chris@17: $contents = str_replace("\n", '\n', $contents);
Chris@17: $contents = str_replace("\r", '\r', $contents);
Chris@17: if (strpos($contents, '\\') !== false) {
Chris@17: $error = 'File has mixed line endings; this may cause incorrect results';
Chris@17: $this->addWarningOnLine($error, 1, 'Internal.LineEndings.Mixed');
Chris@17: }
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) {
Chris@17: if ($this->numTokens === 0) {
Chris@17: $numLines = 0;
Chris@17: } else {
Chris@17: $numLines = $this->tokens[($this->numTokens - 1)]['line'];
Chris@17: }
Chris@17:
Chris@17: echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... ";
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) {
Chris@17: echo PHP_EOL;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: }//end parse()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the token stack for this file.
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: * Remove vars stored in this file that are no longer required.
Chris@17: *
Chris@17: * @return void
Chris@17: */
Chris@17: public function cleanUp()
Chris@17: {
Chris@17: $this->listenerTimes = null;
Chris@17: $this->content = null;
Chris@17: $this->tokens = null;
Chris@17: $this->metricTokens = null;
Chris@17: $this->tokenizer = null;
Chris@17: $this->fixer = null;
Chris@17: $this->config = null;
Chris@17: $this->ruleset = null;
Chris@17:
Chris@17: }//end cleanUp()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records an error against a specific token in the file.
Chris@17: *
Chris@17: * @param string $error The error message.
Chris@17: * @param int $stackPtr The stack position where the error occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the error message.
Chris@17: * @param int $severity The severity level for this error. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: * @param boolean $fixable Can the error be fixed by the sniff?
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addError(
Chris@17: $error,
Chris@17: $stackPtr,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0,
Chris@17: $fixable=false
Chris@17: ) {
Chris@17: if ($stackPtr === null) {
Chris@17: $line = 1;
Chris@17: $column = 1;
Chris@17: } else {
Chris@17: $line = $this->tokens[$stackPtr]['line'];
Chris@17: $column = $this->tokens[$stackPtr]['column'];
Chris@17: }
Chris@17:
Chris@17: return $this->addMessage(true, $error, $line, $column, $code, $data, $severity, $fixable);
Chris@17:
Chris@17: }//end addError()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records a warning against a specific token in the file.
Chris@17: *
Chris@17: * @param string $warning The error message.
Chris@17: * @param int $stackPtr The stack position where the error occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the warning message.
Chris@17: * @param int $severity The severity level for this warning. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: * @param boolean $fixable Can the warning be fixed by the sniff?
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addWarning(
Chris@17: $warning,
Chris@17: $stackPtr,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0,
Chris@17: $fixable=false
Chris@17: ) {
Chris@17: if ($stackPtr === null) {
Chris@17: $line = 1;
Chris@17: $column = 1;
Chris@17: } else {
Chris@17: $line = $this->tokens[$stackPtr]['line'];
Chris@17: $column = $this->tokens[$stackPtr]['column'];
Chris@17: }
Chris@17:
Chris@17: return $this->addMessage(false, $warning, $line, $column, $code, $data, $severity, $fixable);
Chris@17:
Chris@17: }//end addWarning()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records an error against a specific line in the file.
Chris@17: *
Chris@17: * @param string $error The error message.
Chris@17: * @param int $line The line on which the error occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the error message.
Chris@17: * @param int $severity The severity level for this error. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addErrorOnLine(
Chris@17: $error,
Chris@17: $line,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0
Chris@17: ) {
Chris@17: return $this->addMessage(true, $error, $line, 1, $code, $data, $severity, false);
Chris@17:
Chris@17: }//end addErrorOnLine()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records a warning against a specific token in the file.
Chris@17: *
Chris@17: * @param string $warning The error message.
Chris@17: * @param int $line The line on which the warning occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the warning message.
Chris@17: * @param int $severity The severity level for this warning. A value of 0 will
Chris@17: * will be converted into the default severity level.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addWarningOnLine(
Chris@17: $warning,
Chris@17: $line,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0
Chris@17: ) {
Chris@17: return $this->addMessage(false, $warning, $line, 1, $code, $data, $severity, false);
Chris@17:
Chris@17: }//end addWarningOnLine()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records a fixable error against a specific token in the file.
Chris@17: *
Chris@17: * Returns true if the error was recorded and should be fixed.
Chris@17: *
Chris@17: * @param string $error The error message.
Chris@17: * @param int $stackPtr The stack position where the error occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the error message.
Chris@17: * @param int $severity The severity level for this error. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addFixableError(
Chris@17: $error,
Chris@17: $stackPtr,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0
Chris@17: ) {
Chris@17: $recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true);
Chris@17: if ($recorded === true && $this->fixer->enabled === true) {
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end addFixableError()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Records a fixable warning against a specific token in the file.
Chris@17: *
Chris@17: * Returns true if the warning was recorded and should be fixed.
Chris@17: *
Chris@17: * @param string $warning The error message.
Chris@17: * @param int $stackPtr The stack position where the error occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the warning message.
Chris@17: * @param int $severity The severity level for this warning. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function addFixableWarning(
Chris@17: $warning,
Chris@17: $stackPtr,
Chris@17: $code,
Chris@17: $data=[],
Chris@17: $severity=0
Chris@17: ) {
Chris@17: $recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true);
Chris@17: if ($recorded === true && $this->fixer->enabled === true) {
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end addFixableWarning()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Adds an error to the error stack.
Chris@17: *
Chris@17: * @param boolean $error Is this an error message?
Chris@17: * @param string $message The text of the message.
Chris@17: * @param int $line The line on which the message occurred.
Chris@17: * @param int $column The column at which the message occurred.
Chris@17: * @param string $code A violation code unique to the sniff message.
Chris@17: * @param array $data Replacements for the message.
Chris@17: * @param int $severity The severity level for this message. A value of 0
Chris@17: * will be converted into the default severity level.
Chris@17: * @param boolean $fixable Can the problem be fixed by the sniff?
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable)
Chris@17: {
Chris@17: // Check if this line is ignoring all message codes.
Chris@17: if (isset($this->tokenizer->ignoredLines[$line]['.all']) === true) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // Work out which sniff generated the message.
Chris@17: $parts = explode('.', $code);
Chris@17: if ($parts[0] === 'Internal') {
Chris@17: // An internal message.
Chris@17: $listenerCode = Util\Common::getSniffCode($this->activeListener);
Chris@17: $sniffCode = $code;
Chris@17: $checkCodes = [$sniffCode];
Chris@17: } else {
Chris@17: if ($parts[0] !== $code) {
Chris@17: // The full message code has been passed in.
Chris@17: $sniffCode = $code;
Chris@17: $listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.'));
Chris@17: } else {
Chris@17: $listenerCode = Util\Common::getSniffCode($this->activeListener);
Chris@17: $sniffCode = $listenerCode.'.'.$code;
Chris@17: $parts = explode('.', $sniffCode);
Chris@17: }
Chris@17:
Chris@17: $checkCodes = [
Chris@17: $sniffCode,
Chris@17: $parts[0].'.'.$parts[1].'.'.$parts[2],
Chris@17: $parts[0].'.'.$parts[1],
Chris@17: $parts[0],
Chris@17: ];
Chris@17: }//end if
Chris@17:
Chris@17: if (isset($this->tokenizer->ignoredLines[$line]) === true) {
Chris@17: // Check if this line is ignoring this specific message.
Chris@17: $ignored = false;
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: if (isset($this->tokenizer->ignoredLines[$line][$checkCode]) === true) {
Chris@17: $ignored = true;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: // If it is ignored, make sure it's not whitelisted.
Chris@17: if ($ignored === true
Chris@17: && isset($this->tokenizer->ignoredLines[$line]['.except']) === true
Chris@17: ) {
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: if (isset($this->tokenizer->ignoredLines[$line]['.except'][$checkCode]) === true) {
Chris@17: $ignored = false;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($ignored === true) {
Chris@17: return false;
Chris@17: }
Chris@17: }//end if
Chris@17:
Chris@17: $includeAll = true;
Chris@17: if ($this->configCache['cache'] === false
Chris@17: || $this->configCache['recordErrors'] === false
Chris@17: ) {
Chris@17: $includeAll = false;
Chris@17: }
Chris@17:
Chris@17: // Filter out any messages for sniffs that shouldn't have run
Chris@17: // due to the use of the --sniffs command line argument.
Chris@17: if ($includeAll === false
Chris@17: && ((empty($this->configCache['sniffs']) === false
Chris@18: && in_array(strtolower($listenerCode), $this->configCache['sniffs'], true) === false)
Chris@17: || (empty($this->configCache['exclude']) === false
Chris@18: && in_array(strtolower($listenerCode), $this->configCache['exclude'], true) === true))
Chris@17: ) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // If we know this sniff code is being ignored for this file, return early.
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: if (isset($this->ignoredCodes[$checkCode]) === true) {
Chris@17: return false;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: $oppositeType = 'warning';
Chris@17: if ($error === false) {
Chris@17: $oppositeType = 'error';
Chris@17: }
Chris@17:
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: // Make sure this message type has not been set to the opposite message type.
Chris@17: if (isset($this->ruleset->ruleset[$checkCode]['type']) === true
Chris@17: && $this->ruleset->ruleset[$checkCode]['type'] === $oppositeType
Chris@17: ) {
Chris@17: $error = !$error;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($error === true) {
Chris@17: $configSeverity = $this->configCache['errorSeverity'];
Chris@17: $messageCount = &$this->errorCount;
Chris@17: $messages = &$this->errors;
Chris@17: } else {
Chris@17: $configSeverity = $this->configCache['warningSeverity'];
Chris@17: $messageCount = &$this->warningCount;
Chris@17: $messages = &$this->warnings;
Chris@17: }
Chris@17:
Chris@17: if ($includeAll === false && $configSeverity === 0) {
Chris@17: // Don't bother doing any processing as these messages are just going to
Chris@17: // be hidden in the reports anyway.
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: if ($severity === 0) {
Chris@17: $severity = 5;
Chris@17: }
Chris@17:
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: // Make sure we are interested in this severity level.
Chris@17: if (isset($this->ruleset->ruleset[$checkCode]['severity']) === true) {
Chris@17: $severity = $this->ruleset->ruleset[$checkCode]['severity'];
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($includeAll === false && $configSeverity > $severity) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // Make sure we are not ignoring this file.
Chris@17: $included = null;
Chris@17: foreach ($checkCodes as $checkCode) {
Chris@17: $patterns = null;
Chris@17:
Chris@17: if (isset($this->configCache['includePatterns'][$checkCode]) === true) {
Chris@17: $patterns = $this->configCache['includePatterns'][$checkCode];
Chris@17: $excluding = false;
Chris@17: } else if (isset($this->configCache['ignorePatterns'][$checkCode]) === true) {
Chris@17: $patterns = $this->configCache['ignorePatterns'][$checkCode];
Chris@17: $excluding = true;
Chris@17: }
Chris@17:
Chris@17: if ($patterns === null) {
Chris@17: continue;
Chris@17: }
Chris@17:
Chris@17: foreach ($patterns as $pattern => $type) {
Chris@17: // While there is support for a type of each pattern
Chris@17: // (absolute or relative) we don't actually support it here.
Chris@17: $replacements = [
Chris@17: '\\,' => ',',
Chris@17: '*' => '.*',
Chris@17: ];
Chris@17:
Chris@17: // We assume a / directory separator, as do the exclude rules
Chris@17: // most developers write, so we need a special case for any system
Chris@17: // that is different.
Chris@17: if (DIRECTORY_SEPARATOR === '\\') {
Chris@17: $replacements['/'] = '\\\\';
Chris@17: }
Chris@17:
Chris@17: $pattern = '`'.strtr($pattern, $replacements).'`i';
Chris@17: $matched = preg_match($pattern, $this->path);
Chris@17:
Chris@17: if ($matched === 0) {
Chris@17: if ($excluding === false && $included === null) {
Chris@17: // This file path is not being included.
Chris@17: $included = false;
Chris@17: }
Chris@17:
Chris@17: continue;
Chris@17: }
Chris@17:
Chris@17: if ($excluding === true) {
Chris@17: // This file path is being excluded.
Chris@17: $this->ignoredCodes[$checkCode] = true;
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // This file path is being included.
Chris@17: $included = true;
Chris@17: break;
Chris@17: }//end foreach
Chris@17: }//end foreach
Chris@17:
Chris@17: if ($included === false) {
Chris@17: // There were include rules set, but this file
Chris@17: // path didn't match any of them.
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $messageCount++;
Chris@17: if ($fixable === true) {
Chris@17: $this->fixableCount++;
Chris@17: }
Chris@17:
Chris@17: if ($this->configCache['recordErrors'] === false
Chris@17: && $includeAll === false
Chris@17: ) {
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: // Work out the error message.
Chris@17: if (isset($this->ruleset->ruleset[$sniffCode]['message']) === true) {
Chris@17: $message = $this->ruleset->ruleset[$sniffCode]['message'];
Chris@17: }
Chris@17:
Chris@17: if (empty($data) === false) {
Chris@17: $message = vsprintf($message, $data);
Chris@17: }
Chris@17:
Chris@17: if (isset($messages[$line]) === false) {
Chris@17: $messages[$line] = [];
Chris@17: }
Chris@17:
Chris@17: if (isset($messages[$line][$column]) === false) {
Chris@17: $messages[$line][$column] = [];
Chris@17: }
Chris@17:
Chris@17: $messages[$line][$column][] = [
Chris@17: 'message' => $message,
Chris@17: 'source' => $sniffCode,
Chris@17: 'listener' => $this->activeListener,
Chris@17: 'severity' => $severity,
Chris@17: 'fixable' => $fixable,
Chris@17: ];
Chris@17:
Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1
Chris@17: && $this->fixer->enabled === true
Chris@17: && $fixable === true
Chris@17: ) {
Chris@17: @ob_end_clean();
Chris@17: echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL;
Chris@17: ob_start();
Chris@17: }
Chris@17:
Chris@17: return true;
Chris@17:
Chris@17: }//end addMessage()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Record a metric about the file being examined.
Chris@17: *
Chris@17: * @param int $stackPtr The stack position where the metric was recorded.
Chris@17: * @param string $metric The name of the metric being recorded.
Chris@17: * @param string $value The value of the metric being recorded.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function recordMetric($stackPtr, $metric, $value)
Chris@17: {
Chris@17: if (isset($this->metrics[$metric]) === false) {
Chris@17: $this->metrics[$metric] = ['values' => [$value => 1]];
Chris@17: $this->metricTokens[$metric][$stackPtr] = true;
Chris@17: } else if (isset($this->metricTokens[$metric][$stackPtr]) === false) {
Chris@17: $this->metricTokens[$metric][$stackPtr] = true;
Chris@17: if (isset($this->metrics[$metric]['values'][$value]) === false) {
Chris@17: $this->metrics[$metric]['values'][$value] = 1;
Chris@17: } else {
Chris@17: $this->metrics[$metric]['values'][$value]++;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return true;
Chris@17:
Chris@17: }//end recordMetric()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the number of errors raised.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function getErrorCount()
Chris@17: {
Chris@17: return $this->errorCount;
Chris@17:
Chris@17: }//end getErrorCount()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the number of warnings raised.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function getWarningCount()
Chris@17: {
Chris@17: return $this->warningCount;
Chris@17:
Chris@17: }//end getWarningCount()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the number of fixable errors/warnings raised.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function getFixableCount()
Chris@17: {
Chris@17: return $this->fixableCount;
Chris@17:
Chris@17: }//end getFixableCount()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the number of fixed errors/warnings.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function getFixedCount()
Chris@17: {
Chris@17: return $this->fixedCount;
Chris@17:
Chris@17: }//end getFixedCount()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the list of ignored lines.
Chris@17: *
Chris@17: * @return array
Chris@17: */
Chris@17: public function getIgnoredLines()
Chris@17: {
Chris@17: return $this->tokenizer->ignoredLines;
Chris@17:
Chris@17: }//end getIgnoredLines()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the errors raised from processing this file.
Chris@17: *
Chris@17: * @return array
Chris@17: */
Chris@17: public function getErrors()
Chris@17: {
Chris@17: return $this->errors;
Chris@17:
Chris@17: }//end getErrors()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the warnings raised from processing this file.
Chris@17: *
Chris@17: * @return array
Chris@17: */
Chris@17: public function getWarnings()
Chris@17: {
Chris@17: return $this->warnings;
Chris@17:
Chris@17: }//end getWarnings()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the metrics found while processing this file.
Chris@17: *
Chris@17: * @return array
Chris@17: */
Chris@17: public function getMetrics()
Chris@17: {
Chris@17: return $this->metrics;
Chris@17:
Chris@17: }//end getMetrics()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the absolute filename of this file.
Chris@17: *
Chris@17: * @return string
Chris@17: */
Chris@17: public function getFilename()
Chris@17: {
Chris@17: return $this->path;
Chris@17:
Chris@17: }//end getFilename()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the declaration names for classes, interfaces, traits, and functions.
Chris@17: *
Chris@17: * @param int $stackPtr The position of the declaration token which
Chris@17: * declared the class, interface, trait, or function.
Chris@17: *
Chris@17: * @return string|null The name of the class, interface, trait, or function;
Chris@17: * or NULL if the function or class is anonymous.
Chris@17: * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type
Chris@17: * T_FUNCTION, T_CLASS, T_ANON_CLASS,
Chris@17: * T_CLOSURE, T_TRAIT, or T_INTERFACE.
Chris@17: */
Chris@17: public function getDeclarationName($stackPtr)
Chris@17: {
Chris@17: $tokenCode = $this->tokens[$stackPtr]['code'];
Chris@17:
Chris@17: if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) {
Chris@17: return null;
Chris@17: }
Chris@17:
Chris@17: if ($tokenCode !== T_FUNCTION
Chris@17: && $tokenCode !== T_CLASS
Chris@17: && $tokenCode !== T_INTERFACE
Chris@17: && $tokenCode !== T_TRAIT
Chris@17: ) {
Chris@17: throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT');
Chris@17: }
Chris@17:
Chris@17: if ($tokenCode === T_FUNCTION
Chris@17: && strtolower($this->tokens[$stackPtr]['content']) !== 'function'
Chris@17: ) {
Chris@17: // This is a function declared without the "function" keyword.
Chris@17: // So this token is the function name.
Chris@17: return $this->tokens[$stackPtr]['content'];
Chris@17: }
Chris@17:
Chris@17: $content = null;
Chris@17: for ($i = $stackPtr; $i < $this->numTokens; $i++) {
Chris@17: if ($this->tokens[$i]['code'] === T_STRING) {
Chris@17: $content = $this->tokens[$i]['content'];
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return $content;
Chris@17:
Chris@17: }//end getDeclarationName()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the method parameters for the specified function token.
Chris@17: *
Chris@17: * Each parameter is in the following format:
Chris@17: *
Chris@17: *
Chris@17: * 0 => array(
Chris@17: * 'name' => '$var', // The variable name.
Chris@17: * 'token' => integer, // The stack pointer to the variable name.
Chris@17: * 'content' => string, // The full content of the variable definition.
Chris@17: * 'pass_by_reference' => boolean, // Is the variable passed by reference?
Chris@17: * 'variable_length' => boolean, // Is the param of variable length through use of `...` ?
Chris@17: * 'type_hint' => string, // The type hint for the variable.
Chris@17: * 'type_hint_token' => integer, // The stack pointer to the type hint
Chris@17: * // or false if there is no type hint.
Chris@17: * 'nullable_type' => boolean, // Is the variable using a nullable type?
Chris@17: * )
Chris@17: *
Chris@17: *
Chris@17: * Parameters with default values have an additional array index of
Chris@17: * 'default' with the value of the default as a string.
Chris@17: *
Chris@17: * @param int $stackPtr The position in the stack of the function token
Chris@17: * to acquire the parameters for.
Chris@17: *
Chris@17: * @return array
Chris@17: * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of
Chris@17: * type T_FUNCTION or T_CLOSURE.
Chris@17: */
Chris@17: public function getMethodParameters($stackPtr)
Chris@17: {
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
Chris@17: && $this->tokens[$stackPtr]['code'] !== T_CLOSURE
Chris@17: ) {
Chris@17: throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
Chris@17: }
Chris@17:
Chris@17: $opener = $this->tokens[$stackPtr]['parenthesis_opener'];
Chris@17: $closer = $this->tokens[$stackPtr]['parenthesis_closer'];
Chris@17:
Chris@17: $vars = [];
Chris@17: $currVar = null;
Chris@17: $paramStart = ($opener + 1);
Chris@17: $defaultStart = null;
Chris@17: $paramCount = 0;
Chris@17: $passByReference = false;
Chris@17: $variableLength = false;
Chris@17: $typeHint = '';
Chris@17: $typeHintToken = false;
Chris@17: $nullableType = false;
Chris@17:
Chris@17: for ($i = $paramStart; $i <= $closer; $i++) {
Chris@17: // Check to see if this token has a parenthesis or bracket opener. If it does
Chris@17: // it's likely to be an array which might have arguments in it. This
Chris@17: // could cause problems in our parsing below, so lets just skip to the
Chris@17: // end of it.
Chris@17: if (isset($this->tokens[$i]['parenthesis_opener']) === true) {
Chris@17: // Don't do this if it's the close parenthesis for the method.
Chris@17: if ($i !== $this->tokens[$i]['parenthesis_closer']) {
Chris@17: $i = ($this->tokens[$i]['parenthesis_closer'] + 1);
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if (isset($this->tokens[$i]['bracket_opener']) === true) {
Chris@17: // Don't do this if it's the close parenthesis for the method.
Chris@17: if ($i !== $this->tokens[$i]['bracket_closer']) {
Chris@17: $i = ($this->tokens[$i]['bracket_closer'] + 1);
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: switch ($this->tokens[$i]['code']) {
Chris@17: case T_BITWISE_AND:
Chris@17: if ($defaultStart === null) {
Chris@17: $passByReference = true;
Chris@17: }
Chris@17: break;
Chris@17: case T_VARIABLE:
Chris@17: $currVar = $i;
Chris@17: break;
Chris@17: case T_ELLIPSIS:
Chris@17: $variableLength = true;
Chris@17: break;
Chris@17: case T_CALLABLE:
Chris@17: if ($typeHintToken === false) {
Chris@17: $typeHintToken = $i;
Chris@17: }
Chris@17:
Chris@17: $typeHint .= $this->tokens[$i]['content'];
Chris@17: break;
Chris@17: case T_SELF:
Chris@17: case T_PARENT:
Chris@17: case T_STATIC:
Chris@17: // Self and parent are valid, static invalid, but was probably intended as type hint.
Chris@17: if (isset($defaultStart) === false) {
Chris@17: if ($typeHintToken === false) {
Chris@17: $typeHintToken = $i;
Chris@17: }
Chris@17:
Chris@17: $typeHint .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: break;
Chris@17: case T_STRING:
Chris@17: // This is a string, so it may be a type hint, but it could
Chris@17: // also be a constant used as a default value.
Chris@17: $prevComma = false;
Chris@17: for ($t = $i; $t >= $opener; $t--) {
Chris@17: if ($this->tokens[$t]['code'] === T_COMMA) {
Chris@17: $prevComma = $t;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($prevComma !== false) {
Chris@17: $nextEquals = false;
Chris@17: for ($t = $prevComma; $t < $i; $t++) {
Chris@17: if ($this->tokens[$t]['code'] === T_EQUAL) {
Chris@17: $nextEquals = $t;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($nextEquals !== false) {
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($defaultStart === null) {
Chris@17: if ($typeHintToken === false) {
Chris@17: $typeHintToken = $i;
Chris@17: }
Chris@17:
Chris@17: $typeHint .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: break;
Chris@17: case T_NS_SEPARATOR:
Chris@17: // Part of a type hint or default value.
Chris@17: if ($defaultStart === null) {
Chris@17: if ($typeHintToken === false) {
Chris@17: $typeHintToken = $i;
Chris@17: }
Chris@17:
Chris@17: $typeHint .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: break;
Chris@17: case T_NULLABLE:
Chris@17: if ($defaultStart === null) {
Chris@17: $nullableType = true;
Chris@17: $typeHint .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: break;
Chris@17: case T_CLOSE_PARENTHESIS:
Chris@17: case T_COMMA:
Chris@17: // If it's null, then there must be no parameters for this
Chris@17: // method.
Chris@17: if ($currVar === null) {
Chris@17: continue 2;
Chris@17: }
Chris@17:
Chris@17: $vars[$paramCount] = [];
Chris@17: $vars[$paramCount]['token'] = $currVar;
Chris@17: $vars[$paramCount]['name'] = $this->tokens[$currVar]['content'];
Chris@17: $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart)));
Chris@17:
Chris@17: if ($defaultStart !== null) {
Chris@17: $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart)));
Chris@17: }
Chris@17:
Chris@17: $vars[$paramCount]['pass_by_reference'] = $passByReference;
Chris@17: $vars[$paramCount]['variable_length'] = $variableLength;
Chris@17: $vars[$paramCount]['type_hint'] = $typeHint;
Chris@17: $vars[$paramCount]['type_hint_token'] = $typeHintToken;
Chris@17: $vars[$paramCount]['nullable_type'] = $nullableType;
Chris@17:
Chris@17: // Reset the vars, as we are about to process the next parameter.
Chris@17: $defaultStart = null;
Chris@17: $paramStart = ($i + 1);
Chris@17: $passByReference = false;
Chris@17: $variableLength = false;
Chris@17: $typeHint = '';
Chris@17: $typeHintToken = false;
Chris@17: $nullableType = false;
Chris@17:
Chris@17: $paramCount++;
Chris@17: break;
Chris@17: case T_EQUAL:
Chris@17: $defaultStart = ($i + 1);
Chris@17: break;
Chris@17: }//end switch
Chris@17: }//end for
Chris@17:
Chris@17: return $vars;
Chris@17:
Chris@17: }//end getMethodParameters()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the visibility and implementation properties of a method.
Chris@17: *
Chris@17: * The format of the array is:
Chris@17: *
Chris@17: * array(
Chris@17: * 'scope' => 'public', // public protected or protected
Chris@17: * 'scope_specified' => true, // true is scope keyword was found.
Chris@17: * 'return_type' => '', // the return type of the method.
Chris@17: * 'return_type_token' => integer, // The stack pointer to the start of the return type
Chris@17: * // or false if there is no return type.
Chris@17: * 'nullable_return_type' => false, // true if the return type is nullable.
Chris@17: * 'is_abstract' => false, // true if the abstract keyword was found.
Chris@17: * 'is_final' => false, // true if the final keyword was found.
Chris@17: * 'is_static' => false, // true if the static keyword was found.
Chris@17: * 'has_body' => false, // true if the method has a body
Chris@17: * );
Chris@17: *
Chris@17: *
Chris@17: * @param int $stackPtr The position in the stack of the function token to
Chris@17: * acquire the properties for.
Chris@17: *
Chris@17: * @return array
Chris@17: * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
Chris@17: * T_FUNCTION token.
Chris@17: */
Chris@17: public function getMethodProperties($stackPtr)
Chris@17: {
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
Chris@17: && $this->tokens[$stackPtr]['code'] !== T_CLOSURE
Chris@17: ) {
Chris@17: throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
Chris@17: $valid = [
Chris@17: T_PUBLIC => T_PUBLIC,
Chris@17: T_PRIVATE => T_PRIVATE,
Chris@17: T_PROTECTED => T_PROTECTED,
Chris@17: T_STATIC => T_STATIC,
Chris@17: T_FINAL => T_FINAL,
Chris@17: T_ABSTRACT => T_ABSTRACT,
Chris@17: T_WHITESPACE => T_WHITESPACE,
Chris@17: T_COMMENT => T_COMMENT,
Chris@17: T_DOC_COMMENT => T_DOC_COMMENT,
Chris@17: ];
Chris@17: } else {
Chris@17: $valid = [
Chris@17: T_STATIC => T_STATIC,
Chris@17: T_WHITESPACE => T_WHITESPACE,
Chris@17: T_COMMENT => T_COMMENT,
Chris@17: T_DOC_COMMENT => T_DOC_COMMENT,
Chris@17: ];
Chris@17: }
Chris@17:
Chris@17: $scope = 'public';
Chris@17: $scopeSpecified = false;
Chris@17: $isAbstract = false;
Chris@17: $isFinal = false;
Chris@17: $isStatic = false;
Chris@17:
Chris@17: for ($i = ($stackPtr - 1); $i > 0; $i--) {
Chris@17: if (isset($valid[$this->tokens[$i]['code']]) === false) {
Chris@17: break;
Chris@17: }
Chris@17:
Chris@17: switch ($this->tokens[$i]['code']) {
Chris@17: case T_PUBLIC:
Chris@17: $scope = 'public';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_PRIVATE:
Chris@17: $scope = 'private';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_PROTECTED:
Chris@17: $scope = 'protected';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_ABSTRACT:
Chris@17: $isAbstract = true;
Chris@17: break;
Chris@17: case T_FINAL:
Chris@17: $isFinal = true;
Chris@17: break;
Chris@17: case T_STATIC:
Chris@17: $isStatic = true;
Chris@17: break;
Chris@17: }//end switch
Chris@17: }//end for
Chris@17:
Chris@17: $returnType = '';
Chris@17: $returnTypeToken = false;
Chris@17: $nullableReturnType = false;
Chris@17: $hasBody = true;
Chris@17:
Chris@17: if (isset($this->tokens[$stackPtr]['parenthesis_closer']) === true) {
Chris@17: $scopeOpener = null;
Chris@17: if (isset($this->tokens[$stackPtr]['scope_opener']) === true) {
Chris@17: $scopeOpener = $this->tokens[$stackPtr]['scope_opener'];
Chris@17: }
Chris@17:
Chris@17: $valid = [
Chris@17: T_STRING => T_STRING,
Chris@17: T_CALLABLE => T_CALLABLE,
Chris@17: T_SELF => T_SELF,
Chris@17: T_PARENT => T_PARENT,
Chris@17: T_NS_SEPARATOR => T_NS_SEPARATOR,
Chris@17: ];
Chris@17:
Chris@17: for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) {
Chris@17: if (($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON)
Chris@17: || ($scopeOpener !== null && $i === $scopeOpener)
Chris@17: ) {
Chris@17: // End of function definition.
Chris@17: break;
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$i]['code'] === T_NULLABLE) {
Chris@17: $nullableReturnType = true;
Chris@17: }
Chris@17:
Chris@17: if (isset($valid[$this->tokens[$i]['code']]) === true) {
Chris@17: if ($returnTypeToken === false) {
Chris@17: $returnTypeToken = $i;
Chris@17: }
Chris@17:
Chris@17: $returnType .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: $end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']);
Chris@17: $hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET;
Chris@17: }//end if
Chris@17:
Chris@17: if ($returnType !== '' && $nullableReturnType === true) {
Chris@17: $returnType = '?'.$returnType;
Chris@17: }
Chris@17:
Chris@17: return [
Chris@17: 'scope' => $scope,
Chris@17: 'scope_specified' => $scopeSpecified,
Chris@17: 'return_type' => $returnType,
Chris@17: 'return_type_token' => $returnTypeToken,
Chris@17: 'nullable_return_type' => $nullableReturnType,
Chris@17: 'is_abstract' => $isAbstract,
Chris@17: 'is_final' => $isFinal,
Chris@17: 'is_static' => $isStatic,
Chris@17: 'has_body' => $hasBody,
Chris@17: ];
Chris@17:
Chris@17: }//end getMethodProperties()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the visibility and implementation properties of the class member
Chris@17: * variable found at the specified position in the stack.
Chris@17: *
Chris@17: * The format of the array is:
Chris@17: *
Chris@17: *
Chris@17: * array(
Chris@17: * 'scope' => 'public', // public protected or protected.
Chris@17: * 'scope_specified' => false, // true if the scope was explicitly specified.
Chris@17: * 'is_static' => false, // true if the static keyword was found.
Chris@17: * );
Chris@17: *
Chris@17: *
Chris@17: * @param int $stackPtr The position in the stack of the T_VARIABLE token to
Chris@17: * acquire the properties for.
Chris@17: *
Chris@17: * @return array
Chris@17: * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
Chris@17: * T_VARIABLE token, or if the position is not
Chris@17: * a class member variable.
Chris@17: */
Chris@17: public function getMemberProperties($stackPtr)
Chris@17: {
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_VARIABLE) {
Chris@17: throw new TokenizerException('$stackPtr must be of type T_VARIABLE');
Chris@17: }
Chris@17:
Chris@17: $conditions = array_keys($this->tokens[$stackPtr]['conditions']);
Chris@17: $ptr = array_pop($conditions);
Chris@17: if (isset($this->tokens[$ptr]) === false
Chris@17: || ($this->tokens[$ptr]['code'] !== T_CLASS
Chris@17: && $this->tokens[$ptr]['code'] !== T_ANON_CLASS
Chris@17: && $this->tokens[$ptr]['code'] !== T_TRAIT)
Chris@17: ) {
Chris@17: if (isset($this->tokens[$ptr]) === true
Chris@17: && $this->tokens[$ptr]['code'] === T_INTERFACE
Chris@17: ) {
Chris@17: // T_VARIABLEs in interfaces can actually be method arguments
Chris@17: // but they wont be seen as being inside the method because there
Chris@17: // are no scope openers and closers for abstract methods. If it is in
Chris@17: // parentheses, we can be pretty sure it is a method argument.
Chris@17: if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === false
Chris@17: || empty($this->tokens[$stackPtr]['nested_parenthesis']) === true
Chris@17: ) {
Chris@17: $error = 'Possible parse error: interfaces may not include member vars';
Chris@17: $this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar');
Chris@17: return [];
Chris@17: }
Chris@17: } else {
Chris@17: throw new TokenizerException('$stackPtr is not a class member var');
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: // Make sure it's not a method parameter.
Chris@17: if (empty($this->tokens[$stackPtr]['nested_parenthesis']) === false) {
Chris@17: $parenthesis = array_keys($this->tokens[$stackPtr]['nested_parenthesis']);
Chris@17: $deepestOpen = array_pop($parenthesis);
Chris@17: if ($deepestOpen > $ptr
Chris@17: && isset($this->tokens[$deepestOpen]['parenthesis_owner']) === true
Chris@17: && $this->tokens[$this->tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
Chris@17: ) {
Chris@17: throw new TokenizerException('$stackPtr is not a class member var');
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: $valid = [
Chris@17: T_PUBLIC => T_PUBLIC,
Chris@17: T_PRIVATE => T_PRIVATE,
Chris@17: T_PROTECTED => T_PROTECTED,
Chris@17: T_STATIC => T_STATIC,
Chris@17: T_VAR => T_VAR,
Chris@17: ];
Chris@17:
Chris@17: $valid += Util\Tokens::$emptyTokens;
Chris@17:
Chris@17: $scope = 'public';
Chris@17: $scopeSpecified = false;
Chris@17: $isStatic = false;
Chris@17:
Chris@17: $startOfStatement = $this->findPrevious(
Chris@17: [
Chris@17: T_SEMICOLON,
Chris@17: T_OPEN_CURLY_BRACKET,
Chris@17: T_CLOSE_CURLY_BRACKET,
Chris@17: ],
Chris@17: ($stackPtr - 1)
Chris@17: );
Chris@17:
Chris@17: for ($i = ($startOfStatement + 1); $i < $stackPtr; $i++) {
Chris@17: if (isset($valid[$this->tokens[$i]['code']]) === false) {
Chris@17: break;
Chris@17: }
Chris@17:
Chris@17: switch ($this->tokens[$i]['code']) {
Chris@17: case T_PUBLIC:
Chris@17: $scope = 'public';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_PRIVATE:
Chris@17: $scope = 'private';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_PROTECTED:
Chris@17: $scope = 'protected';
Chris@17: $scopeSpecified = true;
Chris@17: break;
Chris@17: case T_STATIC:
Chris@17: $isStatic = true;
Chris@17: break;
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return [
Chris@17: 'scope' => $scope,
Chris@17: 'scope_specified' => $scopeSpecified,
Chris@17: 'is_static' => $isStatic,
Chris@17: ];
Chris@17:
Chris@17: }//end getMemberProperties()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the visibility and implementation properties of a class.
Chris@17: *
Chris@17: * The format of the array is:
Chris@17: *
Chris@17: * array(
Chris@17: * 'is_abstract' => false, // true if the abstract keyword was found.
Chris@17: * 'is_final' => false, // true if the final keyword was found.
Chris@17: * );
Chris@17: *
Chris@17: *
Chris@17: * @param int $stackPtr The position in the stack of the T_CLASS token to
Chris@17: * acquire the properties for.
Chris@17: *
Chris@17: * @return array
Chris@17: * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
Chris@17: * T_CLASS token.
Chris@17: */
Chris@17: public function getClassProperties($stackPtr)
Chris@17: {
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_CLASS) {
Chris@17: throw new TokenizerException('$stackPtr must be of type T_CLASS');
Chris@17: }
Chris@17:
Chris@17: $valid = [
Chris@17: T_FINAL => T_FINAL,
Chris@17: T_ABSTRACT => T_ABSTRACT,
Chris@17: T_WHITESPACE => T_WHITESPACE,
Chris@17: T_COMMENT => T_COMMENT,
Chris@17: T_DOC_COMMENT => T_DOC_COMMENT,
Chris@17: ];
Chris@17:
Chris@17: $isAbstract = false;
Chris@17: $isFinal = false;
Chris@17:
Chris@17: for ($i = ($stackPtr - 1); $i > 0; $i--) {
Chris@17: if (isset($valid[$this->tokens[$i]['code']]) === false) {
Chris@17: break;
Chris@17: }
Chris@17:
Chris@17: switch ($this->tokens[$i]['code']) {
Chris@17: case T_ABSTRACT:
Chris@17: $isAbstract = true;
Chris@17: break;
Chris@17:
Chris@17: case T_FINAL:
Chris@17: $isFinal = true;
Chris@17: break;
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return [
Chris@17: 'is_abstract' => $isAbstract,
Chris@17: 'is_final' => $isFinal,
Chris@17: ];
Chris@17:
Chris@17: }//end getClassProperties()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Determine if the passed token is a reference operator.
Chris@17: *
Chris@17: * Returns true if the specified token position represents a reference.
Chris@17: * Returns false if the token represents a bitwise operator.
Chris@17: *
Chris@17: * @param int $stackPtr The position of the T_BITWISE_AND token.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function isReference($stackPtr)
Chris@17: {
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_BITWISE_AND) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $tokenBefore = $this->findPrevious(
Chris@17: Util\Tokens::$emptyTokens,
Chris@17: ($stackPtr - 1),
Chris@17: null,
Chris@17: true
Chris@17: );
Chris@17:
Chris@17: if ($this->tokens[$tokenBefore]['code'] === T_FUNCTION) {
Chris@17: // Function returns a reference.
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$tokenBefore]['code'] === T_DOUBLE_ARROW) {
Chris@17: // Inside a foreach loop or array assignment, this is a reference.
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$tokenBefore]['code'] === T_AS) {
Chris@17: // Inside a foreach loop, this is a reference.
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) {
Chris@17: // This is directly after an assignment. It's a reference. Even if
Chris@17: // it is part of an operation, the other tests will handle it.
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: $tokenAfter = $this->findNext(
Chris@17: Util\Tokens::$emptyTokens,
Chris@17: ($stackPtr + 1),
Chris@17: null,
Chris@17: true
Chris@17: );
Chris@17:
Chris@17: if ($this->tokens[$tokenAfter]['code'] === T_NEW) {
Chris@17: return true;
Chris@17: }
Chris@17:
Chris@17: if (isset($this->tokens[$stackPtr]['nested_parenthesis']) === true) {
Chris@17: $brackets = $this->tokens[$stackPtr]['nested_parenthesis'];
Chris@17: $lastBracket = array_pop($brackets);
Chris@17: if (isset($this->tokens[$lastBracket]['parenthesis_owner']) === true) {
Chris@17: $owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']];
Chris@17: if ($owner['code'] === T_FUNCTION
Chris@17: || $owner['code'] === T_CLOSURE
Chris@17: ) {
Chris@17: $params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']);
Chris@17: foreach ($params as $param) {
Chris@17: $varToken = $tokenAfter;
Chris@17: if ($param['variable_length'] === true) {
Chris@17: $varToken = $this->findNext(
Chris@17: (Util\Tokens::$emptyTokens + [T_ELLIPSIS]),
Chris@17: ($stackPtr + 1),
Chris@17: null,
Chris@17: true
Chris@17: );
Chris@17: }
Chris@17:
Chris@17: if ($param['token'] === $varToken
Chris@17: && $param['pass_by_reference'] === true
Chris@17: ) {
Chris@17: // Function parameter declared to be passed by reference.
Chris@17: return true;
Chris@17: }
Chris@17: }
Chris@17: }//end if
Chris@17: } else {
Chris@17: $prev = false;
Chris@17: for ($t = ($this->tokens[$lastBracket]['parenthesis_opener'] - 1); $t >= 0; $t--) {
Chris@17: if ($this->tokens[$t]['code'] !== T_WHITESPACE) {
Chris@17: $prev = $t;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($prev !== false && $this->tokens[$prev]['code'] === T_USE) {
Chris@17: // Closure use by reference.
Chris@17: return true;
Chris@17: }
Chris@17: }//end if
Chris@17: }//end if
Chris@17:
Chris@17: // Pass by reference in function calls and assign by reference in arrays.
Chris@17: if ($this->tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS
Chris@17: || $this->tokens[$tokenBefore]['code'] === T_COMMA
Chris@17: || $this->tokens[$tokenBefore]['code'] === T_OPEN_SHORT_ARRAY
Chris@17: ) {
Chris@17: if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) {
Chris@17: return true;
Chris@17: } else {
Chris@17: $skip = Util\Tokens::$emptyTokens;
Chris@17: $skip[] = T_NS_SEPARATOR;
Chris@17: $skip[] = T_SELF;
Chris@17: $skip[] = T_PARENT;
Chris@17: $skip[] = T_STATIC;
Chris@17: $skip[] = T_STRING;
Chris@17: $skip[] = T_NAMESPACE;
Chris@17: $skip[] = T_DOUBLE_COLON;
Chris@17:
Chris@17: $nextSignificantAfter = $this->findNext(
Chris@17: $skip,
Chris@17: ($stackPtr + 1),
Chris@17: null,
Chris@17: true
Chris@17: );
Chris@17: if ($this->tokens[$nextSignificantAfter]['code'] === T_VARIABLE) {
Chris@17: return true;
Chris@17: }
Chris@17: }//end if
Chris@17: }//end if
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end isReference()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the content of the tokens from the specified start position in
Chris@17: * the token stack for the specified length.
Chris@17: *
Chris@17: * @param int $start The position to start from in the token stack.
Chris@17: * @param int $length The length of tokens to traverse from the start pos.
Chris@17: * @param bool $origContent Whether the original content or the tab replaced
Chris@17: * content should be used.
Chris@17: *
Chris@17: * @return string The token contents.
Chris@17: */
Chris@17: public function getTokensAsString($start, $length, $origContent=false)
Chris@17: {
Chris@17: if (is_int($start) === false || isset($this->tokens[$start]) === false) {
Chris@17: throw new RuntimeException('The $start position for getTokensAsString() must exist in the token stack');
Chris@17: }
Chris@17:
Chris@17: if (is_int($length) === false || $length <= 0) {
Chris@17: return '';
Chris@17: }
Chris@17:
Chris@17: $str = '';
Chris@17: $end = ($start + $length);
Chris@17: if ($end > $this->numTokens) {
Chris@17: $end = $this->numTokens;
Chris@17: }
Chris@17:
Chris@17: for ($i = $start; $i < $end; $i++) {
Chris@17: // If tabs are being converted to spaces by the tokeniser, the
Chris@17: // original content should be used instead of the converted content.
Chris@17: if ($origContent === true && isset($this->tokens[$i]['orig_content']) === true) {
Chris@17: $str .= $this->tokens[$i]['orig_content'];
Chris@17: } else {
Chris@17: $str .= $this->tokens[$i]['content'];
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return $str;
Chris@17:
Chris@17: }//end getTokensAsString()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the position of the previous specified token(s).
Chris@17: *
Chris@17: * If a value is specified, the previous token of the specified type(s)
Chris@17: * containing the specified value will be returned.
Chris@17: *
Chris@17: * Returns false if no token can be found.
Chris@17: *
Chris@18: * @param int|string|array $types The type(s) of tokens to search for.
Chris@18: * @param int $start The position to start searching from in the
Chris@18: * token stack.
Chris@18: * @param int $end The end position to fail if no token is found.
Chris@18: * if not specified or null, end will default to
Chris@18: * the start of the token stack.
Chris@18: * @param bool $exclude If true, find the previous token that is NOT of
Chris@18: * the types specified in $types.
Chris@18: * @param string $value The value that the token(s) must be equal to.
Chris@18: * If value is omitted, tokens with any value will
Chris@18: * be returned.
Chris@18: * @param bool $local If true, tokens outside the current statement
Chris@18: * will not be checked. IE. checking will stop
Chris@18: * at the previous semi-colon found.
Chris@17: *
Chris@17: * @return int|bool
Chris@17: * @see findNext()
Chris@17: */
Chris@17: public function findPrevious(
Chris@17: $types,
Chris@17: $start,
Chris@17: $end=null,
Chris@17: $exclude=false,
Chris@17: $value=null,
Chris@17: $local=false
Chris@17: ) {
Chris@17: $types = (array) $types;
Chris@17:
Chris@17: if ($end === null) {
Chris@17: $end = 0;
Chris@17: }
Chris@17:
Chris@17: for ($i = $start; $i >= $end; $i--) {
Chris@17: $found = (bool) $exclude;
Chris@17: foreach ($types as $type) {
Chris@17: if ($this->tokens[$i]['code'] === $type) {
Chris@17: $found = !$exclude;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($found === true) {
Chris@17: if ($value === null) {
Chris@17: return $i;
Chris@17: } else if ($this->tokens[$i]['content'] === $value) {
Chris@17: return $i;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($local === true) {
Chris@17: if (isset($this->tokens[$i]['scope_opener']) === true
Chris@17: && $i === $this->tokens[$i]['scope_closer']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['scope_opener'];
Chris@17: } else if (isset($this->tokens[$i]['bracket_opener']) === true
Chris@17: && $i === $this->tokens[$i]['bracket_closer']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['bracket_opener'];
Chris@17: } else if (isset($this->tokens[$i]['parenthesis_opener']) === true
Chris@17: && $i === $this->tokens[$i]['parenthesis_closer']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['parenthesis_opener'];
Chris@17: } else if ($this->tokens[$i]['code'] === T_SEMICOLON) {
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end findPrevious()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the position of the next specified token(s).
Chris@17: *
Chris@17: * If a value is specified, the next token of the specified type(s)
Chris@17: * containing the specified value will be returned.
Chris@17: *
Chris@17: * Returns false if no token can be found.
Chris@17: *
Chris@18: * @param int|string|array $types The type(s) of tokens to search for.
Chris@18: * @param int $start The position to start searching from in the
Chris@18: * token stack.
Chris@18: * @param int $end The end position to fail if no token is found.
Chris@18: * if not specified or null, end will default to
Chris@18: * the end of the token stack.
Chris@18: * @param bool $exclude If true, find the next token that is NOT of
Chris@18: * a type specified in $types.
Chris@18: * @param string $value The value that the token(s) must be equal to.
Chris@18: * If value is omitted, tokens with any value will
Chris@18: * be returned.
Chris@18: * @param bool $local If true, tokens outside the current statement
Chris@18: * will not be checked. i.e., checking will stop
Chris@18: * at the next semi-colon found.
Chris@17: *
Chris@17: * @return int|bool
Chris@17: * @see findPrevious()
Chris@17: */
Chris@17: public function findNext(
Chris@17: $types,
Chris@17: $start,
Chris@17: $end=null,
Chris@17: $exclude=false,
Chris@17: $value=null,
Chris@17: $local=false
Chris@17: ) {
Chris@17: $types = (array) $types;
Chris@17:
Chris@17: if ($end === null || $end > $this->numTokens) {
Chris@17: $end = $this->numTokens;
Chris@17: }
Chris@17:
Chris@17: for ($i = $start; $i < $end; $i++) {
Chris@17: $found = (bool) $exclude;
Chris@17: foreach ($types as $type) {
Chris@17: if ($this->tokens[$i]['code'] === $type) {
Chris@17: $found = !$exclude;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($found === true) {
Chris@17: if ($value === null) {
Chris@17: return $i;
Chris@17: } else if ($this->tokens[$i]['content'] === $value) {
Chris@17: return $i;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($local === true && $this->tokens[$i]['code'] === T_SEMICOLON) {
Chris@17: break;
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end findNext()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the position of the first non-whitespace token in a statement.
Chris@17: *
Chris@17: * @param int $start The position to start searching from in the token stack.
Chris@17: * @param int|array $ignore Token types that should not be considered stop points.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function findStartOfStatement($start, $ignore=null)
Chris@17: {
Chris@17: $endTokens = Util\Tokens::$blockOpeners;
Chris@17:
Chris@17: $endTokens[T_COLON] = true;
Chris@17: $endTokens[T_COMMA] = true;
Chris@17: $endTokens[T_DOUBLE_ARROW] = true;
Chris@17: $endTokens[T_SEMICOLON] = true;
Chris@17: $endTokens[T_OPEN_TAG] = true;
Chris@17: $endTokens[T_CLOSE_TAG] = true;
Chris@17: $endTokens[T_OPEN_SHORT_ARRAY] = true;
Chris@17:
Chris@17: if ($ignore !== null) {
Chris@17: $ignore = (array) $ignore;
Chris@17: foreach ($ignore as $code) {
Chris@18: unset($endTokens[$code]);
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: $lastNotEmpty = $start;
Chris@17:
Chris@17: for ($i = $start; $i >= 0; $i--) {
Chris@17: if (isset($endTokens[$this->tokens[$i]['code']]) === true) {
Chris@17: // Found the end of the previous statement.
Chris@17: return $lastNotEmpty;
Chris@17: }
Chris@17:
Chris@17: if (isset($this->tokens[$i]['scope_opener']) === true
Chris@17: && $i === $this->tokens[$i]['scope_closer']
Chris@17: ) {
Chris@17: // Found the end of the previous scope block.
Chris@17: return $lastNotEmpty;
Chris@17: }
Chris@17:
Chris@17: // Skip nested statements.
Chris@17: if (isset($this->tokens[$i]['bracket_opener']) === true
Chris@17: && $i === $this->tokens[$i]['bracket_closer']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['bracket_opener'];
Chris@17: } else if (isset($this->tokens[$i]['parenthesis_opener']) === true
Chris@17: && $i === $this->tokens[$i]['parenthesis_closer']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['parenthesis_opener'];
Chris@17: }
Chris@17:
Chris@17: if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
Chris@17: $lastNotEmpty = $i;
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return 0;
Chris@17:
Chris@17: }//end findStartOfStatement()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the position of the last non-whitespace token in a statement.
Chris@17: *
Chris@17: * @param int $start The position to start searching from in the token stack.
Chris@17: * @param int|array $ignore Token types that should not be considered stop points.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function findEndOfStatement($start, $ignore=null)
Chris@17: {
Chris@17: $endTokens = [
Chris@17: T_COLON => true,
Chris@17: T_COMMA => true,
Chris@17: T_DOUBLE_ARROW => true,
Chris@17: T_SEMICOLON => true,
Chris@17: T_CLOSE_PARENTHESIS => true,
Chris@17: T_CLOSE_SQUARE_BRACKET => true,
Chris@17: T_CLOSE_CURLY_BRACKET => true,
Chris@17: T_CLOSE_SHORT_ARRAY => true,
Chris@17: T_OPEN_TAG => true,
Chris@17: T_CLOSE_TAG => true,
Chris@17: ];
Chris@17:
Chris@17: if ($ignore !== null) {
Chris@17: $ignore = (array) $ignore;
Chris@17: foreach ($ignore as $code) {
Chris@18: unset($endTokens[$code]);
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: $lastNotEmpty = $start;
Chris@17:
Chris@17: for ($i = $start; $i < $this->numTokens; $i++) {
Chris@17: if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]) === true) {
Chris@17: // Found the end of the statement.
Chris@17: if ($this->tokens[$i]['code'] === T_CLOSE_PARENTHESIS
Chris@17: || $this->tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET
Chris@17: || $this->tokens[$i]['code'] === T_CLOSE_CURLY_BRACKET
Chris@17: || $this->tokens[$i]['code'] === T_CLOSE_SHORT_ARRAY
Chris@17: || $this->tokens[$i]['code'] === T_OPEN_TAG
Chris@17: || $this->tokens[$i]['code'] === T_CLOSE_TAG
Chris@17: ) {
Chris@17: return $lastNotEmpty;
Chris@17: }
Chris@17:
Chris@17: return $i;
Chris@17: }
Chris@17:
Chris@17: // Skip nested statements.
Chris@17: if (isset($this->tokens[$i]['scope_closer']) === true
Chris@17: && ($i === $this->tokens[$i]['scope_opener']
Chris@17: || $i === $this->tokens[$i]['scope_condition'])
Chris@17: ) {
Chris@17: if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
Chris@17: return $this->tokens[$i]['scope_closer'];
Chris@17: }
Chris@17:
Chris@17: $i = $this->tokens[$i]['scope_closer'];
Chris@17: } else if (isset($this->tokens[$i]['bracket_closer']) === true
Chris@17: && $i === $this->tokens[$i]['bracket_opener']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['bracket_closer'];
Chris@17: } else if (isset($this->tokens[$i]['parenthesis_closer']) === true
Chris@17: && $i === $this->tokens[$i]['parenthesis_opener']
Chris@17: ) {
Chris@17: $i = $this->tokens[$i]['parenthesis_closer'];
Chris@17: }
Chris@17:
Chris@17: if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
Chris@17: $lastNotEmpty = $i;
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return ($this->numTokens - 1);
Chris@17:
Chris@17: }//end findEndOfStatement()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the position of the first token on a line, matching given type.
Chris@17: *
Chris@17: * Returns false if no token can be found.
Chris@17: *
Chris@18: * @param int|string|array $types The type(s) of tokens to search for.
Chris@18: * @param int $start The position to start searching from in the
Chris@18: * token stack. The first token matching on
Chris@18: * this line before this token will be returned.
Chris@18: * @param bool $exclude If true, find the token that is NOT of
Chris@18: * the types specified in $types.
Chris@18: * @param string $value The value that the token must be equal to.
Chris@18: * If value is omitted, tokens with any value will
Chris@18: * be returned.
Chris@17: *
Chris@17: * @return int | bool
Chris@17: */
Chris@17: public function findFirstOnLine($types, $start, $exclude=false, $value=null)
Chris@17: {
Chris@17: if (is_array($types) === false) {
Chris@17: $types = [$types];
Chris@17: }
Chris@17:
Chris@17: $foundToken = false;
Chris@17:
Chris@17: for ($i = $start; $i >= 0; $i--) {
Chris@17: if ($this->tokens[$i]['line'] < $this->tokens[$start]['line']) {
Chris@17: break;
Chris@17: }
Chris@17:
Chris@17: $found = $exclude;
Chris@17: foreach ($types as $type) {
Chris@17: if ($exclude === false) {
Chris@17: if ($this->tokens[$i]['code'] === $type) {
Chris@17: $found = true;
Chris@17: break;
Chris@17: }
Chris@17: } else {
Chris@17: if ($this->tokens[$i]['code'] === $type) {
Chris@17: $found = false;
Chris@17: break;
Chris@17: }
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: if ($found === true) {
Chris@17: if ($value === null) {
Chris@17: $foundToken = $i;
Chris@17: } else if ($this->tokens[$i]['content'] === $value) {
Chris@17: $foundToken = $i;
Chris@17: }
Chris@17: }
Chris@17: }//end for
Chris@17:
Chris@17: return $foundToken;
Chris@17:
Chris@17: }//end findFirstOnLine()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Determine if the passed token has a condition of one of the passed types.
Chris@17: *
Chris@18: * @param int $stackPtr The position of the token we are checking.
Chris@18: * @param int|string|array $types The type(s) of tokens to search for.
Chris@17: *
Chris@17: * @return boolean
Chris@17: */
Chris@17: public function hasCondition($stackPtr, $types)
Chris@17: {
Chris@17: // Check for the existence of the token.
Chris@17: if (isset($this->tokens[$stackPtr]) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // Make sure the token has conditions.
Chris@17: if (isset($this->tokens[$stackPtr]['conditions']) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $types = (array) $types;
Chris@17: $conditions = $this->tokens[$stackPtr]['conditions'];
Chris@17:
Chris@17: foreach ($types as $type) {
Chris@18: if (in_array($type, $conditions, true) === true) {
Chris@17: // We found a token with the required type.
Chris@17: return true;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end hasCondition()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Return the position of the condition for the passed token.
Chris@17: *
Chris@17: * Returns FALSE if the token does not have the condition.
Chris@17: *
Chris@18: * @param int $stackPtr The position of the token we are checking.
Chris@18: * @param int|string $type The type of token to search for.
Chris@17: *
Chris@17: * @return int
Chris@17: */
Chris@17: public function getCondition($stackPtr, $type)
Chris@17: {
Chris@17: // Check for the existence of the token.
Chris@17: if (isset($this->tokens[$stackPtr]) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: // Make sure the token has conditions.
Chris@17: if (isset($this->tokens[$stackPtr]['conditions']) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $conditions = $this->tokens[$stackPtr]['conditions'];
Chris@17: foreach ($conditions as $token => $condition) {
Chris@17: if ($condition === $type) {
Chris@17: return $token;
Chris@17: }
Chris@17: }
Chris@17:
Chris@17: return false;
Chris@17:
Chris@17: }//end getCondition()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the name of the class that the specified class extends.
Chris@17: * (works for classes, anonymous classes and interfaces)
Chris@17: *
Chris@17: * Returns FALSE on error or if there is no extended class name.
Chris@17: *
Chris@17: * @param int $stackPtr The stack position of the class.
Chris@17: *
Chris@17: * @return string|false
Chris@17: */
Chris@17: public function findExtendedClassName($stackPtr)
Chris@17: {
Chris@17: // Check for the existence of the token.
Chris@17: if (isset($this->tokens[$stackPtr]) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_CLASS
Chris@17: && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
Chris@17: && $this->tokens[$stackPtr]['code'] !== T_INTERFACE
Chris@17: ) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: if (isset($this->tokens[$stackPtr]['scope_opener']) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
Chris@17: $extendsIndex = $this->findNext(T_EXTENDS, $stackPtr, $classOpenerIndex);
Chris@17: if (false === $extendsIndex) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $find = [
Chris@17: T_NS_SEPARATOR,
Chris@17: T_STRING,
Chris@17: T_WHITESPACE,
Chris@17: ];
Chris@17:
Chris@17: $end = $this->findNext($find, ($extendsIndex + 1), ($classOpenerIndex + 1), true);
Chris@17: $name = $this->getTokensAsString(($extendsIndex + 1), ($end - $extendsIndex - 1));
Chris@17: $name = trim($name);
Chris@17:
Chris@17: if ($name === '') {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: return $name;
Chris@17:
Chris@17: }//end findExtendedClassName()
Chris@17:
Chris@17:
Chris@17: /**
Chris@17: * Returns the names of the interfaces that the specified class implements.
Chris@17: *
Chris@17: * Returns FALSE on error or if there are no implemented interface names.
Chris@17: *
Chris@17: * @param int $stackPtr The stack position of the class.
Chris@17: *
Chris@17: * @return array|false
Chris@17: */
Chris@17: public function findImplementedInterfaceNames($stackPtr)
Chris@17: {
Chris@17: // Check for the existence of the token.
Chris@17: if (isset($this->tokens[$stackPtr]) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: if ($this->tokens[$stackPtr]['code'] !== T_CLASS
Chris@17: && $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
Chris@17: ) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: if (isset($this->tokens[$stackPtr]['scope_closer']) === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $classOpenerIndex = $this->tokens[$stackPtr]['scope_opener'];
Chris@17: $implementsIndex = $this->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex);
Chris@17: if ($implementsIndex === false) {
Chris@17: return false;
Chris@17: }
Chris@17:
Chris@17: $find = [
Chris@17: T_NS_SEPARATOR,
Chris@17: T_STRING,
Chris@17: T_WHITESPACE,
Chris@17: T_COMMA,
Chris@17: ];
Chris@17:
Chris@17: $end = $this->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true);
Chris@17: $name = $this->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1));
Chris@17: $name = trim($name);
Chris@17:
Chris@17: if ($name === '') {
Chris@17: return false;
Chris@17: } else {
Chris@17: $names = explode(',', $name);
Chris@17: $names = array_map('trim', $names);
Chris@17: return $names;
Chris@17: }
Chris@17:
Chris@17: }//end findImplementedInterfaceNames()
Chris@17:
Chris@17:
Chris@17: }//end class