Skip to content

Commit

Permalink
Ruleset/basic validation: improve error messages
Browse files Browse the repository at this point in the history
While the `libxml` error messages are far from perfect, they at least give some clue as to where to look for the error in a ruleset file.

This PR changes two things:
1. Previously, a PHP warning would also be thrown for a ruleset of a standard which is not used in the current run.
    This warning is now silenced.
2. However, for the standards and rulesets actually _used_ by the current run, any potential warnings are made more readable and will now also mention the file in which they were encountered.
    Previously the output would be along the lines of:
    ```
    Warning: simplexml_load_string(): Entity: line 82: parser error : Opening and ending tag mismatch: rule line 80 and rue in /path/to/PHP_CodeSniffer/src/Util/Standards.php on line 119
    Warning: simplexml_load_string():       </rue> in /path/to/PHP_CodeSniffer/src/Util/Standards.php on line 119
    Warning: simplexml_load_string():             ^ in /path/to/PHP_CodeSniffer/src/Util/Standards.php on line 119
    ```
    Now, it will look like:
    ```
    ERROR: Ruleset /path/to/ruleset.xml is not valid
    - On line 82, column 10: Opening and ending tag mismatch: rule line 80 and rue
    ```

Loosely related to squizlabs#2188
  • Loading branch information
jrfnl committed Dec 23, 2018
1 parent 390bffe commit 4b3611d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,21 @@ public function processRuleset($rulesetPath, $depth=0)
echo 'Processing ruleset '.Util\Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL;
}

$ruleset = @simplexml_load_string(file_get_contents($rulesetPath));
libxml_use_internal_errors(true);
$ruleset = simplexml_load_string(file_get_contents($rulesetPath));
if ($ruleset === false) {
throw new RuntimeException("Ruleset $rulesetPath is not valid");
$errorMsg = "Ruleset $rulesetPath is not valid".PHP_EOL;
$errors = libxml_get_errors();
foreach ($errors as $error) {
$errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message;
}

libxml_clear_errors();
throw new RuntimeException($errorMsg);
}

libxml_use_internal_errors(false);

$ownSniffs = [];
$includedSniffs = [];
$excludedSniffs = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Standards.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function getInstalledStandardDetails(
$installedStandards = [];

foreach ($rulesets as $rulesetPath) {
$ruleset = simplexml_load_string(file_get_contents($rulesetPath));
$ruleset = @simplexml_load_string(file_get_contents($rulesetPath));
if ($ruleset === false) {
continue;
}
Expand Down

0 comments on commit 4b3611d

Please sign in to comment.