1111
1212use PHP_CodeSniffer \Util \Tokens ;
1313use PHPCSUtils \Utils \PassedParameters ;
14- use WordPressVIPMinimum \ Sniffs \ Sniff ;
14+ use WordPressCS \ WordPress \ AbstractFunctionParameterSniff ;
1515
1616/**
1717 * Sniff for properly using constant name when checking whether a constant is defined.
1818 */
19- class ConstantStringSniff extends Sniff {
19+ class ConstantStringSniff extends AbstractFunctionParameterSniff {
2020
2121 /**
22- * Returns an array of tokens this test wants to listen for .
22+ * The group name for this group of functions .
2323 *
24- * @return array<int| string>
24+ * @var string
2525 */
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+ ];
3137
3238 /**
33- * Process this test when one of its tokens is encountered .
39+ * Process the parameters of a matched function .
3440 *
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.
3646 *
3747 * @return void
3848 */
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 ' );
5951 if ( $ param === false ) {
6052 // Target parameter not found.
6153 return ;
@@ -72,7 +64,7 @@ public function process_token( $stackPtr ) {
7264
7365 $ tstring_token = $ this ->phpcsFile ->findNext ( T_STRING , $ param ['start ' ], $ param ['end ' ] + 1 );
7466
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 . ' ;
7668 $ data = [ $ this ->tokens [ $ stackPtr ]['content ' ] ];
7769 $ this ->phpcsFile ->addError ( $ message , $ tstring_token , 'NotCheckingConstantName ' , $ data );
7870 }
0 commit comments