comparison vendor/squizlabs/php_codesniffer/src/Ruleset.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
65 * An array of sniff objects that are being used to check files. 65 * An array of sniff objects that are being used to check files.
66 * 66 *
67 * The key is the fully qualified name of the sniff class 67 * The key is the fully qualified name of the sniff class
68 * and the value is the sniff object. 68 * and the value is the sniff object.
69 * 69 *
70 * @var array<string, \PHP_CodeSniffer\Sniff> 70 * @var array<string, \PHP_CodeSniffer\Sniffs\Sniff>
71 */ 71 */
72 public $sniffs = []; 72 public $sniffs = [];
73 73
74 /** 74 /**
75 * A mapping of sniff codes to fully qualified class names. 75 * A mapping of sniff codes to fully qualified class names.
85 * An array of token types and the sniffs that are listening for them. 85 * An array of token types and the sniffs that are listening for them.
86 * 86 *
87 * The key is the token name being listened for and the value 87 * The key is the token name being listened for and the value
88 * is the sniff object. 88 * is the sniff object.
89 * 89 *
90 * @var array<int, \PHP_CodeSniffer\Sniff> 90 * @var array<int, \PHP_CodeSniffer\Sniffs\Sniff>
91 */ 91 */
92 public $tokenListeners = []; 92 public $tokenListeners = [];
93 93
94 /** 94 /**
95 * An array of rules from the ruleset.xml file. 95 * An array of rules from the ruleset.xml file.
159 $standardName = (string) $ruleset['name']; 159 $standardName = (string) $ruleset['name'];
160 if ($this->name !== '') { 160 if ($this->name !== '') {
161 $this->name .= ', '; 161 $this->name .= ', ';
162 } 162 }
163 163
164 $this->name .= $standardName; 164 $this->name .= $standardName;
165 $this->paths[] = $standard;
166 165
167 // Allow autoloading of custom files inside this standard. 166 // Allow autoloading of custom files inside this standard.
168 if (isset($ruleset['namespace']) === true) { 167 if (isset($ruleset['namespace']) === true) {
169 $namespace = (string) $ruleset['namespace']; 168 $namespace = (string) $ruleset['namespace'];
170 } else { 169 } else {
303 * @param string $rulesetPath The path to a ruleset XML file. 302 * @param string $rulesetPath The path to a ruleset XML file.
304 * @param int $depth How many nested processing steps we are in. This 303 * @param int $depth How many nested processing steps we are in. This
305 * is only used for debug output. 304 * is only used for debug output.
306 * 305 *
307 * @return string[] 306 * @return string[]
308 * @throws RuntimeException If the ruleset path is invalid. 307 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the ruleset path is invalid.
309 */ 308 */
310 public function processRuleset($rulesetPath, $depth=0) 309 public function processRuleset($rulesetPath, $depth=0)
311 { 310 {
312 $rulesetPath = Util\Common::realpath($rulesetPath); 311 $rulesetPath = Util\Common::realpath($rulesetPath);
313 if (PHP_CODESNIFFER_VERBOSITY > 1) { 312 if (PHP_CODESNIFFER_VERBOSITY > 1) {
314 echo str_repeat("\t", $depth); 313 echo str_repeat("\t", $depth);
315 echo 'Processing ruleset '.Util\Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL; 314 echo 'Processing ruleset '.Util\Common::stripBasepath($rulesetPath, $this->config->basepath).PHP_EOL;
316 } 315 }
317 316
318 $ruleset = @simplexml_load_string(file_get_contents($rulesetPath)); 317 libxml_use_internal_errors(true);
318 $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
319 if ($ruleset === false) { 319 if ($ruleset === false) {
320 throw new RuntimeException("Ruleset $rulesetPath is not valid"); 320 $errorMsg = "Ruleset $rulesetPath is not valid".PHP_EOL;
321 } 321 $errors = libxml_get_errors();
322 foreach ($errors as $error) {
323 $errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message;
324 }
325
326 libxml_clear_errors();
327 throw new RuntimeException($errorMsg);
328 }
329
330 libxml_use_internal_errors(false);
322 331
323 $ownSniffs = []; 332 $ownSniffs = [];
324 $includedSniffs = []; 333 $includedSniffs = [];
325 $excludedSniffs = []; 334 $excludedSniffs = [];
326 335
336 $this->paths[] = $rulesetPath;
327 $rulesetDir = dirname($rulesetPath); 337 $rulesetDir = dirname($rulesetPath);
328 $this->rulesetDirs[] = $rulesetDir; 338 $this->rulesetDirs[] = $rulesetDir;
329 339
330 $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs'; 340 $sniffDir = $rulesetDir.DIRECTORY_SEPARATOR.'Sniffs';
331 if (is_dir($sniffDir) === true) { 341 if (is_dir($sniffDir) === true) {
408 echo str_repeat("\t", $depth); 418 echo str_repeat("\t", $depth);
409 echo "\t\t=> severity set to 5".PHP_EOL; 419 echo "\t\t=> severity set to 5".PHP_EOL;
410 } 420 }
411 } else if (empty($newSniffs) === false) { 421 } else if (empty($newSniffs) === false) {
412 $newSniff = $newSniffs[0]; 422 $newSniff = $newSniffs[0];
413 if (in_array($newSniff, $ownSniffs) === false) { 423 if (in_array($newSniff, $ownSniffs, true) === false) {
414 // Including a sniff that hasn't been included higher up, but 424 // Including a sniff that hasn't been included higher up, but
415 // only including a single message from it. So turn off all messages in 425 // only including a single message from it. So turn off all messages in
416 // the sniff, except this one. 426 // the sniff, except this one.
417 $this->ruleset[$sniffCode]['severity'] = 0; 427 $this->ruleset[$sniffCode]['severity'] = 0;
418 $this->ruleset[(string) $rule['ref']]['severity'] = 5; 428 $this->ruleset[(string) $rule['ref']]['severity'] = 5;
455 echo "\t\t=> severity set to 0".PHP_EOL; 465 echo "\t\t=> severity set to 0".PHP_EOL;
456 } 466 }
457 } else { 467 } else {
458 $excludedSniffs = array_merge( 468 $excludedSniffs = array_merge(
459 $excludedSniffs, 469 $excludedSniffs,
460 $this->expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1)) 470 $this->expandRulesetReference((string) $exclude['name'], $rulesetDir, ($depth + 1))
461 ); 471 );
462 } 472 }
463 }//end foreach 473 }//end foreach
464 }//end if 474 }//end if
465 475
576 586
577 // Merge our own sniff list with our externally included 587 // Merge our own sniff list with our externally included
578 // sniff list, but filter out any excluded sniffs. 588 // sniff list, but filter out any excluded sniffs.
579 $files = []; 589 $files = [];
580 foreach ($includedSniffs as $sniff) { 590 foreach ($includedSniffs as $sniff) {
581 if (in_array($sniff, $excludedSniffs) === true) { 591 if (in_array($sniff, $excludedSniffs, true) === true) {
582 continue; 592 continue;
583 } else { 593 } else {
584 $files[] = Util\Common::realpath($sniff); 594 $files[] = Util\Common::realpath($sniff);
585 } 595 }
586 } 596 }
658 * evaluate relative paths. 668 * evaluate relative paths.
659 * @param int $depth How many nested processing steps we are in. This 669 * @param int $depth How many nested processing steps we are in. This
660 * is only used for debug output. 670 * is only used for debug output.
661 * 671 *
662 * @return array 672 * @return array
663 * @throws RuntimeException If the reference is invalid. 673 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the reference is invalid.
664 */ 674 */
665 private function expandRulesetReference($ref, $rulesetDir, $depth=0) 675 private function expandRulesetReference($ref, $rulesetDir, $depth=0)
666 { 676 {
667 // Ignore internal sniffs codes as they are used to only 677 // Ignore internal sniffs codes as they are used to only
668 // hide and change internal messages. 678 // hide and change internal messages.
713 // See if this is a whole standard being referenced. 723 // See if this is a whole standard being referenced.
714 $path = Util\Standards::getInstalledStandardPath($ref); 724 $path = Util\Standards::getInstalledStandardPath($ref);
715 if (Util\Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { 725 if (Util\Common::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) {
716 // If the ruleset exists inside the phar file, use it. 726 // If the ruleset exists inside the phar file, use it.
717 if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { 727 if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) {
718 $path = $path.DIRECTORY_SEPARATOR.'ruleset.xml'; 728 $path .= DIRECTORY_SEPARATOR.'ruleset.xml';
719 } else { 729 } else {
720 $path = null; 730 $path = null;
721 } 731 }
722 } 732 }
723 733
834 844
835 845
836 /** 846 /**
837 * Processes a rule from a ruleset XML file, overriding built-in defaults. 847 * Processes a rule from a ruleset XML file, overriding built-in defaults.
838 * 848 *
839 * @param SimpleXMLElement $rule The rule object from a ruleset XML file. 849 * @param \SimpleXMLElement $rule The rule object from a ruleset XML file.
840 * @param string[] $newSniffs An array of sniffs that got included by this rule. 850 * @param string[] $newSniffs An array of sniffs that got included by this rule.
841 * @param int $depth How many nested processing steps we are in. 851 * @param int $depth How many nested processing steps we are in.
842 * This is only used for debug output. 852 * This is only used for debug output.
843 * 853 *
844 * @return void 854 * @return void
845 * @throws RuntimeException If rule settings are invalid. 855 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If rule settings are invalid.
846 */ 856 */
847 private function processRule($rule, $newSniffs, $depth=0) 857 private function processRule($rule, $newSniffs, $depth=0)
848 { 858 {
849 $ref = (string) $rule['ref']; 859 $ref = (string) $rule['ref'];
850 $todo = [$ref]; 860 $todo = [$ref];
1071 1081
1072 1082
1073 /** 1083 /**
1074 * Determine if an element should be processed or ignored. 1084 * Determine if an element should be processed or ignored.
1075 * 1085 *
1076 * @param SimpleXMLElement $element An object from a ruleset XML file. 1086 * @param \SimpleXMLElement $element An object from a ruleset XML file.
1077 * 1087 *
1078 * @return bool 1088 * @return bool
1079 */ 1089 */
1080 private function shouldProcessElement($element) 1090 private function shouldProcessElement($element)
1081 { 1091 {
1172 1182
1173 /** 1183 /**
1174 * Populates the array of PHP_CodeSniffer_Sniff's for this file. 1184 * Populates the array of PHP_CodeSniffer_Sniff's for this file.
1175 * 1185 *
1176 * @return void 1186 * @return void
1177 * @throws RuntimeException If sniff registration fails. 1187 * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If sniff registration fails.
1178 */ 1188 */
1179 public function populateTokenListeners() 1189 public function populateTokenListeners()
1180 { 1190 {
1181 // Construct a list of listeners indexed by token being listened for. 1191 // Construct a list of listeners indexed by token being listened for.
1182 $this->tokenListeners = []; 1192 $this->tokenListeners = [];