Skip to content

Commit

Permalink
Merge branch 'master' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jun 29, 2016
2 parents 3669a7f + c287c99 commit 392471f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- PHPCBF always uses this setting to reduce memory
-- Setting $recordErrors in custom reports no longer supported

- Added a new --exclude CLI argument to exclude a list of sniffs from checking and fixing (request #904)
-- Accepts the same sniff codes as the --sniffs command line argument, but provides the opposite functionality
- Generic LineLength sniff no longer errors for comments that cannot be broken out onto a new line (request #766)
-- A typical case is a comment that contains a very long URL
-- The comment is ignored if putting the URL on a indented new comment line would be longer than the allowed length
Expand Down
27 changes: 22 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Config
* string encoding The encoding of the files being checked.
* string[] sniffs The sniffs that should be used for checking.
* If empty, all sniffs in the supplied standards will be used.
* string[] exclude The sniffs that should be excluded from checking.
* If empty, all sniffs in the supplied standards will be used.
* string[] ignored Regular expressions used to ignore files and folders during checking.
* string reportFile A file where the report output should be written.
* string generator The documentation generator to use.
Expand Down Expand Up @@ -106,6 +108,7 @@ class Config
'encoding' => null,
'extensions' => null,
'sniffs' => null,
'exclude' => null,
'ignored' => null,
'reportFile' => null,
'generator' => null,
Expand Down Expand Up @@ -416,6 +419,7 @@ public function restoreDefaults()
'css' => 'CSS',
);
$this->sniffs = array();
$this->exclude = array();
$this->ignored = array();
$this->reportFile = null;
$this->generator = null;
Expand Down Expand Up @@ -699,6 +703,18 @@ public function processLongArgument($arg, $pos)

$this->sniffs = $sniffs;
$this->overriddenDefaults['sniffs'] = true;
} else if (substr($arg, 0, 8) === 'exclude=') {
$sniffs = explode(',', substr($arg, 8));
foreach ($sniffs as $sniff) {
if (substr_count($sniff, '.') !== 2) {
echo 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
$this->printUsage();
exit(2);
}
}

$this->exclude = $sniffs;
$this->overriddenDefaults['exclude'] = true;
} else if (defined('PHP_CODESNIFFER_IN_TESTS') === false
&& substr($arg, 0, 6) === 'cache='
) {
Expand Down Expand Up @@ -1051,8 +1067,9 @@ public function printPHPCSUsage()
echo ' [--report-width=<reportWidth>] [--basepath=<basepath>] [--tab-width=<tabWidth>]'.PHP_EOL;
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
echo ' [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]'.PHP_EOL;
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--encoding=<encoding>] [--parallel=<processes>]'.PHP_EOL;
echo ' [--extensions=<extensions>] [--generator=<generator>] [--ignore=<patterns>] <file> - ...'.PHP_EOL;
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>] '.PHP_EOL;
echo ' [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]'.PHP_EOL;
echo ' [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL;
echo ' - Check STDIN instead of local files and directories'.PHP_EOL;
echo ' -n Do not print warnings (shortcut for --warning-severity=0)'.PHP_EOL;
echo ' -w Print both warnings and errors (this is the default)'.PHP_EOL;
Expand Down Expand Up @@ -1091,7 +1108,7 @@ public function printPHPCSUsage()
echo ' <reportFile> Write the report to the specified file path'.PHP_EOL;
echo ' <reportWidth> How many columns wide screen reports should be printed'.PHP_EOL;
echo ' or set to "auto" to use current screen width, where supported'.PHP_EOL;
echo ' <sniffs> A comma separated list of sniff codes to limit the check to'.PHP_EOL;
echo ' <sniffs> A comma separated list of sniff codes to include or exclude from checking'.PHP_EOL;
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL;
echo ' <severity> The minimum severity required to display an error or warning'.PHP_EOL;
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL;
Expand All @@ -1108,7 +1125,7 @@ public function printPHPCSUsage()
public function printPHPCBFUsage()
{
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL;
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL;
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>] [--suffix=<suffix>]'.PHP_EOL;
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
echo ' [--tab-width=<tabWidth>] [--encoding=<encoding>] [--parallel=<processes>]'.PHP_EOL;
echo ' [--basepath=<basepath>] [--extensions=<extensions>] [--ignore=<patterns>] <file> - ...'.PHP_EOL;
Expand All @@ -1129,7 +1146,7 @@ public function printPHPCBFUsage()
echo ' e.g., module/php,es/js'.PHP_EOL;
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL;
echo ' <processes> How many files should be fixed simultaneously (default is 1)'.PHP_EOL;
echo ' <sniffs> A comma separated list of sniff codes to limit the fixes to'.PHP_EOL;
echo ' <sniffs> A comma separated list of sniff codes to include or exclude from fixing'.PHP_EOL;
echo ' (all sniffs must be part of the specified standard)'.PHP_EOL;
echo ' <severity> The minimum severity required to fix an error or warning'.PHP_EOL;
echo ' <standard> The name or path of the coding standard to use'.PHP_EOL;
Expand Down
7 changes: 5 additions & 2 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public function __construct($path, Ruleset $ruleset, Config $config)

$this->configCache['cache'] = $this->config->cache;
$this->configCache['sniffs'] = $this->config->sniffs;
$this->configCache['exclude'] = $this->config->exclude;
$this->configCache['errorSeverity'] = $this->config->errorSeverity;
$this->configCache['warningSeverity'] = $this->config->warningSeverity;
$this->configCache['recordErrors'] = $this->config->recordErrors;
Expand Down Expand Up @@ -790,8 +791,10 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
// Filter out any messages for sniffs that shouldn't have run
// due to the use of the --sniffs command line argument.
if ($includeAll === false
&& empty($this->configCache['sniffs']) === false
&& in_array($listenerCode, $this->configCache['sniffs']) === false
&& ((empty($this->configCache['sniffs']) === false
&& in_array($listenerCode, $this->configCache['sniffs']) === false)
|| (empty($this->configCache['exclude']) === false
&& in_array($listenerCode, $this->configCache['exclude']) === true))
) {
return false;
}
Expand Down
23 changes: 21 additions & 2 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ public function __construct(Config $config)
{
// Ignore sniff restrictions if caching is on.
$restrictions = array();
$exclusions = array();
if ($config->cache === false) {
$restrictions = $config->sniffs;
$exclusions = $config->exclude;
}

$this->config = $config;
Expand Down Expand Up @@ -175,7 +177,14 @@ public function __construct(Config $config)
$sniffRestrictions[$sniffName] = true;
}

$this->registerSniffs($sniffs, $sniffRestrictions);
$sniffExclusions = array();
foreach ($exclusions as $sniffCode) {
$parts = explode('.', strtolower($sniffCode));
$sniffName = 'php_codesniffer\standards\\'.$parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
$sniffExclusions[$sniffName] = true;
}

$this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions);
$this->populateTokenListeners();

if (PHP_CODESNIFFER_VERBOSITY === 1) {
Expand Down Expand Up @@ -991,10 +1000,12 @@ private function shouldProcessElement($element)
* @param array $files Paths to the sniff files to register.
* @param array $restrictions The sniff class names to restrict the allowed
* listeners to.
* @param array $exclusions The sniff class names to exclude from the
* listeners list.
*
* @return void
*/
public function registerSniffs($files, $restrictions)
public function registerSniffs($files, $restrictions, $exclusions)
{
$listeners = array();

Expand All @@ -1021,6 +1032,14 @@ public function registerSniffs($files, $restrictions)
continue;
}

// If they have specified a list of sniffs to exclude, check
// to see if this sniff is allowed.
if (empty($exclusions) === false
&& isset($exclusions[strtolower($className)]) === true
) {
continue;
}

// Skip abstract classes.
$reflection = new \ReflectionClass($className);
if ($reflection->isAbstract() === true) {
Expand Down

0 comments on commit 392471f

Please sign in to comment.