Skip to content

Commit 88e85eb

Browse files
committed
Merge branch 'feature/2497-map-settings-to-sniff-included-via-path' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 68e213d + e8f2c75 commit 88e85eb

9 files changed

+563
-4
lines changed

package.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ http://pear.php.net/dtd/package-2.0.xsd">
111111
<file baseinstalldir="" name="AcceptTest.php" role="test" />
112112
</dir>
113113
</dir>
114+
<dir name="Ruleset">
115+
<file baseinstalldir="" name="RuleInclusionTest.php" role="test" />
116+
<file baseinstalldir="" name="RuleInclusionTest.xml" role="test" />
117+
<file baseinstalldir="" name="RuleInclusionTest-include.xml" role="test" />
118+
<file baseinstalldir="" name="RuleInclusionAbsoluteLinuxTest.php" role="test" />
119+
<file baseinstalldir="" name="RuleInclusionAbsoluteLinuxTest.xml" role="test" />
120+
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.php" role="test" />
121+
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.xml" role="test" />
122+
</dir>
114123
<dir name="Tokenizer">
115124
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.inc" role="test" />
116125
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.php" role="test" />
@@ -1954,6 +1963,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
19541963
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
19551964
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
19561965
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
1966+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.php" name="tests/Core/Ruleset/RuleInclusionTest.php" />
1967+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.xml" name="tests/Core/Ruleset/RuleInclusionTest.xml" />
1968+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest-include.xml" name="tests/Core/Ruleset/RuleInclusionTest-include.xml" />
1969+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" />
1970+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
1971+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
1972+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
19571973
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
19581974
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
19591975
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />
@@ -1997,6 +2013,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
19972013
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
19982014
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
19992015
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
2016+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.php" name="tests/Core/Ruleset/RuleInclusionTest.php" />
2017+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.xml" name="tests/Core/Ruleset/RuleInclusionTest.xml" />
2018+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest-include.xml" name="tests/Core/Ruleset/RuleInclusionTest-include.xml" />
2019+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" />
2020+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
2021+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
2022+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
20002023
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
20012024
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
20022025
<install as="CodeSniffer/Core/Tokenizer/BackfillFnTokenTest.php" name="tests/Core/Tokenizer/BackfillFnTokenTest.php" />

src/Ruleset.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,17 @@ private function processRule($rule, $newSniffs, $depth=0)
863863
$ref = (string) $rule['ref'];
864864
$todo = [$ref];
865865

866-
$parts = explode('.', $ref);
867-
if (count($parts) <= 2) {
868-
// We are processing a standard or a category of sniffs.
866+
$parts = explode('.', $ref);
867+
$partsCount = count($parts);
868+
if ($partsCount <= 2 || $partsCount > count(array_filter($parts))) {
869+
// We are processing a standard, a category of sniffs or a relative path inclusion.
869870
foreach ($newSniffs as $sniffFile) {
870-
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
871+
$parts = explode(DIRECTORY_SEPARATOR, $sniffFile);
872+
if (count($parts) === 1 && DIRECTORY_SEPARATOR === '\\') {
873+
// Path using forward slashes while running on Windows.
874+
$parts = explode('/', $sniffFile);
875+
}
876+
871877
$sniffName = array_pop($parts);
872878
$sniffCategory = array_pop($parts);
873879
array_pop($parts);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Ruleset class using a Linux-style absolute path to include a sniff.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class RuleInclusionAbsoluteLinuxTest extends TestCase
17+
{
18+
19+
/**
20+
* The Ruleset object.
21+
*
22+
* @var \PHP_CodeSniffer\Ruleset
23+
*/
24+
protected $ruleset;
25+
26+
/**
27+
* Path to the ruleset file.
28+
*
29+
* @var string
30+
*/
31+
private $standard = '';
32+
33+
/**
34+
* The original content of the ruleset.
35+
*
36+
* @var string
37+
*/
38+
private $contents = '';
39+
40+
41+
/**
42+
* Initialize the config and ruleset objects.
43+
*
44+
* @return void
45+
*/
46+
public function setUp()
47+
{
48+
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
49+
$repoRootDir = dirname(dirname(dirname(__DIR__)));
50+
51+
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
52+
$contents = file_get_contents($this->standard);
53+
$this->contents = $contents;
54+
55+
$newPath = $repoRootDir;
56+
if (DIRECTORY_SEPARATOR === '\\') {
57+
$newPath = str_replace('\\', '/', $repoRootDir);
58+
}
59+
60+
$adjusted = str_replace('%path_slash_forward%', $newPath, $contents);
61+
62+
if (file_put_contents($this->standard, $adjusted) === false) {
63+
$this->markTestSkipped('On the fly ruleset adjustment failed');
64+
}
65+
66+
// Initialize the config and ruleset objects for the test.
67+
$config = new Config(["--standard={$this->standard}"]);
68+
$this->ruleset = new Ruleset($config);
69+
70+
}//end setUp()
71+
72+
73+
/**
74+
* Reset ruleset file.
75+
*
76+
* @return void
77+
*/
78+
public function tearDown()
79+
{
80+
file_put_contents($this->standard, $this->contents);
81+
82+
}//end tearDown()
83+
84+
85+
/**
86+
* Test that sniffs registed with a Linux absolute path are correctly recognized and that
87+
* properties are correctly set for them.
88+
*
89+
* @return void
90+
*/
91+
public function testLinuxStylePathRuleInclusion()
92+
{
93+
// Test that the sniff is correctly registered.
94+
$this->assertObjectHasAttribute('sniffCodes', $this->ruleset);
95+
$this->assertCount(1, $this->ruleset->sniffCodes);
96+
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterNot', $this->ruleset->sniffCodes);
97+
$this->assertSame(
98+
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff',
99+
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterNot']
100+
);
101+
102+
// Test that the sniff properties are correctly set.
103+
$this->assertSame(
104+
'10',
105+
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff']->spacing
106+
);
107+
108+
}//end testLinuxStylePathRuleInclusion()
109+
110+
111+
}//end class
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteLinuxTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- %path_slash_forward% will be replaced on the fly -->
5+
<rule ref="%path_slash_forward%/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php">
6+
<properties>
7+
<property name="spacing" value="10" />
8+
</properties>
9+
</rule>
10+
11+
</ruleset>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Ruleset class using a Windows-style absolute path to include a sniff.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class RuleInclusionAbsoluteWindowsTest extends TestCase
17+
{
18+
19+
/**
20+
* The Ruleset object.
21+
*
22+
* @var \PHP_CodeSniffer\Ruleset
23+
*/
24+
protected $ruleset;
25+
26+
/**
27+
* Path to the ruleset file.
28+
*
29+
* @var string
30+
*/
31+
private $standard = '';
32+
33+
/**
34+
* The original content of the ruleset.
35+
*
36+
* @var string
37+
*/
38+
private $contents = '';
39+
40+
41+
/**
42+
* Initialize the config and ruleset objects.
43+
*
44+
* @return void
45+
*/
46+
public function setUp()
47+
{
48+
if (DIRECTORY_SEPARATOR === '/') {
49+
$this->markTestSkipped('Windows specific test');
50+
}
51+
52+
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
53+
$repoRootDir = dirname(dirname(dirname(__DIR__)));
54+
55+
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
56+
$contents = file_get_contents($this->standard);
57+
$this->contents = $contents;
58+
59+
$adjusted = str_replace('%path_slash_back%', $repoRootDir, $contents);
60+
61+
if (file_put_contents($this->standard, $adjusted) === false) {
62+
$this->markTestSkipped('On the fly ruleset adjustment failed');
63+
}
64+
65+
// Initialize the config and ruleset objects for the test.
66+
$config = new Config(["--standard={$this->standard}"]);
67+
$this->ruleset = new Ruleset($config);
68+
69+
}//end setUp()
70+
71+
72+
/**
73+
* Reset ruleset file.
74+
*
75+
* @return void
76+
*/
77+
public function tearDown()
78+
{
79+
if (DIRECTORY_SEPARATOR !== '/') {
80+
file_put_contents($this->standard, $this->contents);
81+
}
82+
83+
}//end tearDown()
84+
85+
86+
/**
87+
* Test that sniffs registed with a Windows absolute path are correctly recognized and that
88+
* properties are correctly set for them.
89+
*
90+
* @return void
91+
*/
92+
public function testWindowsStylePathRuleInclusion()
93+
{
94+
// Test that the sniff is correctly registered.
95+
$this->assertObjectHasAttribute('sniffCodes', $this->ruleset);
96+
$this->assertCount(1, $this->ruleset->sniffCodes);
97+
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterCast', $this->ruleset->sniffCodes);
98+
$this->assertSame(
99+
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff',
100+
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterCast']
101+
);
102+
103+
// Test that the sniff property is correctly set.
104+
$this->assertSame(
105+
'10',
106+
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff']->spacing
107+
);
108+
109+
}//end testWindowsStylePathRuleInclusion()
110+
111+
112+
}//end class
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteWindowsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- %path_slash_back% will be replaced on the fly -->
5+
<rule ref="%path_slash_back%\src\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff.php">
6+
<properties>
7+
<property name="spacing" value="10" />
8+
</properties>
9+
</rule>
10+
11+
</ruleset>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionTest-include" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="Generic.Metrics.NestingLevel">
5+
<properties>
6+
<property name="nestingLevel" value="2" />
7+
</properties>
8+
</rule>
9+
10+
</ruleset>

0 commit comments

Comments
 (0)