1010
1111namespace PHPCSExtra \Universal \Sniffs \PHP ;
1212
13+ use PHP_CodeSniffer \Config ;
1314use PHP_CodeSniffer \Files \File ;
1415use PHP_CodeSniffer \Sniffs \Sniff ;
1516use 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