Skip to content

Commit daa440f

Browse files
committed
Fixed an issue where including sniffs using paths containing multiple 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
1 parent c36b2b9 commit daa440f

File tree

4 files changed

+93
-43
lines changed

4 files changed

+93
-43
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2828
<notes>
2929
- The T_FN backfill now works more reliably so T_FN tokens only ever represent real arrow functions
3030
-- Thanks to Juliette Reinders Folmer for the patch
31+
- Fixed an issue where including sniffs using paths containing multiple dots would silently fail
3132
- Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures
3233
- Fixed bug #2810 : PHPCBF fails to fix file with empty statement at start on control structure
3334
- Fixed bug #2826 : Generic.WhiteSpace.ArbitraryParenthesesSpacing doesn't detect issues for statements directly after a control structure

src/Ruleset.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,10 @@ private function processRule($rule, $newSniffs, $depth=0)
865865

866866
$parts = explode('.', $ref);
867867
$partsCount = count($parts);
868-
if ($partsCount <= 2 || $partsCount > count(array_filter($parts))) {
868+
if ($partsCount <= 2
869+
|| $partsCount > count(array_filter($parts))
870+
|| in_array($ref, $newSniffs) === true
871+
) {
869872
// We are processing a standard, a category of sniffs or a relative path inclusion.
870873
foreach ($newSniffs as $sniffFile) {
871874
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile);

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ class RuleInclusionTest extends TestCase
2323
*/
2424
protected static $ruleset;
2525

26+
/**
27+
* Path to the ruleset file.
28+
*
29+
* @var string
30+
*/
31+
private static $standard = '';
32+
33+
/**
34+
* The original content of the ruleset.
35+
*
36+
* @var string
37+
*/
38+
private static $contents = '';
39+
2640

2741
/**
2842
* Initialize the test.
@@ -53,13 +67,45 @@ public static function setUpBeforeClass()
5367
return;
5468
}
5569

56-
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
70+
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
71+
self::$standard = $standard;
72+
73+
// On-the-fly adjust the ruleset test file to be able to test
74+
// sniffs included with relative paths.
75+
$contents = file_get_contents($standard);
76+
self::$contents = $contents;
77+
78+
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
79+
80+
$newPath = $repoRootDir;
81+
if (DIRECTORY_SEPARATOR === '\\') {
82+
$newPath = str_replace('\\', '/', $repoRootDir);
83+
}
84+
85+
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
86+
87+
if (file_put_contents($standard, $adjusted) === false) {
88+
self::markTestSkipped('On the fly ruleset adjustment failed');
89+
}
90+
5791
$config = new Config(["--standard=$standard"]);
5892
self::$ruleset = new Ruleset($config);
5993

6094
}//end setUpBeforeClass()
6195

6296

97+
/**
98+
* Reset ruleset file.
99+
*
100+
* @return void
101+
*/
102+
public function tearDown()
103+
{
104+
file_put_contents(self::$standard, self::$contents);
105+
106+
}//end tearDown()
107+
108+
63109
/**
64110
* Test that sniffs are registered.
65111
*
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
<?xml version="1.0"?>
22
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
33

4-
<rule ref="PSR1">
5-
<properties>
6-
<property name="setforallsniffs" value="true" />
7-
</properties>
8-
</rule>
9-
10-
<rule ref="Zend.NamingConventions">
11-
<properties>
12-
<property name="setforallincategory" value="true" />
13-
</properties>
14-
</rule>
15-
16-
<rule ref="Generic.Arrays.ArrayIndent">
17-
<properties>
18-
<property name="indent" value="2" />
19-
</properties>
20-
</rule>
21-
22-
<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
23-
<properties>
24-
<property name="complexity" value="50" />
25-
</properties>
26-
</rule>
27-
28-
<rule ref="./src/Standards/Generic/Sniffs/Files/LineLengthSniff.php">
29-
<properties>
30-
<property name="lineLimit" value="10" />
31-
</properties>
32-
</rule>
33-
34-
<rule ref="./../PHP_CodeSniffer/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php">
35-
<properties>
36-
<property name="strict" value="false" />
37-
</properties>
38-
</rule>
39-
40-
<rule ref="./RuleInclusionTest-include.xml">
41-
<properties>
42-
<property name="setforsniffsinincludedruleset" value="true" />
43-
</properties>
44-
</rule>
4+
<rule ref="PSR1">
5+
<properties>
6+
<property name="setforallsniffs" value="true" />
7+
</properties>
8+
</rule>
9+
10+
<rule ref="Zend.NamingConventions">
11+
<properties>
12+
<property name="setforallincategory" value="true" />
13+
</properties>
14+
</rule>
15+
16+
<rule ref="Generic.Arrays.ArrayIndent">
17+
<properties>
18+
<property name="indent" value="2" />
19+
</properties>
20+
</rule>
21+
22+
<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
23+
<properties>
24+
<property name="complexity" value="50" />
25+
</properties>
26+
</rule>
27+
28+
<rule ref="./src/Standards/Generic/Sniffs/Files/LineLengthSniff.php">
29+
<properties>
30+
<property name="lineLimit" value="10" />
31+
</properties>
32+
</rule>
33+
34+
<rule ref="./../%path_root_dir%/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php">
35+
<properties>
36+
<property name="strict" value="false" />
37+
</properties>
38+
</rule>
39+
40+
<rule ref="./RuleInclusionTest-include.xml">
41+
<properties>
42+
<property name="setforsniffsinincludedruleset" value="true" />
43+
</properties>
44+
</rule>
4545

4646
</ruleset>

0 commit comments

Comments
 (0)