Skip to content

Commit 1a59851

Browse files
committed
Rulesets can now specify custom autoloaders using the new autoload tag (ref #1469)
1 parent ca446ae commit 1a59851

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
5252
-- If ommitted, the namespace is assumed to be the same as the directory name containing the ruleset.xml file
5353
-- The namespace is set in the ruleset tag of the ruleset.xml file
5454
-- e.g., ruleset name="My Coding Standard" namespace="MyProject\CS\Standard"
55+
- Rulesets can now specify custom autoloaders using the new autoload tag
56+
-- Autloaders are included while the ruleset is being processed and before any custom sniffs are included
57+
-- Allows for very custom autoloading of helper classes well before the boostrap files are included
5558
- Fixed a problem where excluding a message from a custom standard's own sniff would exclude the whole sniff
5659
-- This caused some PSR2 errors to be under-reported
5760
- Fixed bug #1442 : T_NULLABLE detection not working for nullable parameters and return type hints in some cases

src/Ruleset.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,30 @@ public function processRuleset($rulesetPath, $depth=0)
326326
$ownSniffs = $this->expandSniffDirectory($sniffDir, $depth);
327327
}
328328

329+
// Included custom autoloaders.
330+
foreach ($ruleset->{'autoload'} as $autoload) {
331+
if ($this->shouldProcessElement($autoload) === false) {
332+
continue;
333+
}
334+
335+
$autoloadPath = (string) $autoload;
336+
if (is_file($autoloadPath) === false) {
337+
$autoloadPath = Util\Common::realPath(dirname($rulesetPath).DIRECTORY_SEPARATOR.$autoloadPath);
338+
}
339+
340+
if ($autoloadPath === false) {
341+
echo 'ERROR: The specified autoload file "'.$autoload.'" does not exist'.PHP_EOL.PHP_EOL;
342+
exit(3);
343+
}
344+
345+
include $autoloadPath;
346+
347+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
348+
echo str_repeat("\t", $depth);
349+
echo "\t=> included autoloader $autoloadPath".PHP_EOL;
350+
}
351+
}//end foreach
352+
329353
// Process custom sniff config settings.
330354
foreach ($ruleset->{'config'} as $config) {
331355
if ($this->shouldProcessElement($config) === false) {

0 commit comments

Comments
 (0)