From 60d1042b17813d384a6b39a8df3c83fd45a8677f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 8 May 2017 11:27:25 +0200 Subject: [PATCH] Native standard determination: Use a function instead of an array. Change the determination of whether something is a native standard over from a hard coded array to a function checking the standards directory. The set of standards is only retrieved once for efficiency. --- src/Ruleset.php | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Ruleset.php b/src/Ruleset.php index 2c57506b79..9c627d6833 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -115,21 +115,6 @@ class Ruleset */ private $config = null; - /** - * PHPCS native standards. - * - * @var array - */ - private $nativeStandards = array( - 'generic' => true, - 'mysource' => true, - 'pear' => true, - 'psr1' => true, - 'psr2' => true, - 'squiz' => true, - 'zend' => true, - ); - /** * Initialise the ruleset that the run will use. @@ -202,7 +187,7 @@ public function __construct(Config $config) foreach ($restrictions as $sniffCode) { $parts = explode('.', strtolower($sniffCode)); $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; - if (isset($this->nativeStandards[$parts[0]]) === true) { + if ($this->isNativeStandard($parts[0]) === true) { $sniffName = 'php_codesniffer\standards\\'.$sniffName; } @@ -213,7 +198,7 @@ public function __construct(Config $config) foreach ($exclusions as $sniffCode) { $parts = explode('.', strtolower($sniffCode)); $sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff'; - if (isset($this->nativeStandards[$parts[0]]) === true) { + if ($this->isNativeStandard($parts[0]) === true) { $sniffName = 'php_codesniffer\standards\\'.$sniffName; } @@ -1286,4 +1271,27 @@ public function getIncludePatterns($listener=null) }//end getIncludePatterns() + /** + * Verify whether a standard is native to PHP_Codesniffer or a custom standard. + * + * @param string $name The lowercase name of a standard. + * + * @return bool + */ + public function isNativeStandard($name) + { + static $nativeStandards; + + if (isset($nativeStandards) === false) { + $nativeStandards = glob(__DIR__.'/Standards/*' , GLOB_ONLYDIR); + $nativeStandards = array_map('basename', $nativeStandards); + $nativeStandards = array_map('strtolower', $nativeStandards); + $nativeStandards = array_flip($nativeStandards); + } + + return isset($nativeStandards[$name]); + + }//end isNativeStandard() + + }//end class