Skip to content

Commit cef31ad

Browse files
committed
Ruleset::expandRulesetReference(): test handling "Internal" references
1 parent 3eca7f7 commit cef31ad

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceInternalTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/Internal/"/>
5+
6+
<rule ref="Internal.NoCodeFound">
7+
<severity>0</severity>
8+
</rule>
9+
10+
<!-- While this references a valid sniff category, it will be ignored anyway as the ref starts with "Internal". -->
11+
<rule ref="Internal.Valid"/>
12+
13+
<!-- Prevent a "no sniff were registered" error. -->
14+
<rule ref="Generic.PHP.BacktickOperator"/>
15+
</ruleset>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceInternalTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/Internal/"/>
5+
6+
<rule ref="Internal"/>
7+
8+
</ruleset>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::expandRulesetReference() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test handling of "internal" references in the Ruleset::expandRulesetReference() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference
20+
*/
21+
final class ExpandRulesetReferenceInternalTest extends AbstractRulesetTestCase
22+
{
23+
24+
25+
/**
26+
* Verify that a ruleset reference starting with "Internal." (including the dot) doesn't cause any sniffs to be registered.
27+
*
28+
* @return void
29+
*/
30+
public function testInternalRefDoesNotGetExpanded()
31+
{
32+
// Set up the ruleset.
33+
$standard = __DIR__.'/ExpandRulesetReferenceInternalIgnoreTest.xml';
34+
$config = new ConfigDouble(["--standard=$standard"]);
35+
$ruleset = new Ruleset($config);
36+
37+
$expected = ['Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff'];
38+
39+
$this->assertSame($expected, $ruleset->sniffCodes);
40+
41+
}//end testInternalRefDoesNotGetExpanded()
42+
43+
44+
/**
45+
* While definitely not recommended, including a standard named "Internal", _does_ allow for sniffs to be registered.
46+
*
47+
* Note: customizations (exclusions/property setting etc) for individual sniffs may not always be handled correctly,
48+
* which is why naming a standard "Internal" is definitely not recommended.
49+
*
50+
* @return void
51+
*/
52+
public function testInternalStandardDoesGetExpanded()
53+
{
54+
// Set up the ruleset.
55+
$standard = __DIR__.'/ExpandRulesetReferenceInternalStandardTest.xml';
56+
$config = new ConfigDouble(["--standard=$standard"]);
57+
$ruleset = new Ruleset($config);
58+
59+
$expected = ['Internal.Valid.Valid' => 'Fixtures\\Internal\\Sniffs\\Valid\\ValidSniff'];
60+
61+
$this->assertSame($expected, $ruleset->sniffCodes);
62+
63+
}//end testInternalStandardDoesGetExpanded()
64+
65+
66+
}//end class
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandRulesetReferenceInternalTest
6+
*/
7+
8+
namespace Fixtures\Internal\Sniffs\Valid;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class ValidSniff implements Sniff
14+
{
15+
16+
public function register()
17+
{
18+
return [T_CLASS];
19+
}
20+
21+
public function process(File $phpcsFile, $stackPtr)
22+
{
23+
// Do something.
24+
}
25+
}
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="Internal" namespace="Fixtures\Internal" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!--
5+
A standard named "Internal" can be included, but individual categories or sniffs from that standard cannot.
6+
In other words: standards should not be named "Internal".
7+
8+
This test fixture helps document and safeguard the current behaviour for standards named "Internal".
9+
-->
10+
11+
</ruleset>

0 commit comments

Comments
 (0)