function Ruleset::processRule
Processes a rule from a ruleset XML file, overriding built-in defaults.
Parameters
\SimpleXMLElement $rule The rule object from a ruleset XML file.:
string[] $newSniffs An array of sniffs that got included by this rule.:
int $depth How many nested processing steps we are in.: This is only used for debug output.
Return value
void
Throws
\PHP_CodeSniffer\Exceptions\RuntimeException If rule settings are invalid.
1 call to Ruleset::processRule()
- Ruleset::processRuleset in vendor/
squizlabs/ php_codesniffer/ src/ Ruleset.php - Processes a single ruleset and returns a list of the sniffs it represents.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Ruleset.php, line 1028
Class
Namespace
PHP_CodeSnifferCode
private function processRule($rule, $newSniffs, $depth = 0) {
$ref = (string) $rule['ref'];
$todo = [
$ref,
];
$parts = explode('.', $ref);
$partsCount = count($parts);
if ($partsCount <= 2 || $partsCount > count(array_filter($parts)) || in_array($ref, $newSniffs) === true) {
// We are processing a standard, a category of sniffs or a relative path inclusion.
foreach ($newSniffs as $sniffFile) {
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
if (count($parts) === 1 && DIRECTORY_SEPARATOR === '\\') {
// Path using forward slashes while running on Windows.
$parts = explode('/', $sniffFile);
}
$sniffName = array_pop($parts);
$sniffCategory = array_pop($parts);
array_pop($parts);
$sniffStandard = array_pop($parts);
$todo[] = $sniffStandard . '.' . $sniffCategory . '.' . substr($sniffName, 0, -9);
}
}
foreach ($todo as $code) {
// Custom severity.
if (isset($rule->severity) === true && $this->shouldProcessElement($rule->severity) === true) {
if (isset($this->ruleset[$code]) === false) {
$this->ruleset[$code] = [];
}
$this->ruleset[$code]['severity'] = (int) $rule->severity;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> severity set to " . (int) $rule->severity;
if ($code !== $ref) {
echo " for {$code}";
}
echo PHP_EOL;
}
}
// Custom message type.
if (isset($rule->type) === true && $this->shouldProcessElement($rule->type) === true) {
if (isset($this->ruleset[$code]) === false) {
$this->ruleset[$code] = [];
}
$type = strtolower((string) $rule->type);
if ($type !== 'error' && $type !== 'warning') {
throw new RuntimeException("Message type \"{$type}\" is invalid; must be \"error\" or \"warning\"");
}
$this->ruleset[$code]['type'] = $type;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> message type set to " . (string) $rule->type;
if ($code !== $ref) {
echo " for {$code}";
}
echo PHP_EOL;
}
}
//end if
// Custom message.
if (isset($rule->message) === true && $this->shouldProcessElement($rule->message) === true) {
if (isset($this->ruleset[$code]) === false) {
$this->ruleset[$code] = [];
}
$this->ruleset[$code]['message'] = (string) $rule->message;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> message set to " . (string) $rule->message;
if ($code !== $ref) {
echo " for {$code}";
}
echo PHP_EOL;
}
}
// Custom properties.
if (isset($rule->properties) === true && $this->shouldProcessElement($rule->properties) === true) {
$propertyScope = 'standard';
if ($code === $ref || substr($ref, -9) === 'Sniff.php') {
$propertyScope = 'sniff';
}
foreach ($rule->properties->property as $prop) {
if ($this->shouldProcessElement($prop) === false) {
continue;
}
if (isset($this->ruleset[$code]) === false) {
$this->ruleset[$code] = [
'properties' => [],
];
}
else {
if (isset($this->ruleset[$code]['properties']) === false) {
$this->ruleset[$code]['properties'] = [];
}
}
$name = (string) $prop['name'];
if (isset($prop['type']) === true && (string) $prop['type'] === 'array') {
$values = [];
if (isset($prop['extend']) === true && (string) $prop['extend'] === 'true' && isset($this->ruleset[$code]['properties'][$name]['value']) === true) {
$values = $this->ruleset[$code]['properties'][$name]['value'];
}
if (isset($prop->element) === true) {
$printValue = '';
foreach ($prop->element as $element) {
if ($this->shouldProcessElement($element) === false) {
continue;
}
$value = (string) $element['value'];
if (isset($element['key']) === true) {
$key = (string) $element['key'];
$values[$key] = $value;
$printValue .= $key . '=>' . $value . ',';
}
else {
$values[] = $value;
$printValue .= $value . ',';
}
}
$printValue = rtrim($printValue, ',');
}
else {
$value = (string) $prop['value'];
$printValue = $value;
foreach (explode(',', $value) as $val) {
list($k, $v) = explode('=>', $val . '=>');
if ($v !== '') {
$values[trim($k)] = trim($v);
}
else {
$values[] = trim($k);
}
}
}
//end if
$this->ruleset[$code]['properties'][$name] = [
'value' => $values,
'scope' => $propertyScope,
];
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> array property \"{$name}\" set to \"{$printValue}\"";
if ($code !== $ref) {
echo " for {$code}";
}
echo PHP_EOL;
}
}
else {
$this->ruleset[$code]['properties'][$name] = [
'value' => (string) $prop['value'],
'scope' => $propertyScope,
];
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> property \"{$name}\" set to \"" . (string) $prop['value'] . '"';
if ($code !== $ref) {
echo " for {$code}";
}
echo PHP_EOL;
}
}
//end if
}
//end foreach
}
//end if
// Ignore patterns.
foreach ($rule->{'exclude-pattern'} as $pattern) {
if ($this->shouldProcessElement($pattern) === false) {
continue;
}
if (isset($this->ignorePatterns[$code]) === false) {
$this->ignorePatterns[$code] = [];
}
if (isset($pattern['type']) === false) {
$pattern['type'] = 'absolute';
}
$this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type'];
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> added rule-specific " . (string) $pattern['type'] . ' ignore pattern';
if ($code !== $ref) {
echo " for {$code}";
}
echo ': ' . (string) $pattern . PHP_EOL;
}
}
//end foreach
// Include patterns.
foreach ($rule->{'include-pattern'} as $pattern) {
if ($this->shouldProcessElement($pattern) === false) {
continue;
}
if (isset($this->includePatterns[$code]) === false) {
$this->includePatterns[$code] = [];
}
if (isset($pattern['type']) === false) {
$pattern['type'] = 'absolute';
}
$this->includePatterns[$code][(string) $pattern] = (string) $pattern['type'];
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
echo "\t\t=> added rule-specific " . (string) $pattern['type'] . ' include pattern';
if ($code !== $ref) {
echo " for {$code}";
}
echo ': ' . (string) $pattern . PHP_EOL;
}
}
//end foreach
}
//end foreach
}