Skip to content

Commit

Permalink
Merge branch 'master' into issue#1627
Browse files Browse the repository at this point in the history
  • Loading branch information
gmponos committed Aug 27, 2017
2 parents 6546f36 + ed5dbc3 commit f28d344
Show file tree
Hide file tree
Showing 51 changed files with 902 additions and 62 deletions.
27 changes: 27 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,36 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Previously, a file had to be in each of the include patterns to be processed by a sniff
-- Now, a file has to only be in at least one of the patterns
-- This change reflects the original intention of the feature
- PHPCS will now follow symlinks under the list of checked directories
-- This previously only worked if you specified the path to a symlink on the command line
- Output from --config-show, --config-set, and --config-delete now includes the path to the loaded config file
- PHPCS now cleanly exits if its config file is not readable
-- Previously, a combination of PHP notices and PHPCS errors would be generated
- Comment tokens that start with /** are now always tokenized as docblocks
-- Thanks to Michał Bundyra for the patch
- The PHP-supplied T_YIELD and T_YIELD_FROM token have been replicated for older PHP versions
-- Thanks to Michał Bundyra for the patch
- Added new Generic.CodeAnalysis.AssignmentInCondition sniff to warn about variable assignments inside conditions
-- Thanks to Juliette Reinders Folmer for the contribution
- PEAR.Functions.FunctionDeclaration now removes a blank line if it creates one by moving the curly brace during fixing
- Squiz.Commenting.FunctionCommentThrowTag now supports PHP 7.1 multi catch exceptions
- Squiz.Formatting.OperatorBracket no longer throws errors for PHP 7.1 multi catch exceptions
- Squiz.Commenting.LongConditionClosingComment now supports finally statements
- Squiz.Formatting.OperatorBracket now correctly fixes pipe separated flags
- Squiz.WhiteSpace.ControlStructureSpacing now ignores comments when checking blank lines at the top of control structures
- Squiz.WhiteSpace.ObjectOperatorSpacing now detects and fixes spaces around double colons
-- Thanks to Julius Šmatavičius for the patch
- Added a new PHP_CodeSniffer\Util\Tokens::$ooScopeTokens static member var for quickly checking object scope
-- Includes T_CLASS, T_ANON_CLASS, T_INTERFACE, and T_TRAIT
-- Thanks to Juliette Reinders Folmer for the patch
- PHP_CodeSniffer\Files\File::findExtendedClassName() now supports extended interfaces
-- Thanks to Martin Hujer for the patch
- Fixed bug #1550 : Squiz.Commenting.FunctionComment false positive when function contains closure
- Fixed bug #1577 : Generic.InlineControlStructureSniff breaks with a comment between body and condition in do while loops
- Fixed bug #1581 : Sniffs not loaded when one-standard directories are being registered in installed_paths
- Fixed bug #1591 : Autoloader failing to load arbitrary files when installed_paths only set via a custom ruleset
- Fixed bug #1605 : Squiz.WhiteSpace.OperatorSpacing false positive on unary minus after comment
-- Thanks to Juliette Reinders Folmer for the patch
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -170,6 +193,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="InlineControlStructureStandard.xml" role="php" />
</dir>
<dir name="CodeAnalysis">
<file baseinstalldir="PHP/CodeSniffer" name="AssignmentInConditionStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EmptyStatementStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ForLoopShouldBeWhileLoopStandard.xml" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ForLoopWithTestFunctionCallStandard.xml" role="php" />
Expand Down Expand Up @@ -245,6 +269,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSameLineSniff.php" role="php" />
</dir>
<dir name="CodeAnalysis">
<file baseinstalldir="PHP/CodeSniffer" name="AssignmentInConditionSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="EmptyStatementSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ForLoopShouldBeWhileLoopSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ForLoopWithTestFunctionCallSniff.php" role="php" />
Expand Down Expand Up @@ -351,6 +376,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="OpeningBraceSameLineUnitTest.php" role="test" />
</dir>
<dir name="CodeAnalysis">
<file baseinstalldir="PHP/CodeSniffer" name="AssignmentInConditionUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="AssignmentInConditionUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="EmptyStatementUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="EmptyStatementUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ForLoopShouldBeWhileLoopUnitTest.inc" role="test" />
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<file>src</file>
<file>tests</file>

<exclude-pattern>*/Standards/*/Tests/*.(inc|css|js)</exclude-pattern>
<exclude-pattern>*/Standards/*/Tests/*\.(inc|css|js)</exclude-pattern>

<arg name="basepath" value="."/>
<arg name="colors" />
Expand Down
46 changes: 36 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ class Config
*/
private static $configData = null;

/**
* The full path to the config data file that has been loaded.
*
* @var string
*/
private static $configDataFile = null;

/**
* Automatically discovered executable utility paths.
*
Expand Down Expand Up @@ -743,10 +750,12 @@ public function processLongArgument($arg, $pos)
throw new DeepExitException($e->getMessage().PHP_EOL, 3);
}

$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;

if ($current === null) {
$output = "Config value \"$key\" added successfully".PHP_EOL;
$output .= "Config value \"$key\" added successfully".PHP_EOL;
} else {
$output = "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL;
$output .= "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL;
}
throw new DeepExitException($output, 0);
case 'config-delete':
Expand All @@ -756,23 +765,26 @@ public function processLongArgument($arg, $pos)
throw new DeepExitException($error, 3);
}

$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;

$key = $this->cliArgs[($pos + 1)];
$current = self::getConfigData($key);
if ($current === null) {
$output = "Config value \"$key\" has not been set".PHP_EOL;
$output .= "Config value \"$key\" has not been set".PHP_EOL;
} else {
try {
$this->setConfigData($key, null);
} catch (\Exception $e) {
throw new DeepExitException($e->getMessage().PHP_EOL, 3);
}

$output = "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
$output .= "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
}
throw new DeepExitException($output, 0);
case 'config-show':
ob_start();
$data = self::getAllConfigData();
echo 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
$this->printConfigData($data);
$output = ob_get_contents();
ob_end_clean();
Expand Down Expand Up @@ -1358,7 +1370,6 @@ public function printPHPCSUsage()
echo ' <fileList> A file containing a list of files and/or directories to check (one per line)'.PHP_EOL;
echo ' <encoding> The encoding of the files being checked (default is utf-8)'.PHP_EOL;
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL;
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL;
echo ' The type of the file can be specified using: ext/type'.PHP_EOL;
echo ' e.g., module/php,es/js'.PHP_EOL;
echo ' <generator> Uses either the "HTML", "Markdown" or "Text" generator'.PHP_EOL;
Expand Down Expand Up @@ -1418,7 +1429,6 @@ public function printPHPCBFUsage()
echo ' <fileList> A file containing a list of files and/or directories to fix (one per line)'.PHP_EOL;
echo ' <encoding> The encoding of the files being fixed (default is utf-8)'.PHP_EOL;
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL;
echo ' (extension filtering only valid when checking a directory)'.PHP_EOL;
echo ' The type of the file can be specified using: ext/type'.PHP_EOL;
echo ' e.g., module/php,es/js'.PHP_EOL;
echo ' <patterns> A comma separated list of patterns to ignore files and directories'.PHP_EOL;
Expand Down Expand Up @@ -1536,8 +1546,8 @@ public static function setConfigData($key, $value, $temp=false)
if (is_file($configFile) === true
&& is_writable($configFile) === false
) {
$error = 'Config file '.$configFile.' is not writable';
throw new RuntimeException($error);
$error = 'ERROR: Config file '.$configFile.' is not writable'.PHP_EOL.PHP_EOL;
throw new DeepExitException($error, 3);
}
}//end if

Expand All @@ -1561,7 +1571,17 @@ public static function setConfigData($key, $value, $temp=false)
}
}

self::$configData = $phpCodeSnifferConfig;
self::$configDataFile = $configFile;
self::$configData = $phpCodeSnifferConfig;

// If the installed paths are being set, make sure all known
// standards paths are added to the autoloader.
if ($key === 'installed_paths') {
$installedStandards = Util\Standards::getInstalledStandardDetails();
foreach ($installedStandards as $name => $details) {
Autoload::addSearchPath($details['path'], $details['namespace']);
}
}

return true;

Expand Down Expand Up @@ -1601,8 +1621,14 @@ public static function getAllConfigData()
return array();
}

if (is_readable($configFile) === false) {
$error = 'ERROR: Config file '.$configFile.' is not readable'.PHP_EOL.PHP_EOL;
throw new DeepExitException($error, 3);
}

include $configFile;
self::$configData = $phpCodeSnifferConfig;
self::$configDataFile = $configFile;
self::$configData = $phpCodeSnifferConfig;
return self::$configData;

}//end getAllConfigData()
Expand Down
2 changes: 2 additions & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,7 @@ public function getCondition($stackPtr, $type)

/**
* Returns the name of the class that the specified class extends.
* (works for classes, anonymous classes and interfaces)
*
* Returns FALSE on error or if there is no extended class name.
*
Expand All @@ -2136,6 +2137,7 @@ public function findExtendedClassName($stackPtr)

if ($this->tokens[$stackPtr]['code'] !== T_CLASS
&& $this->tokens[$stackPtr]['code'] !== T_ANON_CLASS
&& $this->tokens[$stackPtr]['code'] !== T_INTERFACE
) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files/FileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(Config $config, Ruleset $ruleset)

$filterClass = $this->getFilterClass();

$di = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
$di = new \RecursiveDirectoryIterator($path, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS));
$filter = new $filterClass($di, $path, $config, $ruleset);
$iterator = new \RecursiveIteratorIterator($filter);

Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function accept()
public function getChildren()
{
$children = new static(
new \RecursiveDirectoryIterator($this->current(), \RecursiveDirectoryIterator::SKIP_DOTS),
new \RecursiveDirectoryIterator($this->current(), (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)),
$this->basedir,
$this->config,
$this->ruleset
Expand Down
2 changes: 1 addition & 1 deletion src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function processRuleset($rulesetPath, $depth=0)
echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL;
}

$expandedSniffs = $this->expandRulesetReference($rule['ref'], $rulesetDir, $depth);
$expandedSniffs = $this->expandRulesetReference((string) $rule['ref'], $rulesetDir, $depth);
$newSniffs = array_diff($expandedSniffs, $includedSniffs);
$includedSniffs = array_merge($includedSniffs, $expandedSniffs);

Expand Down
9 changes: 3 additions & 6 deletions src/Sniffs/AbstractVariableSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
namespace PHP_CodeSniffer\Sniffs;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Exceptions\RuntimeException;

abstract class AbstractVariableSniff extends AbstractScopeSniff
{
Expand Down Expand Up @@ -47,12 +49,7 @@ abstract class AbstractVariableSniff extends AbstractScopeSniff
*/
public function __construct()
{
$scopes = array(
T_CLASS,
T_ANON_CLASS,
T_TRAIT,
T_INTERFACE,
);
$scopes = Tokens::$ooScopeTokens;

$listen = array(
T_FUNCTION,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<documentation title="Assignment In Condition">
<standard>
<![CDATA[
Variable assignments should not be made within conditions.
]]>
</standard>
<code_comparison>
<code title="Valid: A variable comparison being executed within a condition.">
<![CDATA[
if (<em>$test === 'abc'</em>) {
// Code.
}
]]>
</code>
<code title="Invalid: A variable assignment being made within a condition.">
<![CDATA[
if (<em>$test = 'abc'</em>) {
// Code.
}
]]>
</code>
</code_comparison>
</documentation>
Loading

0 comments on commit f28d344

Please sign in to comment.