Skip to content

Commit 28ddd64

Browse files
committed
Added support for default ruleset + file paths in ruleset.xml files
1 parent ebf9941 commit 28ddd64

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

CodeSniffer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ public function processRuleset($rulesetPath, $depth=0)
702702
$ownSniffs = array();
703703
$includedSniffs = array();
704704
$excludedSniffs = array();
705+
$cliValues = $this->cli->getCommandLineValues();
705706

706707
$rulesetDir = dirname($rulesetPath);
707708
self::$rulesetDirs[] = $rulesetDir;
@@ -772,7 +773,10 @@ public function processRuleset($rulesetPath, $depth=0)
772773
}
773774

774775
if (isset($arg['name']) === true) {
775-
$argString = '--'.(string) $arg['name'].'='.(string) $arg['value'];
776+
$argString = '--'.(string) $arg['name'];
777+
if (isset($arg['value']) === true) {
778+
$argString .= '='.(string) $arg['value'];
779+
}
776780
} else {
777781
$argString = '-'.(string) $arg['value'];
778782
}
@@ -783,6 +787,18 @@ public function processRuleset($rulesetPath, $depth=0)
783787
echo str_repeat("\t", $depth);
784788
echo "\t=> set command line value $argString".PHP_EOL;
785789
}
790+
}//end foreach
791+
792+
if (empty($cliValues['files']) === true) {
793+
// Process hard-coded file paths.
794+
foreach ($ruleset->{'file'} as $file) {
795+
$file = (string) $file;
796+
$cliArgs[] = $file;
797+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
798+
echo str_repeat("\t", $depth);
799+
echo "\t=> added \"$file\" to the file list".PHP_EOL;
800+
}
801+
}
786802
}
787803

788804
if (empty($cliArgs) === false) {

CodeSniffer/CLI.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -825,21 +825,6 @@ public function process($values=array())
825825
exit(0);
826826
}
827827

828-
$fileContents = '';
829-
if (empty($values['files']) === true) {
830-
// Check if they are passing in the file contents.
831-
$handle = fopen('php://stdin', 'r');
832-
$fileContents = stream_get_contents($handle);
833-
fclose($handle);
834-
835-
if ($fileContents === '') {
836-
// No files and no content passed in.
837-
echo 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL;
838-
$this->printUsage();
839-
exit(2);
840-
}
841-
}
842-
843828
$phpcs = new PHP_CodeSniffer($values['verbosity'], null, null, null);
844829
$phpcs->setCli($this);
845830
$phpcs->initStandard($values['standard'], $values['sniffs']);
@@ -880,8 +865,22 @@ public function process($values=array())
880865

881866
$phpcs->processFiles($values['files'], $values['local']);
882867

883-
if ($fileContents !== '') {
884-
$phpcs->processFile('STDIN', $fileContents);
868+
if (empty($values['files']) === true) {
869+
// Check if they are passing in the file contents.
870+
$handle = fopen('php://stdin', 'r');
871+
$fileContents = stream_get_contents($handle);
872+
fclose($handle);
873+
874+
if ($fileContents === '') {
875+
// No files and no content passed in.
876+
echo 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL;
877+
$this->printUsage();
878+
exit(2);
879+
} else {
880+
if ($fileContents !== '') {
881+
$phpcs->processFile('STDIN', $fileContents);
882+
}
883+
}
885884
}
886885

887886
// Interactive runs don't require a final report and it doesn't really
@@ -997,6 +996,14 @@ public function validateStandard($standards)
997996
{
998997
if ($standards === null) {
999998
// They did not supply a standard to use.
999+
// Looks for a ruleset in the current directory.
1000+
if (empty($this->values['files']) === true) {
1001+
$default = getcwd().DIRECTORY_SEPARATOR.'ruleset.xml';
1002+
if (is_file($default) === true) {
1003+
return array($default);
1004+
}
1005+
}
1006+
10001007
// Try to get the default from the config system.
10011008
$standard = PHP_CodeSniffer::getConfigData('default_standard');
10021009
if ($standard === null) {

package.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
3434
-- Use the --report-width=auto command line argument to auto-size the reports
3535
-- Use the command "phpcs --config-set report_width auto" to use auto-sizing by default
3636
- Reports will now size to fit inside the report width setting instead of always using padding to fill the space
37+
- If no files or standards are specified, PHPCS will now look for a ruleset.xml file in the current directory
38+
-- This file has the same format as a standard ruleset.xml file
39+
-- The ruleset.xml file should specify (at least) files to process and a standard/sniffs to use
40+
-- Useful for running the phpcs and phpcbf commands without any arguments at the top of a repository
41+
- Default file paths can now be specified in a ruleset.xml file using the "file" tag
42+
-- File paths are only processed if no files were specified on the command line
3743
- Interactive mode no longer breaks if you also specify a report type on the command line
3844
- PEAR InlineCommentSniff now fixes the Perl-style comments that it finds (request #375)
3945
- PSR2 standard no longer fixes the placement of docblock open tags as comments are excluded from this standard

0 commit comments

Comments
 (0)