Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add support to import namespaces for sniffs #25

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions CodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ class PHP_CodeSniffer
*/
protected $ignorePatterns = array();

/**
* An array of imported namespaces
*
* @var array
*/
protected $importedNamespaces = array();

/**
* An array of extensions for files we will check.
*
Expand Down Expand Up @@ -736,6 +743,9 @@ public function getSniffFiles($dir, $standard=null)
throw new PHP_CodeSniffer_Exception("Ruleset $rulesetPath is not valid");
}

// Load imported namespaces for relative standards
$this->loadImportedNamespaces($dir, $ruleset);

foreach ($ruleset->rule as $rule) {
$includedSniffs = array_merge($includedSniffs, $this->_expandRulesetReference($rule['ref']));

Expand Down Expand Up @@ -766,6 +776,36 @@ public function getSniffFiles($dir, $standard=null)
}//end getSniffFiles()


/**
* Load namespaces for sniffs which are not located as installed sniff.
*
* @param string $dir The directory where to look for the files.
* @param SimpleXMLElement $ruleset The ruleset xml of standard
*
* @return void
*/
protected function loadImportedNamespaces($dir, SimpleXMLElement $ruleset) {
// If there are no namespace imports, exit right here
if(property_exists($ruleset, 'namespace-import') !== true) {
return;
}

// Import the namespaces
foreach ($ruleset->{'namespace-import'} as $ruleImport) {
$importedRulePath = $ruleImport['ref'];

// If an imported rule starts with / already is fine, because there is a absolute path
// If it starts with "../" the imported rule is relative to the ruleset.xml
if(substr($importedRulePath, 0, 3) === '../') {
$importedRulePath = realpath($dir . '/' . $importedRulePath);
}

if(is_dir($importedRulePath) === true) {
$this->importedNamespaces[(string) $ruleImport['name']] = $importedRulePath;
}
}
}//end loadImportedNamespaces()

/**
* Expand a ruleset sniff reference into a list of sniff files.
*
Expand Down Expand Up @@ -813,6 +853,11 @@ private function _expandRulesetReference($sniff)
// installed in there.
$path = realpath(self::$standardDir.'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php');
}

// If there was no standard found, have a look at the namespace import
if ($path === false && isset($this->importedNamespaces[$parts[0]])) {
$path = realpath($this->importedNamespaces[$parts[0]].'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php');
}
}
}//end if

Expand Down