Skip to content

Commit

Permalink
Native standard determination: Use a function instead of an array.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed May 8, 2017
1 parent d187210 commit 60d1042
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/Ruleset.php
Expand Up @@ -115,21 +115,6 @@ class Ruleset
*/
private $config = null;

/**
* PHPCS native standards.
*
* @var array<string, bool>
*/
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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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

0 comments on commit 60d1042

Please sign in to comment.