Skip to content

Commit 5d1a922

Browse files
committed
Config: add tests for setting --extensions
Includes minor bug fix to treat an empty value correctly. Previously, passing `--extensions=` (without value) would result in the `Config::$extensions` property being set to: ```php public $extensions = [ '' => 'PHP' ]; ``` Now, an empty value will set the extensions array to an empty array. ```php public $extensions = []; ``` Note: in practice, the above change will not make a difference in how files are filtered/scanned. :point_right: The change to the `Config` class will be most straight-forward to review while ignoring whitespace changes.
1 parent d276f42 commit 5d1a922

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

src/Config.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,13 @@ public function processLongArgument($arg, $pos)
11391139
break;
11401140
}
11411141

1142-
$this->extensions = explode(',', substr($arg, 11));
1142+
$extensionsString = substr($arg, 11);
1143+
$newExtensions = [];
1144+
if (empty($extensionsString) === false) {
1145+
$newExtensions = explode(',', $extensionsString);
1146+
}
1147+
1148+
$this->extensions = $newExtensions;
11431149
$this->overriddenDefaults['extensions'] = true;
11441150
} else if (substr($arg, 0, 7) === 'suffix=') {
11451151
if (isset($this->overriddenDefaults['suffix']) === true) {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Config --extensions argument.
4+
*
5+
* @copyright 2025 PHPCSStandards and contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Config;
10+
11+
use PHP_CodeSniffer\Tests\ConfigDouble;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Tests for the \PHP_CodeSniffer\Config --extensions argument.
16+
*
17+
* @covers \PHP_CodeSniffer\Config::processLongArgument
18+
*/
19+
final class ExtensionsArgTest extends TestCase
20+
{
21+
22+
23+
/**
24+
* Ensure that the extension property is set when the parameter is passed a valid value.
25+
*
26+
* @param string $passedValue Extensions as passed on the command line.
27+
* @param string $expected Expected value for the extensions property.
28+
*
29+
* @dataProvider dataValidExtensions
30+
*
31+
* @return void
32+
*/
33+
public function testValidExtensions($passedValue, $expected)
34+
{
35+
$config = new ConfigDouble(["--extensions=$passedValue"]);
36+
37+
$this->assertSame($expected, $config->extensions);
38+
39+
}//end testValidExtensions()
40+
41+
42+
/**
43+
* Data provider.
44+
*
45+
* @see self::testValidExtensions()
46+
*
47+
* @return array<int, array<string>>
48+
*/
49+
public static function dataValidExtensions()
50+
{
51+
return [
52+
// Passing an empty extensions list is not useful, as it will result in no files being scanned,
53+
// but that's the responsibility of the user.
54+
'Empty extensions list' => [
55+
'passedValue' => '',
56+
'expected' => [],
57+
],
58+
'Single extension passed: php' => [
59+
'passedValue' => 'php',
60+
'expected' => [
61+
'php',
62+
],
63+
],
64+
// This would cause PHPCS to scan python files as PHP, which will probably cause very weird scan results,
65+
// but that's the responsibility of the user.
66+
'Single extension passed: py' => [
67+
'passedValue' => 'py',
68+
'expected' => [
69+
'py',
70+
],
71+
],
72+
// This would likely result in a problem when PHPCS can't find a "PY" tokenizer class,
73+
// but that's not our concern at this moment. Support for non-PHP tokenizers is being dropped soon anyway.
74+
'Single extension passed with language: py/py' => [
75+
'passedValue' => 'py/py',
76+
'expected' => [
77+
'py/py',
78+
],
79+
],
80+
'Multiple extensions passed: php,js,css' => [
81+
'passedValue' => 'php,js,css',
82+
'expected' => [
83+
'php',
84+
'js',
85+
'css',
86+
],
87+
],
88+
'Multiple extensions passed, some with language: php,inc/php,phpt/php,js' => [
89+
'passedValue' => 'php,inc/php,phpt/php,js',
90+
'expected' => [
91+
'php',
92+
'inc/php',
93+
'phpt/php',
94+
'js',
95+
],
96+
],
97+
'File extensions are set case sensitively (and filtering is case sensitive too)' => [
98+
'passedValue' => 'PHP,php',
99+
'expected' => [
100+
'PHP',
101+
'php',
102+
],
103+
],
104+
];
105+
106+
}//end dataValidExtensions()
107+
108+
109+
/**
110+
* Ensure that only the first argument is processed and others are ignored.
111+
*
112+
* @return void
113+
*/
114+
public function testOnlySetOnce()
115+
{
116+
$config = new ConfigDouble(
117+
[
118+
'--extensions=php',
119+
'--extensions=inc,module',
120+
]
121+
);
122+
123+
$this->assertSame(['php'], $config->extensions);
124+
125+
}//end testOnlySetOnce()
126+
127+
128+
}//end class

0 commit comments

Comments
 (0)