Skip to content

Commit f91c044

Browse files
authored
Merge pull request #872 from PHPCSStandards/feature/config-add-tests-for-extensions
Config: add tests for setting `--extensions`
2 parents f1ab93f + 25d0f75 commit f91c044

File tree

2 files changed

+145
-14
lines changed

2 files changed

+145
-14
lines changed

src/Config.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,21 +1141,24 @@ public function processLongArgument($arg, $pos)
11411141
break;
11421142
}
11431143

1144-
$extensions = explode(',', substr($arg, 11));
1145-
$newExtensions = [];
1146-
foreach ($extensions as $ext) {
1147-
$slash = strpos($ext, '/');
1148-
if ($slash !== false) {
1149-
// They specified the tokenizer too.
1150-
list($ext, $tokenizer) = explode('/', $ext);
1151-
$newExtensions[$ext] = strtoupper($tokenizer);
1152-
continue;
1153-
}
1144+
$extensionsString = substr($arg, 11);
1145+
$newExtensions = [];
1146+
if (empty($extensionsString) === false) {
1147+
$extensions = explode(',', $extensionsString);
1148+
foreach ($extensions as $ext) {
1149+
$slash = strpos($ext, '/');
1150+
if ($slash !== false) {
1151+
// They specified the tokenizer too.
1152+
list($ext, $tokenizer) = explode('/', $ext);
1153+
$newExtensions[$ext] = strtoupper($tokenizer);
1154+
continue;
1155+
}
11541156

1155-
if (isset($this->extensions[$ext]) === true) {
1156-
$newExtensions[$ext] = $this->extensions[$ext];
1157-
} else {
1158-
$newExtensions[$ext] = 'PHP';
1157+
if (isset($this->extensions[$ext]) === true) {
1158+
$newExtensions[$ext] = $this->extensions[$ext];
1159+
} else {
1160+
$newExtensions[$ext] = 'PHP';
1161+
}
11591162
}
11601163
}
11611164

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' => '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' => 'PHP',
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' => 'PHP',
84+
'js' => 'JS',
85+
'css' => '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' => 'PHP',
92+
'inc' => 'PHP',
93+
'phpt' => 'PHP',
94+
'js' => 'JS',
95+
],
96+
],
97+
'File extensions are set case sensitively (and filtering is case sensitive too)' => [
98+
'passedValue' => 'PHP,php',
99+
'expected' => [
100+
'PHP' => 'PHP',
101+
'php' => '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' => 'PHP'], $config->extensions);
124+
125+
}//end testOnlySetOnce()
126+
127+
128+
}//end class

0 commit comments

Comments
 (0)