Skip to content

Commit f2e1d78

Browse files
jrfnlgsherwood
andcommitted
Config: drop support for setting language via --extensions CLI arg
This has been implemented slightly differently from the original PHPCS 4.0 branch implementation. 1. The array format of the `Config::$extensions` property has not been changed. The format is an associative array with the extensions as keys and the tokenizer type as the value. Originally, the format was changed to a list with the extensions as values. While information on the tokenizer type now no longer has any value as the only supported tokenizer is PHP, changing the array format could break integrations in unexpected ways, so I have decided against this. 2. I have also elected to throw an error when the passed extensions contain the `/tokenizer` switch, but only if the tokenizer is set to something other than PHP. In the original implementation, no information was provided to the user and all extensions values would be accepted and expected to be acceptable, including values which contained the `/tokenizer` switch. Note: users can still pass extensions for unsupported file types. This is no different than before, either in the previous implementation or in PHPCS 3.x. I mean, users could already pass `html` as an extension and it would be scanned as if it were PHP. Whether the results of such scans would be useful is doubtful, but that's the user's responsibility. So if the user passes `--extensions=php,js`, as of PHPCS 4.0, javascript files will be scanned as if they were PHP. Ref: * squizlabs/PHP_CodeSniffer 2448 Co-authored-by: Greg Sherwood <[email protected]>
1 parent 4cc3e59 commit f2e1d78

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

src/Config.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@
6464
* @property string $stdinPath The path to use for content passed on STDIN.
6565
* @property bool $trackTime Whether or not to track sniff run time.
6666
*
67-
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer to use.
67+
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer is used.
6868
* E.g., array('inc' => 'PHP');
69+
* Note: since PHPCS 4.0.0, the tokenizer used will always be 'PHP',
70+
* but the array format of the property has not been changed to prevent
71+
* breaking integrations which may be accessing this property.
6972
* @property array<string, string|null> $reports The reports to use for printing output after the run.
7073
* The format of the array is:
7174
* array(
@@ -545,8 +548,6 @@ public function restoreDefaults()
545548
$this->extensions = [
546549
'php' => 'PHP',
547550
'inc' => 'PHP',
548-
'js' => 'JS',
549-
'css' => 'CSS',
550551
];
551552
$this->sniffs = [];
552553
$this->exclude = [];
@@ -1146,19 +1147,19 @@ public function processLongArgument($arg, $pos)
11461147
if (empty($extensionsString) === false) {
11471148
$extensions = explode(',', $extensionsString);
11481149
foreach ($extensions as $ext) {
1149-
$slash = strpos($ext, '/');
1150-
if ($slash !== false) {
1150+
if (strpos($ext, '/') !== false) {
11511151
// They specified the tokenizer too.
11521152
list($ext, $tokenizer) = explode('/', $ext);
1153-
$newExtensions[$ext] = strtoupper($tokenizer);
1154-
continue;
1153+
if (strtoupper($tokenizer) !== 'PHP') {
1154+
$error = 'ERROR: Specifying the tokenizer to use for an extension is no longer supported.'.PHP_EOL;
1155+
$error .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
1156+
$error .= 'Received: '.substr($arg, 11).PHP_EOL.PHP_EOL;
1157+
$error .= $this->printShortUsage(true);
1158+
throw new DeepExitException($error, 3);
1159+
}
11551160
}
11561161

1157-
if (isset($this->extensions[$ext]) === true) {
1158-
$newExtensions[$ext] = $this->extensions[$ext];
1159-
} else {
1160-
$newExtensions[$ext] = 'PHP';
1161-
}
1162+
$newExtensions[$ext] = 'PHP';
11621163
}
11631164
}
11641165

src/Util/Help.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ private function getAllOptions()
412412
],
413413
'extensions' => [
414414
'argument' => '--extensions=<extensions>',
415-
'description' => 'Check files with the specified file extensions (comma-separated list). Defaults to php,inc/php,js,css.'."\n"
416-
.'The type of the file can be specified using: ext/type; e.g. module/php,es/js.',
415+
'description' => 'Check files with the specified file extensions (comma-separated list). Defaults to "php,inc".',
417416
],
418417
'l' => [
419418
'argument' => '-l',

tests/Core/Config/ExtensionsArgTest.php

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,23 @@ public static function dataValidExtensions()
6969
'py' => 'PHP',
7070
],
7171
],
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-
],
8072
'Multiple extensions passed: php,js,css' => [
8173
'passedValue' => 'php,js,css',
8274
'expected' => [
8375
'php' => 'PHP',
84-
'js' => 'JS',
85-
'css' => 'CSS',
76+
'js' => 'PHP',
77+
'css' => 'PHP',
8678
],
8779
],
88-
'Multiple extensions passed, some with language: php,inc/php,phpt/php,js' => [
80+
// While setting the language is no longer supported, we are being tolerant to the language
81+
// being set to PHP as that doesn't break anything.
82+
'Multiple extensions passed, some with language PHP: php,inc/php,phpt/php,js' => [
8983
'passedValue' => 'php,inc/php,phpt/php,js',
9084
'expected' => [
9185
'php' => 'PHP',
9286
'inc' => 'PHP',
9387
'phpt' => 'PHP',
94-
'js' => 'JS',
88+
'js' => 'PHP',
9589
],
9690
],
9791
'File extensions are set case sensitively (and filtering is case sensitive too)' => [
@@ -125,4 +119,51 @@ public function testOnlySetOnce()
125119
}//end testOnlySetOnce()
126120

127121

122+
/**
123+
* Ensure that an exception is thrown for an invalid extension.
124+
*
125+
* @param string $passedValue Extensions as passed on the command line.
126+
*
127+
* @dataProvider dataInvalidExtensions
128+
*
129+
* @return void
130+
*/
131+
public function testInvalidExtensions($passedValue)
132+
{
133+
$message = 'ERROR: Specifying the tokenizer to use for an extension is no longer supported.'.PHP_EOL;
134+
$message .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
135+
$message .= 'Received: '.$passedValue.PHP_EOL.PHP_EOL;
136+
137+
$this->expectException('PHP_CodeSniffer\Exceptions\DeepExitException');
138+
$this->expectExceptionMessage($message);
139+
140+
new ConfigDouble(["--extensions={$passedValue}"]);
141+
142+
}//end testInvalidExtensions()
143+
144+
145+
/**
146+
* Data provider.
147+
*
148+
* @see self::testInvalidExtensions()
149+
*
150+
* @return array<string, array<string, string>>
151+
*/
152+
public static function dataInvalidExtensions()
153+
{
154+
return [
155+
'Single extension passed with language: py/py' => [
156+
'passedValue' => 'py/py',
157+
],
158+
'Multiple extensions passed, all setting non-PHP language: ts/js,less/css' => [
159+
'passedValue' => 'ts/js,less/css',
160+
],
161+
'Multiple extensions passed, some with non-PHP language: js/js,phpt/php' => [
162+
'passedValue' => 'php,js/js,phpt/php',
163+
],
164+
];
165+
166+
}//end dataInvalidExtensions()
167+
168+
128169
}//end class

tests/Core/Filters/AbstractFilterTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class AbstractFilterTestCase extends TestCase
4545
*/
4646
public static function initializeConfigAndRuleset()
4747
{
48-
self::$config = new ConfigDouble(['--extensions=php,inc/php,js,css']);
48+
self::$config = new ConfigDouble();
4949
self::$ruleset = new Ruleset(self::$config);
5050

5151
}//end initializeConfigAndRuleset()

0 commit comments

Comments
 (0)