Skip to content

Commit 83f7062

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 83f7062

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

Universal/Sniffs/PHP/NoFQNTrueFalseNullSniff.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,9 @@ final class NoFQNTrueFalseNullSniff implements Sniff
3232
public function register()
3333
{
3434
return [
35-
// PHPCS 3.x on PHP < 8.0.
3635
\T_TRUE,
3736
\T_FALSE,
3837
\T_NULL,
39-
40-
// PHPCS 3.x on PHP >= 8.0.
41-
\T_STRING,
42-
43-
// PHPCS 4.x.
44-
\T_NAME_FULLY_QUALIFIED,
4538
];
4639
}
4740

@@ -62,17 +55,12 @@ public function process(File $phpcsFile, $stackPtr)
6255
$content = $tokens[$stackPtr]['content'];
6356
$contentLC = \strtolower($content);
6457

65-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
58+
$error = false;
59+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
6660
// PHPCS 4.x.
67-
if ($contentLC !== '\true' && $contentLC !== '\false' && $contentLC !== '\null') {
68-
return;
69-
}
61+
$error = true;
7062
} else {
7163
// PHPCS 3.x.
72-
if ($contentLC !== 'true' && $contentLC !== 'false' && $contentLC !== 'null') {
73-
return;
74-
}
75-
7664
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
7765
if ($tokens[$prev]['code'] !== \T_NS_SEPARATOR) {
7866
return;
@@ -87,6 +75,12 @@ public function process(File $phpcsFile, $stackPtr)
8775
if ($tokens[$next]['code'] === \T_NS_SEPARATOR) {
8876
return;
8977
}
78+
79+
$error = true;
80+
}
81+
82+
if ($error === false) {
83+
return;
9084
}
9185

9286
$fix = $phpcsFile->addFixableError(
@@ -97,7 +91,7 @@ public function process(File $phpcsFile, $stackPtr)
9791
);
9892

9993
if ($fix === true) {
100-
if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
94+
if ($contentLC === '\true' || $contentLC === '\false' || $contentLC === '\null') {
10195
// PHPCS 4.x.
10296
$phpcsFile->fixer->replaceToken($stackPtr, \ltrim($tokens[$stackPtr]['content'], '\\'));
10397
} else {

0 commit comments

Comments
 (0)