11
11
12
12
use PHP_CodeSniffer \Util \Tokens ;
13
13
use PHPCSUtils \Utils \PassedParameters ;
14
- use WordPressVIPMinimum \ Sniffs \ Sniff ;
14
+ use WordPressCS \ WordPress \ AbstractFunctionParameterSniff ;
15
15
16
16
/**
17
17
* Sniff for properly using constant name when checking whether a constant is defined.
18
18
*/
19
- class ConstantStringSniff extends Sniff {
19
+ class ConstantStringSniff extends AbstractFunctionParameterSniff {
20
20
21
21
/**
22
- * Returns an array of tokens this test wants to listen for .
22
+ * The group name for this group of functions .
23
23
*
24
- * @return array<int| string>
24
+ * @var string
25
25
*/
26
- public function register () {
27
- return [
28
- T_STRING ,
29
- ];
30
- }
26
+ protected $ group_name = 'constant_functions ' ;
27
+
28
+ /**
29
+ * Functions this sniff is looking for.
30
+ *
31
+ * @var array<string, bool> Key is the function name, value irrelevant.
32
+ */
33
+ protected $ target_functions = [
34
+ 'define ' => true ,
35
+ 'defined ' => true ,
36
+ ];
31
37
32
38
/**
33
- * Process this test when one of its tokens is encountered .
39
+ * Process the parameters of a matched function .
34
40
*
35
- * @param int $stackPtr The position of the current token in the stack passed in $tokens.
41
+ * @param int $stackPtr The position of the current token in the stack.
42
+ * @param string $group_name The name of the group which was matched.
43
+ * @param string $matched_content The token content (function name) which was matched
44
+ * in lowercase.
45
+ * @param array $parameters Array with information about the parameters.
36
46
*
37
47
* @return void
38
48
*/
39
- public function process_token ( $ stackPtr ) {
40
-
41
- if ( in_array ( $ this ->tokens [ $ stackPtr ]['content ' ], [ 'define ' , 'defined ' ], true ) === false ) {
42
- return ;
43
- }
44
-
45
- // Find the next non-empty token.
46
- $ nextToken = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , $ stackPtr + 1 , null , true , null , true );
47
-
48
- if ( $ this ->tokens [ $ nextToken ]['code ' ] !== T_OPEN_PARENTHESIS ) {
49
- // Not a function call.
50
- return ;
51
- }
52
-
53
- if ( isset ( $ this ->tokens [ $ nextToken ]['parenthesis_closer ' ] ) === false ) {
54
- // Not a function call.
55
- return ;
56
- }
57
-
58
- $ param = PassedParameters::getParameter ( $ this ->phpcsFile , $ stackPtr , 1 , 'constant_name ' );
49
+ public function process_parameters ( $ stackPtr , $ group_name , $ matched_content , $ parameters ) {
50
+ $ param = PassedParameters::getParameterFromStack ( $ parameters , 1 , 'constant_name ' );
59
51
if ( $ param === false ) {
60
52
// Target parameter not found.
61
53
return ;
@@ -72,7 +64,7 @@ public function process_token( $stackPtr ) {
72
64
73
65
$ tstring_token = $ this ->phpcsFile ->findNext ( T_STRING , $ param ['start ' ], $ param ['end ' ] + 1 );
74
66
75
- $ message = 'Constant name, as a string, should be used along with `%s()` . ' ;
67
+ $ message = 'The `%s()` function expects to be passed the constant name as a text string . ' ;
76
68
$ data = [ $ this ->tokens [ $ stackPtr ]['content ' ] ];
77
69
$ this ->phpcsFile ->addError ( $ message , $ tstring_token , 'NotCheckingConstantName ' , $ data );
78
70
}
0 commit comments