Skip to content

Commit

Permalink
Fixed an issue where including sniffs using paths containing multiple…
Browse files Browse the repository at this point in the history
… dots would silently fail (ref #2847)

This also fixes an issue where the unit tests were failing when run from a an extracted PEAR archive
  • Loading branch information
gsherwood committed Feb 26, 2020
1 parent c36b2b9 commit daa440f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 43 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<notes>
- The T_FN backfill now works more reliably so T_FN tokens only ever represent real arrow functions
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed an issue where including sniffs using paths containing multiple dots would silently fail
- Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures
- Fixed bug #2810 : PHPCBF fails to fix file with empty statement at start on control structure
- Fixed bug #2826 : Generic.WhiteSpace.ArbitraryParenthesesSpacing doesn't detect issues for statements directly after a control structure
Expand Down
5 changes: 4 additions & 1 deletion src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,10 @@ private function processRule($rule, $newSniffs, $depth=0)

$parts = explode('.', $ref);
$partsCount = count($parts);
if ($partsCount <= 2 || $partsCount > count(array_filter($parts))) {
if ($partsCount <= 2
|| $partsCount > count(array_filter($parts))
|| in_array($ref, $newSniffs) === true

This comment has been minimized.

Copy link
@jrfnl

jrfnl Feb 27, 2020

Contributor

I'd suggest that a strict in_array() comparison will be more stable here as this will compare strings and if anything, for whatever reason in the $newSniffs array would not be a string, non-strict will be unreliable.

in_array($ref, $newSniffs, true) === true
) {
// We are processing a standard, a category of sniffs or a relative path inclusion.
foreach ($newSniffs as $sniffFile) {
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
Expand Down
48 changes: 47 additions & 1 deletion tests/Core/Ruleset/RuleInclusionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class RuleInclusionTest extends TestCase
*/
protected static $ruleset;

/**
* Path to the ruleset file.
*
* @var string
*/
private static $standard = '';

/**
* The original content of the ruleset.
*
* @var string
*/
private static $contents = '';


/**
* Initialize the test.
Expand Down Expand Up @@ -53,13 +67,45 @@ public static function setUpBeforeClass()
return;
}

$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
self::$standard = $standard;

// On-the-fly adjust the ruleset test file to be able to test
// sniffs included with relative paths.
$contents = file_get_contents($standard);
self::$contents = $contents;

$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));

$newPath = $repoRootDir;
if (DIRECTORY_SEPARATOR === '\\') {
$newPath = str_replace('\\', '/', $repoRootDir);
}

$adjusted = str_replace('%path_root_dir%', $newPath, $contents);

if (file_put_contents($standard, $adjusted) === false) {
self::markTestSkipped('On the fly ruleset adjustment failed');
}

$config = new Config(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);

}//end setUpBeforeClass()


/**
* Reset ruleset file.
*
* @return void
*/
public function tearDown()
{
file_put_contents(self::$standard, self::$contents);

}//end tearDown()


/**
* Test that sniffs are registered.
*
Expand Down
82 changes: 41 additions & 41 deletions tests/Core/Ruleset/RuleInclusionTest.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">

<rule ref="PSR1">
<properties>
<property name="setforallsniffs" value="true" />
</properties>
</rule>

<rule ref="Zend.NamingConventions">
<properties>
<property name="setforallincategory" value="true" />
</properties>
</rule>

<rule ref="Generic.Arrays.ArrayIndent">
<properties>
<property name="indent" value="2" />
</properties>
</rule>

<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
<properties>
<property name="complexity" value="50" />
</properties>
</rule>

<rule ref="./src/Standards/Generic/Sniffs/Files/LineLengthSniff.php">
<properties>
<property name="lineLimit" value="10" />
</properties>
</rule>

<rule ref="./../PHP_CodeSniffer/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php">
<properties>
<property name="strict" value="false" />
</properties>
</rule>

<rule ref="./RuleInclusionTest-include.xml">
<properties>
<property name="setforsniffsinincludedruleset" value="true" />
</properties>
</rule>
<rule ref="PSR1">
<properties>
<property name="setforallsniffs" value="true" />
</properties>
</rule>

<rule ref="Zend.NamingConventions">
<properties>
<property name="setforallincategory" value="true" />
</properties>
</rule>

<rule ref="Generic.Arrays.ArrayIndent">
<properties>
<property name="indent" value="2" />
</properties>
</rule>

<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
<properties>
<property name="complexity" value="50" />
</properties>
</rule>

<rule ref="./src/Standards/Generic/Sniffs/Files/LineLengthSniff.php">
<properties>
<property name="lineLimit" value="10" />
</properties>
</rule>

<rule ref="./../%path_root_dir%/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php">
<properties>
<property name="strict" value="false" />
</properties>
</rule>

<rule ref="./RuleInclusionTest-include.xml">
<properties>
<property name="setforsniffsinincludedruleset" value="true" />
</properties>
</rule>

</ruleset>

0 comments on commit daa440f

Please sign in to comment.