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; Chris@17: Chris@17: use PHP_CodeSniffer\Util; Chris@17: use PHP_CodeSniffer\Exceptions\RuntimeException; Chris@17: Chris@17: class Ruleset Chris@17: { Chris@17: Chris@17: /** Chris@17: * The name of the coding standard being used. Chris@17: * Chris@17: * If a top-level standard includes other standards, or sniffs Chris@17: * from other standards, only the name of the top-level standard Chris@17: * will be stored in here. Chris@17: * Chris@17: * If multiple top-level standards are being loaded into Chris@17: * a single ruleset object, this will store a comma separated list Chris@17: * of the top-level standard names. Chris@17: * Chris@17: * @var string Chris@17: */ Chris@17: public $name = ''; Chris@17: Chris@17: /** Chris@17: * A list of file paths for the ruleset files being used. Chris@17: * Chris@17: * @var string[] Chris@17: */ Chris@17: public $paths = []; Chris@17: Chris@17: /** Chris@17: * A list of regular expressions used to ignore specific sniffs for files and folders. Chris@17: * Chris@17: * Is also used to set global exclude patterns. Chris@17: * The key is the regular expression and the value is the type Chris@17: * of ignore pattern (absolute or relative). Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: public $ignorePatterns = []; Chris@17: Chris@17: /** Chris@17: * A list of regular expressions used to include specific sniffs for files and folders. Chris@17: * Chris@17: * The key is the sniff code and the value is an array with Chris@17: * the key being a regular expression and the value is the type Chris@17: * of ignore pattern (absolute or relative). Chris@17: * Chris@17: * @var array> Chris@17: */ Chris@17: public $includePatterns = []; Chris@17: Chris@17: /** Chris@17: * An array of sniff objects that are being used to check files. Chris@17: * Chris@17: * The key is the fully qualified name of the sniff class Chris@17: * and the value is the sniff object. Chris@17: * Chris@18: * @var array Chris@17: */ Chris@17: public $sniffs = []; Chris@17: Chris@17: /** Chris@17: * A mapping of sniff codes to fully qualified class names. Chris@17: * Chris@17: * The key is the sniff code and the value Chris@17: * is the fully qualified name of the sniff class. Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: public $sniffCodes = []; Chris@17: Chris@17: /** Chris@17: * An array of token types and the sniffs that are listening for them. Chris@17: * Chris@17: * The key is the token name being listened for and the value Chris@17: * is the sniff object. Chris@17: * Chris@18: * @var array Chris@17: */ Chris@17: public $tokenListeners = []; Chris@17: Chris@17: /** Chris@17: * An array of rules from the ruleset.xml file. Chris@17: * Chris@17: * It may be empty, indicating that the ruleset does not override Chris@17: * any of the default sniff settings. Chris@17: * Chris@17: * @var array Chris@17: */ Chris@17: public $ruleset = []; Chris@17: Chris@17: /** Chris@17: * The directories that the processed rulesets are in. Chris@17: * Chris@17: * @var string[] Chris@17: */ Chris@17: protected $rulesetDirs = []; Chris@17: Chris@17: /** Chris@17: * The config data for the run. Chris@17: * Chris@17: * @var \PHP_CodeSniffer\Config Chris@17: */ Chris@17: private $config = null; Chris@17: Chris@17: Chris@17: /** Chris@17: * Initialise the ruleset that the run will use. Chris@17: * 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(Config $config) Chris@17: { Chris@17: // Ignore sniff restrictions if caching is on. Chris@17: $restrictions = []; Chris@17: $exclusions = []; Chris@17: if ($config->cache === false) { Chris@17: $restrictions = $config->sniffs; Chris@17: $exclusions = $config->exclude; Chris@17: } Chris@17: Chris@17: $this->config = $config; Chris@17: $sniffs = []; Chris@17: Chris@17: $standardPaths = []; Chris@17: foreach ($config->standards as $standard) { Chris@17: $installed = Util\Standards::getInstalledStandardPath($standard); Chris@17: if ($installed === null) { Chris@17: $standard = Util\Common::realpath($standard); Chris@17: if (is_dir($standard) === true Chris@17: && is_file(Util\Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true Chris@17: ) { Chris@17: $standard = Util\Common::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); Chris@17: } Chris@17: } else { Chris@17: $standard = $installed; Chris@17: } Chris@17: Chris@17: $standardPaths[] = $standard; Chris@17: } Chris@17: Chris@17: foreach ($standardPaths as $standard) { Chris@17: $ruleset = @simplexml_load_string(file_get_contents($standard)); Chris@17: if ($ruleset !== false) { Chris@17: $standardName = (string) $ruleset['name']; Chris@17: if ($this->name !== '') { Chris@17: $this->name .= ', '; Chris@17: } Chris@17: Chris@18: $this->name .= $standardName; Chris@17: Chris@17: // Allow autoloading of custom files inside this standard. Chris@17: if (isset($ruleset['namespace']) === true) { Chris@17: $namespace = (string) $ruleset['namespace']; Chris@17: } else { Chris@17: $namespace = basename(dirname($standard)); Chris@17: } Chris@17: Chris@17: Autoload::addSearchPath(dirname($standard), $namespace); Chris@17: } Chris@17: Chris@17: if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) { Chris@17: // In unit tests, only register the sniffs that the test wants and not the entire standard. Chris@17: try { Chris@17: foreach ($restrictions as $restriction) { Chris@17: $sniffs = array_merge($sniffs, $this->expandRulesetReference($restriction, dirname($standard))); Chris@17: } Chris@17: } catch (RuntimeException $e) { Chris@17: // Sniff reference could not be expanded, which probably means this Chris@17: // is an installed standard. Let the unit test system take care of Chris@17: // setting the correct sniff for testing. Chris@17: return; Chris@17: } Chris@17: Chris@17: break; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@17: echo "Registering sniffs in the $standardName standard... "; Chris@17: if (count($config->standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) { Chris@17: echo PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: $sniffs = array_merge($sniffs, $this->processRuleset($standard)); Chris@17: }//end foreach Chris@17: Chris@17: $sniffRestrictions = []; Chris@17: foreach ($restrictions as $sniffCode) { Chris@17: $parts = explode('.', strtolower($sniffCode)); Chris@17: $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; Chris@17: $sniffRestrictions[$sniffName] = true; Chris@17: } Chris@17: Chris@17: $sniffExclusions = []; Chris@17: foreach ($exclusions as $sniffCode) { Chris@17: $parts = explode('.', strtolower($sniffCode)); Chris@17: $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; Chris@17: $sniffExclusions[$sniffName] = true; Chris@17: } Chris@17: Chris@17: $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions); Chris@17: $this->populateTokenListeners(); Chris@17: Chris@17: $numSniffs = count($this->sniffs); Chris@17: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@17: echo "DONE ($numSniffs sniffs registered)".PHP_EOL; Chris@17: } Chris@17: Chris@17: if ($numSniffs === 0) { Chris@17: throw new RuntimeException('No sniffs were registered'); Chris@17: } Chris@17: Chris@17: }//end __construct() Chris@17: Chris@17: Chris@17: /** Chris@17: * Prints a report showing the sniffs contained in a standard. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: public function explain() Chris@17: { Chris@17: $sniffs = array_keys($this->sniffCodes); Chris@17: sort($sniffs); Chris@17: Chris@17: ob_start(); Chris@17: Chris@17: $lastStandard = null; Chris@17: $lastCount = ''; Chris@17: $sniffCount = count($sniffs); Chris@17: Chris@17: // Add a dummy entry to the end so we loop Chris@17: // one last time and clear the output buffer. Chris@17: $sniffs[] = ''; Chris@17: Chris@17: echo PHP_EOL."The $this->name standard contains $sniffCount sniffs".PHP_EOL; Chris@17: Chris@17: ob_start(); Chris@17: Chris@17: foreach ($sniffs as $i => $sniff) { Chris@17: if ($i === $sniffCount) { Chris@17: $currentStandard = null; Chris@17: } else { Chris@17: $currentStandard = substr($sniff, 0, strpos($sniff, '.')); Chris@17: if ($lastStandard === null) { Chris@17: $lastStandard = $currentStandard; Chris@17: } Chris@17: } Chris@17: Chris@17: if ($currentStandard !== $lastStandard) { Chris@17: $sniffList = ob_get_contents(); Chris@17: ob_end_clean(); Chris@17: Chris@17: echo PHP_EOL.$lastStandard.' ('.$lastCount.' sniff'; Chris@17: if ($lastCount > 1) { Chris@17: echo 's'; Chris@17: } Chris@17: Chris@17: echo ')'.PHP_EOL; Chris@17: echo str_repeat('-', (strlen($lastStandard.$lastCount) + 10)); Chris@17: echo PHP_EOL; Chris@17: echo $sniffList; Chris@17: Chris@17: $lastStandard = $currentStandard; Chris@17: $lastCount = 0; Chris@17: Chris@17: if ($currentStandard === null) { Chris@17: break; Chris@17: } Chris@17: Chris@17: ob_start(); Chris@17: }//end if Chris@17: Chris@17: echo ' '.$sniff.PHP_EOL; Chris@17: $lastCount++; Chris@17: }//end foreach Chris@17: Chris@17: }//end explain() Chris@17: Chris@17: Chris@17: /** Chris@17: * Processes a single ruleset and returns a list of the sniffs it represents. Chris@17: * Chris@17: * Rules founds within the ruleset are processed immediately, but sniff classes Chris@17: * are not registered by this method. Chris@17: * Chris@17: * @param string $rulesetPath The path to a ruleset XML file. Chris@17: * @param int $depth How many nested processing steps we are in. This Chris@17: * is only used for debug output. Chris@17: * Chris@17: * @return string[] Chris@18: * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the ruleset path is invalid. Chris@17: */ Chris@17: public function processRuleset($rulesetPath, $depth=0) Chris@17: { Chris@17: $rulesetPath = Util\Common::realpath($rulesetPath); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo 'Processing ruleset '.Util\Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: Chris@18: libxml_use_internal_errors(true); Chris@18: $ruleset = simplexml_load_string(file_get_contents($rulesetPath)); Chris@17: if ($ruleset === false) { Chris@18: $errorMsg = "Ruleset $rulesetPath is not valid".PHP_EOL; Chris@18: $errors = libxml_get_errors(); Chris@18: foreach ($errors as $error) { Chris@18: $errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message; Chris@18: } Chris@18: Chris@18: libxml_clear_errors(); Chris@18: throw new RuntimeException($errorMsg); Chris@17: } Chris@17: Chris@18: libxml_use_internal_errors(false); Chris@18: Chris@17: $ownSniffs = []; Chris@17: $includedSniffs = []; Chris@17: $excludedSniffs = []; Chris@17: Chris@18: $this->paths[] = $rulesetPath; Chris@17: $rulesetDir = dirname($rulesetPath); Chris@17: $this->rulesetDirs[] = $rulesetDir; Chris@17: Chris@17: $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs'; Chris@17: if (is_dir($sniffDir) === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\tAdding sniff files from ".Util\Common::stripBasepath($sniffDir, $this->config->basepath).' directory'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $ownSniffs = $this->expandSniffDirectory($sniffDir, $depth); Chris@17: } Chris@17: Chris@17: // Included custom autoloaders. Chris@17: foreach ($ruleset->{'autoload'} as $autoload) { Chris@17: if ($this->shouldProcessElement($autoload) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $autoloadPath = (string) $autoload; Chris@17: if (is_file($autoloadPath) === false) { Chris@17: $autoloadPath = Util\Common::realPath(dirname($rulesetPath).DIRECTORY_SEPARATOR.$autoloadPath); Chris@17: } Chris@17: Chris@17: if ($autoloadPath === false) { Chris@17: throw new RuntimeException('The specified autoload file "'.$autoload.'" does not exist'); Chris@17: } Chris@17: Chris@17: include_once $autoloadPath; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> included autoloader $autoloadPath".PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: // Process custom sniff config settings. Chris@17: foreach ($ruleset->{'config'} as $config) { Chris@17: if ($this->shouldProcessElement($config) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: Config::setConfigData((string) $config['name'], (string) $config['value'], true); Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: foreach ($ruleset->rule as $rule) { Chris@17: if (isset($rule['ref']) === false Chris@17: || $this->shouldProcessElement($rule) === false Chris@17: ) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL; Chris@17: } Chris@17: Chris@17: $expandedSniffs = $this->expandRulesetReference((string) $rule['ref'], $rulesetDir, $depth); Chris@17: $newSniffs = array_diff($expandedSniffs, $includedSniffs); Chris@17: $includedSniffs = array_merge($includedSniffs, $expandedSniffs); Chris@17: Chris@17: $parts = explode('.', $rule['ref']); Chris@17: if (count($parts) === 4 Chris@17: && $parts[0] !== '' Chris@17: && $parts[1] !== '' Chris@17: && $parts[2] !== '' Chris@17: ) { Chris@17: $sniffCode = $parts[0].'.'.$parts[1].'.'.$parts[2]; Chris@17: if (isset($this->ruleset[$sniffCode]['severity']) === true Chris@17: && $this->ruleset[$sniffCode]['severity'] === 0 Chris@17: ) { Chris@17: // This sniff code has already been turned off, but now Chris@17: // it is being explicitly included again, so turn it back on. Chris@17: $this->ruleset[(string) $rule['ref']]['severity'] = 5; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* disabling sniff exclusion for specific message code *".PHP_EOL; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> severity set to 5".PHP_EOL; Chris@17: } Chris@17: } else if (empty($newSniffs) === false) { Chris@17: $newSniff = $newSniffs[0]; Chris@18: if (in_array($newSniff, $ownSniffs, true) === false) { Chris@17: // Including a sniff that hasn't been included higher up, but Chris@17: // only including a single message from it. So turn off all messages in Chris@17: // the sniff, except this one. Chris@17: $this->ruleset[$sniffCode]['severity'] = 0; Chris@17: $this->ruleset[(string) $rule['ref']]['severity'] = 5; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\tExcluding sniff \"".$sniffCode.'" except for "'.$parts[3].'"'.PHP_EOL; Chris@17: } Chris@17: } Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: if (isset($rule->exclude) === true) { Chris@17: foreach ($rule->exclude as $exclude) { Chris@17: if (isset($exclude['name']) === false) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* ignoring empty exclude rule *".PHP_EOL; Chris@17: echo "\t\t\t=> ".$exclude->asXML().PHP_EOL; Chris@17: } Chris@17: Chris@17: continue; Chris@17: } Chris@17: Chris@17: if ($this->shouldProcessElement($exclude) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL; Chris@17: } Chris@17: Chris@17: // Check if a single code is being excluded, which is a shortcut Chris@17: // for setting the severity of the message to 0. Chris@17: $parts = explode('.', $exclude['name']); Chris@17: if (count($parts) === 4) { Chris@17: $this->ruleset[(string) $exclude['name']]['severity'] = 0; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> severity set to 0".PHP_EOL; Chris@17: } Chris@17: } else { Chris@17: $excludedSniffs = array_merge( Chris@17: $excludedSniffs, Chris@18: $this->expandRulesetReference((string) $exclude['name'], $rulesetDir, ($depth + 1)) Chris@17: ); Chris@17: } Chris@17: }//end foreach Chris@17: }//end if Chris@17: Chris@17: $this->processRule($rule, $newSniffs, $depth); Chris@17: }//end foreach Chris@17: Chris@17: // Process custom command line arguments. Chris@17: $cliArgs = []; Chris@17: foreach ($ruleset->{'arg'} as $arg) { Chris@17: if ($this->shouldProcessElement($arg) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($arg['name']) === true) { Chris@17: $argString = '--'.(string) $arg['name']; Chris@17: if (isset($arg['value']) === true) { Chris@17: $argString .= '='.(string) $arg['value']; Chris@17: } Chris@17: } else { Chris@17: $argString = '-'.(string) $arg['value']; Chris@17: } Chris@17: Chris@17: $cliArgs[] = $argString; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> set command line value $argString".PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: // Set custom php ini values as CLI args. Chris@17: foreach ($ruleset->{'ini'} as $arg) { Chris@17: if ($this->shouldProcessElement($arg) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($arg['name']) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $name = (string) $arg['name']; Chris@17: $argString = $name; Chris@17: if (isset($arg['value']) === true) { Chris@17: $value = (string) $arg['value']; Chris@17: $argString .= "=$value"; Chris@17: } else { Chris@17: $value = 'true'; Chris@17: } Chris@17: Chris@17: $cliArgs[] = '-d'; Chris@17: $cliArgs[] = $argString; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> set PHP ini value $name to $value".PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: if (empty($this->config->files) === true) { Chris@17: // Process hard-coded file paths. Chris@17: foreach ($ruleset->{'file'} as $file) { Chris@17: $file = (string) $file; Chris@17: $cliArgs[] = $file; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> added \"$file\" to the file list".PHP_EOL; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: if (empty($cliArgs) === false) { Chris@17: // Change the directory so all relative paths are worked Chris@17: // out based on the location of the ruleset instead of Chris@17: // the location of the user. Chris@17: $inPhar = Util\Common::isPharFile($rulesetDir); Chris@17: if ($inPhar === false) { Chris@17: $currentDir = getcwd(); Chris@17: chdir($rulesetDir); Chris@17: } Chris@17: Chris@17: $this->config->setCommandLineValues($cliArgs); Chris@17: Chris@17: if ($inPhar === false) { Chris@17: chdir($currentDir); Chris@17: } Chris@17: } Chris@17: Chris@17: // Process custom ignore pattern rules. Chris@17: foreach ($ruleset->{'exclude-pattern'} as $pattern) { Chris@17: if ($this->shouldProcessElement($pattern) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($pattern['type']) === false) { Chris@17: $pattern['type'] = 'absolute'; Chris@17: } Chris@17: Chris@17: $this->ignorePatterns[(string) $pattern] = (string) $pattern['type']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs)); Chris@17: $excludedSniffs = array_unique($excludedSniffs); Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: $included = count($includedSniffs); Chris@17: $excluded = count($excludedSniffs); Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL; Chris@17: } Chris@17: Chris@17: // Merge our own sniff list with our externally included Chris@17: // sniff list, but filter out any excluded sniffs. Chris@17: $files = []; Chris@17: foreach ($includedSniffs as $sniff) { Chris@18: if (in_array($sniff, $excludedSniffs, true) === true) { Chris@17: continue; Chris@17: } else { Chris@17: $files[] = Util\Common::realpath($sniff); Chris@17: } Chris@17: } Chris@17: Chris@17: return $files; Chris@17: Chris@17: }//end processRuleset() Chris@17: Chris@17: Chris@17: /** Chris@17: * Expands a directory into a list of sniff files within. Chris@17: * Chris@17: * @param string $directory The path to a directory. Chris@17: * @param int $depth How many nested processing steps we are in. This Chris@17: * is only used for debug output. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: private function expandSniffDirectory($directory, $depth=0) Chris@17: { Chris@17: $sniffs = []; Chris@17: Chris@17: $rdi = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS); Chris@17: $di = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD); Chris@17: Chris@17: $dirLen = strlen($directory); Chris@17: Chris@17: foreach ($di as $file) { Chris@17: $filename = $file->getFilename(); Chris@17: Chris@17: // Skip hidden files. Chris@17: if (substr($filename, 0, 1) === '.') { Chris@17: continue; Chris@17: } Chris@17: Chris@17: // We are only interested in PHP and sniff files. Chris@17: $fileParts = explode('.', $filename); Chris@17: if (array_pop($fileParts) !== 'php') { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $basename = basename($filename, '.php'); Chris@17: if (substr($basename, -5) !== 'Sniff') { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $path = $file->getPathname(); Chris@17: Chris@17: // Skip files in hidden directories within the Sniffs directory of this Chris@17: // standard. We use the offset with strpos() to allow hidden directories Chris@17: // before, valid example: Chris@17: // /home/foo/.composer/vendor/squiz/custom_tool/MyStandard/Sniffs/... Chris@17: if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> ".Util\Common::stripBasepath($path, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: Chris@17: $sniffs[] = $path; Chris@17: }//end foreach Chris@17: Chris@17: return $sniffs; Chris@17: Chris@17: }//end expandSniffDirectory() Chris@17: Chris@17: Chris@17: /** Chris@17: * Expands a ruleset reference into a list of sniff files. Chris@17: * Chris@17: * @param string $ref The reference from the ruleset XML file. Chris@17: * @param string $rulesetDir The directory of the ruleset XML file, used to Chris@17: * evaluate relative paths. Chris@17: * @param int $depth How many nested processing steps we are in. This Chris@17: * is only used for debug output. Chris@17: * Chris@17: * @return array Chris@18: * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the reference is invalid. Chris@17: */ Chris@17: private function expandRulesetReference($ref, $rulesetDir, $depth=0) Chris@17: { Chris@17: // Ignore internal sniffs codes as they are used to only Chris@17: // hide and change internal messages. Chris@17: if (substr($ref, 0, 9) === 'Internal.') { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* ignoring internal sniff code *".PHP_EOL; Chris@17: } Chris@17: Chris@17: return []; Chris@17: } Chris@17: Chris@17: // As sniffs can't begin with a full stop, assume references in Chris@17: // this format are relative paths and attempt to convert them Chris@17: // to absolute paths. If this fails, let the reference run through Chris@17: // the normal checks and have it fail as normal. Chris@17: if (substr($ref, 0, 1) === '.') { Chris@17: $realpath = Util\Common::realpath($rulesetDir.'/'.$ref); Chris@17: if ($realpath !== false) { Chris@17: $ref = $realpath; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: // As sniffs can't begin with a tilde, assume references in Chris@17: // this format are relative to the user's home directory. Chris@17: if (substr($ref, 0, 2) === '~/') { Chris@17: $realpath = Util\Common::realpath($ref); Chris@17: if ($realpath !== false) { Chris@17: $ref = $realpath; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: if (is_file($ref) === true) { Chris@17: if (substr($ref, -9) === 'Sniff.php') { Chris@17: // A single external sniff. Chris@17: $this->rulesetDirs[] = dirname(dirname(dirname($ref))); Chris@17: return [$ref]; Chris@17: } Chris@17: } else { Chris@17: // See if this is a whole standard being referenced. Chris@17: $path = Util\Standards::getInstalledStandardPath($ref); Chris@17: if (Util\Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { Chris@17: // If the ruleset exists inside the phar file, use it. Chris@17: if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { Chris@18: $path .= DIRECTORY_SEPARATOR.'ruleset.xml'; Chris@17: } else { Chris@17: $path = null; Chris@17: } Chris@17: } Chris@17: Chris@17: if ($path !== null) { Chris@17: $ref = $path; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: } else if (is_dir($ref) === false) { Chris@17: // Work out the sniff path. Chris@17: $sepPos = strpos($ref, DIRECTORY_SEPARATOR); Chris@17: if ($sepPos !== false) { Chris@17: $stdName = substr($ref, 0, $sepPos); Chris@17: $path = substr($ref, $sepPos); Chris@17: } else { Chris@17: $parts = explode('.', $ref); Chris@17: $stdName = $parts[0]; Chris@17: if (count($parts) === 1) { Chris@17: // A whole standard? Chris@17: $path = ''; Chris@17: } else if (count($parts) === 2) { Chris@17: // A directory of sniffs? Chris@17: $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1]; Chris@17: } else { Chris@17: // A single sniff? Chris@17: $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php'; Chris@17: } Chris@17: } Chris@17: Chris@17: $newRef = false; Chris@17: $stdPath = Util\Standards::getInstalledStandardPath($stdName); Chris@17: if ($stdPath !== null && $path !== '') { Chris@17: if (Util\Common::isPharFile($stdPath) === true Chris@17: && strpos($stdPath, 'ruleset.xml') === false Chris@17: ) { Chris@17: // Phar files can only return the directory, Chris@17: // since ruleset can be omitted if building one standard. Chris@17: $newRef = Util\Common::realpath($stdPath.$path); Chris@17: } else { Chris@17: $newRef = Util\Common::realpath(dirname($stdPath).$path); Chris@17: } Chris@17: } Chris@17: Chris@17: if ($newRef === false) { Chris@17: // The sniff is not locally installed, so check if it is being Chris@17: // referenced as a remote sniff outside the install. We do this Chris@17: // by looking through all directories where we have found ruleset Chris@17: // files before, looking for ones for this particular standard, Chris@17: // and seeing if it is in there. Chris@17: foreach ($this->rulesetDirs as $dir) { Chris@17: if (strtolower(basename($dir)) !== strtolower($stdName)) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $newRef = Util\Common::realpath($dir.$path); Chris@17: Chris@17: if ($newRef !== false) { Chris@17: $ref = $newRef; Chris@17: } Chris@17: } Chris@17: } else { Chris@17: $ref = $newRef; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> ".Util\Common::stripBasepath($ref, $this->config->basepath).PHP_EOL; Chris@17: } Chris@17: }//end if Chris@17: }//end if Chris@17: Chris@17: if (is_dir($ref) === true) { Chris@17: if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { Chris@17: // We are referencing an external coding standard. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2)); Chris@17: } else { Chris@17: // We are referencing a whole directory of sniffs. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL; Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\tAdding sniff files from directory".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $this->expandSniffDirectory($ref, ($depth + 1)); Chris@17: } Chris@17: } else { Chris@17: if (is_file($ref) === false) { Chris@17: $error = "Referenced sniff \"$ref\" does not exist"; Chris@17: throw new RuntimeException($error); Chris@17: } Chris@17: Chris@17: if (substr($ref, -9) === 'Sniff.php') { Chris@17: // A single sniff. Chris@17: return [$ref]; Chris@17: } else { Chris@17: // Assume an external ruleset.xml file. Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL; Chris@17: } Chris@17: Chris@17: return $this->processRuleset($ref, ($depth + 2)); Chris@17: } Chris@17: }//end if Chris@17: Chris@17: }//end expandRulesetReference() Chris@17: Chris@17: Chris@17: /** Chris@17: * Processes a rule from a ruleset XML file, overriding built-in defaults. Chris@17: * Chris@18: * @param \SimpleXMLElement $rule The rule object from a ruleset XML file. Chris@18: * @param string[] $newSniffs An array of sniffs that got included by this rule. Chris@18: * @param int $depth How many nested processing steps we are in. Chris@18: * This is only used for debug output. Chris@17: * Chris@17: * @return void Chris@18: * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If rule settings are invalid. Chris@17: */ Chris@17: private function processRule($rule, $newSniffs, $depth=0) Chris@17: { Chris@17: $ref = (string) $rule['ref']; Chris@17: $todo = [$ref]; Chris@17: Chris@17: $parts = explode('.', $ref); Chris@17: if (count($parts) <= 2) { Chris@17: // We are processing a standard or a category of sniffs. Chris@17: foreach ($newSniffs as $sniffFile) { Chris@17: $parts = explode(DIRECTORY_SEPARATOR, $sniffFile); Chris@17: $sniffName = array_pop($parts); Chris@17: $sniffCategory = array_pop($parts); Chris@17: array_pop($parts); Chris@17: $sniffStandard = array_pop($parts); Chris@17: $todo[] = $sniffStandard.'.'.$sniffCategory.'.'.substr($sniffName, 0, -9); Chris@17: } Chris@17: } Chris@17: Chris@17: foreach ($todo as $code) { Chris@17: // Custom severity. Chris@17: if (isset($rule->severity) === true Chris@17: && $this->shouldProcessElement($rule->severity) === true Chris@17: ) { Chris@17: if (isset($this->ruleset[$code]) === false) { Chris@17: $this->ruleset[$code] = []; Chris@17: } Chris@17: Chris@17: $this->ruleset[$code]['severity'] = (int) $rule->severity; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> severity set to ".(int) $rule->severity; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: // Custom message type. Chris@17: if (isset($rule->type) === true Chris@17: && $this->shouldProcessElement($rule->type) === true Chris@17: ) { Chris@17: if (isset($this->ruleset[$code]) === false) { Chris@17: $this->ruleset[$code] = []; Chris@17: } Chris@17: Chris@17: $type = strtolower((string) $rule->type); Chris@17: if ($type !== 'error' && $type !== 'warning') { Chris@17: throw new RuntimeException("Message type \"$type\" is invalid; must be \"error\" or \"warning\""); Chris@17: } Chris@17: Chris@17: $this->ruleset[$code]['type'] = $type; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> message type set to ".(string) $rule->type; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: }//end if Chris@17: Chris@17: // Custom message. Chris@17: if (isset($rule->message) === true Chris@17: && $this->shouldProcessElement($rule->message) === true Chris@17: ) { Chris@17: if (isset($this->ruleset[$code]) === false) { Chris@17: $this->ruleset[$code] = []; Chris@17: } Chris@17: Chris@17: $this->ruleset[$code]['message'] = (string) $rule->message; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> message set to ".(string) $rule->message; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: // Custom properties. Chris@17: if (isset($rule->properties) === true Chris@17: && $this->shouldProcessElement($rule->properties) === true Chris@17: ) { Chris@17: foreach ($rule->properties->property as $prop) { Chris@17: if ($this->shouldProcessElement($prop) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($this->ruleset[$code]) === false) { Chris@17: $this->ruleset[$code] = [ Chris@17: 'properties' => [], Chris@17: ]; Chris@17: } else if (isset($this->ruleset[$code]['properties']) === false) { Chris@17: $this->ruleset[$code]['properties'] = []; Chris@17: } Chris@17: Chris@17: $name = (string) $prop['name']; Chris@17: if (isset($prop['type']) === true Chris@17: && (string) $prop['type'] === 'array' Chris@17: ) { Chris@17: $values = []; Chris@17: if (isset($prop['extend']) === true Chris@17: && (string) $prop['extend'] === 'true' Chris@17: && isset($this->ruleset[$code]['properties'][$name]) === true Chris@17: ) { Chris@17: $values = $this->ruleset[$code]['properties'][$name]; Chris@17: } Chris@17: Chris@17: if (isset($prop->element) === true) { Chris@17: $printValue = ''; Chris@17: foreach ($prop->element as $element) { Chris@17: if ($this->shouldProcessElement($element) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $value = (string) $element['value']; Chris@17: if (isset($element['key']) === true) { Chris@17: $key = (string) $element['key']; Chris@17: $values[$key] = $value; Chris@17: $printValue .= $key.'=>'.$value.','; Chris@17: } else { Chris@17: $values[] = $value; Chris@17: $printValue .= $value.','; Chris@17: } Chris@17: } Chris@17: Chris@17: $printValue = rtrim($printValue, ','); Chris@17: } else { Chris@17: $value = (string) $prop['value']; Chris@17: $printValue = $value; Chris@17: foreach (explode(',', $value) as $val) { Chris@17: list($k, $v) = explode('=>', $val.'=>'); Chris@17: if ($v !== '') { Chris@17: $values[trim($k)] = trim($v); Chris@17: } else { Chris@17: $values[] = trim($k); Chris@17: } Chris@17: } Chris@17: }//end if Chris@17: Chris@17: $this->ruleset[$code]['properties'][$name] = $values; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> array property \"$name\" set to \"$printValue\""; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: } else { Chris@17: $this->ruleset[$code]['properties'][$name] = (string) $prop['value']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: }//end if Chris@17: }//end foreach Chris@17: }//end if Chris@17: Chris@17: // Ignore patterns. Chris@17: foreach ($rule->{'exclude-pattern'} as $pattern) { Chris@17: if ($this->shouldProcessElement($pattern) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($this->ignorePatterns[$code]) === false) { Chris@17: $this->ignorePatterns[$code] = []; Chris@17: } Chris@17: Chris@17: if (isset($pattern['type']) === false) { Chris@17: $pattern['type'] = 'absolute'; Chris@17: } Chris@17: Chris@17: $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> added rule-specific ".(string) $pattern['type'].' ignore pattern'; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo ': '.(string) $pattern.PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: // Include patterns. Chris@17: foreach ($rule->{'include-pattern'} as $pattern) { Chris@17: if ($this->shouldProcessElement($pattern) === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: if (isset($this->includePatterns[$code]) === false) { Chris@17: $this->includePatterns[$code] = []; Chris@17: } Chris@17: Chris@17: if (isset($pattern['type']) === false) { Chris@17: $pattern['type'] = 'absolute'; Chris@17: } Chris@17: Chris@17: $this->includePatterns[$code][(string) $pattern] = (string) $pattern['type']; Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo str_repeat("\t", $depth); Chris@17: echo "\t\t=> added rule-specific ".(string) $pattern['type'].' include pattern'; Chris@17: if ($code !== $ref) { Chris@17: echo " for $code"; Chris@17: } Chris@17: Chris@17: echo ': '.(string) $pattern.PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: }//end foreach Chris@17: Chris@17: }//end processRule() Chris@17: Chris@17: Chris@17: /** Chris@17: * Determine if an element should be processed or ignored. Chris@17: * Chris@18: * @param \SimpleXMLElement $element An object from a ruleset XML file. Chris@17: * Chris@17: * @return bool Chris@17: */ Chris@17: private function shouldProcessElement($element) Chris@17: { Chris@17: if (isset($element['phpcbf-only']) === false Chris@17: && isset($element['phpcs-only']) === false Chris@17: ) { Chris@17: // No exceptions are being made. Chris@17: return true; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_CBF === true Chris@17: && isset($element['phpcbf-only']) === true Chris@17: && (string) $element['phpcbf-only'] === 'true' Chris@17: ) { Chris@17: return true; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_CBF === false Chris@17: && isset($element['phpcs-only']) === true Chris@17: && (string) $element['phpcs-only'] === 'true' Chris@17: ) { Chris@17: return true; Chris@17: } Chris@17: Chris@17: return false; Chris@17: Chris@17: }//end shouldProcessElement() Chris@17: Chris@17: Chris@17: /** Chris@17: * Loads and stores sniffs objects used for sniffing files. Chris@17: * Chris@17: * @param array $files Paths to the sniff files to register. Chris@17: * @param array $restrictions The sniff class names to restrict the allowed Chris@17: * listeners to. Chris@17: * @param array $exclusions The sniff class names to exclude from the Chris@17: * listeners list. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: public function registerSniffs($files, $restrictions, $exclusions) Chris@17: { Chris@17: $listeners = []; Chris@17: Chris@17: foreach ($files as $file) { Chris@17: // Work out where the position of /StandardName/Sniffs/... is Chris@17: // so we can determine what the class will be called. Chris@17: $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR); Chris@17: if ($sniffPos === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR); Chris@17: if ($slashPos === false) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $className = Autoload::loadFile($file); Chris@17: $compareName = Util\Common::cleanSniffClass($className); Chris@17: Chris@17: // If they have specified a list of sniffs to restrict to, check Chris@17: // to see if this sniff is allowed. Chris@17: if (empty($restrictions) === false Chris@17: && isset($restrictions[$compareName]) === false Chris@17: ) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: // If they have specified a list of sniffs to exclude, check Chris@17: // to see if this sniff is allowed. Chris@17: if (empty($exclusions) === false Chris@17: && isset($exclusions[$compareName]) === true Chris@17: ) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: // Skip abstract classes. Chris@17: $reflection = new \ReflectionClass($className); Chris@17: if ($reflection->isAbstract() === true) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $listeners[$className] = $className; Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 2) { Chris@17: echo "Registered $className".PHP_EOL; Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: $this->sniffs = $listeners; Chris@17: Chris@17: }//end registerSniffs() Chris@17: Chris@17: Chris@17: /** Chris@17: * Populates the array of PHP_CodeSniffer_Sniff's for this file. Chris@17: * Chris@17: * @return void Chris@18: * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If sniff registration fails. Chris@17: */ Chris@17: public function populateTokenListeners() Chris@17: { Chris@17: // Construct a list of listeners indexed by token being listened for. Chris@17: $this->tokenListeners = []; Chris@17: Chris@17: foreach ($this->sniffs as $sniffClass => $sniffObject) { Chris@17: $this->sniffs[$sniffClass] = null; Chris@17: $this->sniffs[$sniffClass] = new $sniffClass(); Chris@17: Chris@17: $sniffCode = Util\Common::getSniffCode($sniffClass); Chris@17: $this->sniffCodes[$sniffCode] = $sniffClass; Chris@17: Chris@17: // Set custom properties. Chris@17: if (isset($this->ruleset[$sniffCode]['properties']) === true) { Chris@17: foreach ($this->ruleset[$sniffCode]['properties'] as $name => $value) { Chris@17: $this->setSniffProperty($sniffClass, $name, $value); Chris@17: } Chris@17: } Chris@17: Chris@17: $tokenizers = []; Chris@17: $vars = get_class_vars($sniffClass); Chris@17: if (isset($vars['supportedTokenizers']) === true) { Chris@17: foreach ($vars['supportedTokenizers'] as $tokenizer) { Chris@17: $tokenizers[$tokenizer] = $tokenizer; Chris@17: } Chris@17: } else { Chris@17: $tokenizers = ['PHP' => 'PHP']; Chris@17: } Chris@17: Chris@17: $tokens = $this->sniffs[$sniffClass]->register(); Chris@17: if (is_array($tokens) === false) { Chris@17: $msg = "Sniff $sniffClass register() method must return an array"; Chris@17: throw new RuntimeException($msg); Chris@17: } Chris@17: Chris@17: $ignorePatterns = []; Chris@17: $patterns = $this->getIgnorePatterns($sniffCode); Chris@17: foreach ($patterns as $pattern => $type) { Chris@17: $replacements = [ Chris@17: '\\,' => ',', Chris@17: '*' => '.*', Chris@17: ]; Chris@17: Chris@17: $ignorePatterns[] = strtr($pattern, $replacements); Chris@17: } Chris@17: Chris@17: $includePatterns = []; Chris@17: $patterns = $this->getIncludePatterns($sniffCode); Chris@17: foreach ($patterns as $pattern => $type) { Chris@17: $replacements = [ Chris@17: '\\,' => ',', Chris@17: '*' => '.*', Chris@17: ]; Chris@17: Chris@17: $includePatterns[] = strtr($pattern, $replacements); Chris@17: } Chris@17: Chris@17: foreach ($tokens as $token) { Chris@17: if (isset($this->tokenListeners[$token]) === false) { Chris@17: $this->tokenListeners[$token] = []; Chris@17: } Chris@17: Chris@17: if (isset($this->tokenListeners[$token][$sniffClass]) === false) { Chris@17: $this->tokenListeners[$token][$sniffClass] = [ Chris@17: 'class' => $sniffClass, Chris@17: 'source' => $sniffCode, Chris@17: 'tokenizers' => $tokenizers, Chris@17: 'ignore' => $ignorePatterns, Chris@17: 'include' => $includePatterns, Chris@17: ]; Chris@17: } Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: }//end populateTokenListeners() Chris@17: Chris@17: Chris@17: /** Chris@17: * Set a single property for a sniff. Chris@17: * Chris@17: * @param string $sniffClass The class name of the sniff. Chris@17: * @param string $name The name of the property to change. Chris@17: * @param string $value The new value of the property. Chris@17: * Chris@17: * @return void Chris@17: */ Chris@17: public function setSniffProperty($sniffClass, $name, $value) Chris@17: { Chris@17: // Setting a property for a sniff we are not using. Chris@17: if (isset($this->sniffs[$sniffClass]) === false) { Chris@17: return; Chris@17: } Chris@17: Chris@17: $name = trim($name); Chris@17: if (is_string($value) === true) { Chris@17: $value = trim($value); Chris@17: } Chris@17: Chris@17: if ($value === '') { Chris@17: $value = null; Chris@17: } Chris@17: Chris@17: // Special case for booleans. Chris@17: if ($value === 'true') { Chris@17: $value = true; Chris@17: } else if ($value === 'false') { Chris@17: $value = false; Chris@17: } else if (substr($name, -2) === '[]') { Chris@17: $name = substr($name, 0, -2); Chris@17: $values = []; Chris@17: if ($value !== null) { Chris@17: foreach (explode(',', $value) as $val) { Chris@17: list($k, $v) = explode('=>', $val.'=>'); Chris@17: if ($v !== '') { Chris@17: $values[trim($k)] = trim($v); Chris@17: } else { Chris@17: $values[] = trim($k); Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: $value = $values; Chris@17: } Chris@17: Chris@17: $this->sniffs[$sniffClass]->$name = $value; Chris@17: Chris@17: }//end setSniffProperty() Chris@17: Chris@17: Chris@17: /** Chris@17: * Gets the array of ignore patterns. Chris@17: * Chris@17: * Optionally takes a listener to get ignore patterns specified Chris@17: * for that sniff only. Chris@17: * Chris@17: * @param string $listener The listener to get patterns for. If NULL, all Chris@17: * patterns are returned. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: public function getIgnorePatterns($listener=null) Chris@17: { Chris@17: if ($listener === null) { Chris@17: return $this->ignorePatterns; Chris@17: } Chris@17: Chris@17: if (isset($this->ignorePatterns[$listener]) === true) { Chris@17: return $this->ignorePatterns[$listener]; Chris@17: } Chris@17: Chris@17: return []; Chris@17: Chris@17: }//end getIgnorePatterns() Chris@17: Chris@17: Chris@17: /** Chris@17: * Gets the array of include patterns. Chris@17: * Chris@17: * Optionally takes a listener to get include patterns specified Chris@17: * for that sniff only. Chris@17: * Chris@17: * @param string $listener The listener to get patterns for. If NULL, all Chris@17: * patterns are returned. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: public function getIncludePatterns($listener=null) Chris@17: { Chris@17: if ($listener === null) { Chris@17: return $this->includePatterns; Chris@17: } Chris@17: Chris@17: if (isset($this->includePatterns[$listener]) === true) { Chris@17: return $this->includePatterns[$listener]; Chris@17: } Chris@17: Chris@17: return []; Chris@17: Chris@17: }//end getIncludePatterns() Chris@17: Chris@17: Chris@17: }//end class