Skip to content

Commit f6441ad

Browse files
committed
Universal/NoFQNTrueFalseNull: fix for changed tokenization in PHPCS 3.13.3 and 4.0
The tokenization of fully qualified `true`/`false`/`null` has been changed for both PHPCS 3.x (as of 3.13.3) as well as PHPCS 4.0. The new tokenization means we don't need to take these tokens being tokenized as `T_STRING` or `T_NAME_FULLY_QUALIFIED` into account anymore, as long as the minimum supported PHPCS version will be PHPCS 3.13.3. However, we now do need to verify that `T_TRUE`/`T_FALSE`/`T_NULL` tokens don't have a namespace separator included in their contents. This commit updates the sniff for these changes. Ref: * PHPCSStandards/PHP_CodeSniffer 1201 * PHPCSStandards/PHP_CodeSniffer 1206
1 parent 6108d2a commit f6441ad

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

Universal/Sniffs/PHP/NoFQNTrueFalseNullSniff.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Sniffs\PHP;
1212

13+
use PHP_CodeSniffer\Config;
1314
use PHP_CodeSniffer\Files\File;
1415
use PHP_CodeSniffer\Sniffs\Sniff;
1516
use PHP_CodeSniffer\Util\Tokens;
@@ -31,18 +32,17 @@ final class NoFQNTrueFalseNullSniff implements Sniff
3132
*/
3233
public function register()
3334
{
34-
return [
35-
// PHPCS 3.x on PHP < 8.0.
35+
$targets = [
3636
\T_TRUE,
3737
\T_FALSE,
3838
\T_NULL,
39+
];
3940

40-
// PHPCS 3.x on PHP >= 8.0.
41-
\T_STRING,
41+
if (\version_compare(Config::VERSION, '4.0.0', '>=') === true) {
42+
$targets[] = \T_NS_SEPARATOR;
43+
}
4244

43-
// PHPCS 4.x.
44-
\T_NAME_FULLY_QUALIFIED,
45-
];
45+
return $targets;
4646
}
4747

4848
/**
@@ -62,17 +62,21 @@ public function process(File $phpcsFile, $stackPtr)
6262
$content = $tokens[$stackPtr]['content'];
6363
$contentLC = \strtolower($content);
6464

65-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
65+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
6666
// PHPCS 4.x.
67-
if ($contentLC !== '\true' && $contentLC !== '\false' && $contentLC !== '\null') {
67+
} elseif ($tokens[$stackPtr]['code'] === \T_NS_SEPARATOR) {
68+
// PHPCS 4.x for code which is a parse error on PHP 8.0+.
69+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
70+
if ($tokens[$next]['code'] !== \T_STRING) {
6871
return;
6972
}
70-
} else {
71-
// PHPCS 3.x.
72-
if ($contentLC !== 'true' && $contentLC !== 'false' && $contentLC !== 'null') {
73+
74+
$nextContentLC = \strtolower($tokens[$next]['content']);
75+
if ($nextContentLC !== 'true' && $nextContentLC !== 'false' && $nextContentLC !== 'null') {
7376
return;
7477
}
75-
78+
} else {
79+
// PHPCS 3.x.
7680
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
7781
if ($tokens[$prev]['code'] !== \T_NS_SEPARATOR) {
7882
return;
@@ -97,9 +101,12 @@ public function process(File $phpcsFile, $stackPtr)
97101
);
98102

99103
if ($fix === true) {
100-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
104+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
101105
// PHPCS 4.x.
102106
$phpcsFile->fixer->replaceToken($stackPtr, \ltrim($tokens[$stackPtr]['content'], '\\'));
107+
} elseif ($tokens[$stackPtr]['code'] === \T_NS_SEPARATOR) {
108+
// PHPCS 4.x for code which is a parse error on PHP 8.0+.
109+
$phpcsFile->fixer->replaceToken($stackPtr, '');
103110
} else {
104111
// PHPCS 3.x.
105112
$phpcsFile->fixer->replaceToken($prev, '');

0 commit comments

Comments
 (0)