10
10
11
11
namespace PHPCSExtra \Universal \Sniffs \PHP ;
12
12
13
+ use PHP_CodeSniffer \Config ;
13
14
use PHP_CodeSniffer \Files \File ;
14
15
use PHP_CodeSniffer \Sniffs \Sniff ;
15
16
use PHP_CodeSniffer \Util \Tokens ;
@@ -31,18 +32,17 @@ final class NoFQNTrueFalseNullSniff implements Sniff
31
32
*/
32
33
public function register ()
33
34
{
34
- return [
35
- // PHPCS 3.x on PHP < 8.0.
35
+ $ targets = [
36
36
\T_TRUE ,
37
37
\T_FALSE ,
38
38
\T_NULL ,
39
+ ];
39
40
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
+ }
42
44
43
- // PHPCS 4.x.
44
- \T_NAME_FULLY_QUALIFIED ,
45
- ];
45
+ return $ targets ;
46
46
}
47
47
48
48
/**
@@ -62,17 +62,21 @@ public function process(File $phpcsFile, $stackPtr)
62
62
$ content = $ tokens [$ stackPtr ]['content ' ];
63
63
$ contentLC = \strtolower ($ content );
64
64
65
- if ($ tokens [ $ stackPtr ][ ' code ' ] === \ T_NAME_FULLY_QUALIFIED ) {
65
+ if ($ contentLC === ' \true ' || $ contentLC === ' \false ' || $ contentLC === ' \null ' ) {
66
66
// 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 ) {
68
71
return ;
69
72
}
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 ' ) {
73
76
return ;
74
77
}
75
-
78
+ } else {
79
+ // PHPCS 3.x.
76
80
$ prev = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ stackPtr - 1 ), null , true );
77
81
if ($ tokens [$ prev ]['code ' ] !== \T_NS_SEPARATOR ) {
78
82
return ;
@@ -97,9 +101,12 @@ public function process(File $phpcsFile, $stackPtr)
97
101
);
98
102
99
103
if ($ fix === true ) {
100
- if ($ tokens [ $ stackPtr ][ ' code ' ] === \ T_NAME_FULLY_QUALIFIED ) {
104
+ if ($ contentLC === ' \true ' || $ contentLC === ' \false ' || $ contentLC === ' \null ' ) {
101
105
// PHPCS 4.x.
102
106
$ 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 , '' );
103
110
} else {
104
111
// PHPCS 3.x.
105
112
$ phpcsFile ->fixer ->replaceToken ($ prev , '' );
0 commit comments