55 * @package VIPCS\WordPressVIPMinimum
66 */
77
8+ namespace WordPressVIPMinimum \Sniffs \Actions ;
9+
10+ use PHP_CodeSniffer_File as File ;
11+ use PHP_CodeSniffer_Tokens as Tokens ;
12+
813/**
914 * This sniff validates a propper usage of pre_get_posts action callback
1015 *
1116 * It looks for cases when the WP_Query object is being modified without checking for WP_Query::is_main_query().
1217 *
1318 * @package VIPCS\WordPressVIPMinimum
1419 */
15- class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSniffer_Sniff {
20+ class PreGetPostsSniff implements \ PHP_CodeSniffer_Sniff {
1621
1722 /**
1823 * The tokens of the phpcsFile.
@@ -34,21 +39,21 @@ class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSni
3439 * @return array(int)
3540 */
3641 public function register () {
37- return PHP_CodeSniffer_Tokens ::$ functionNameTokens ;
42+ return Tokens ::$ functionNameTokens ;
3843
3944 }//end register()
4045
4146
4247 /**
4348 * Processes the tokens that this sniff is interested in.
4449 *
45- * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
46- * @param int $stackPtr The position in the stack where
47- * the token was found.
50+ * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
51+ * @param int $stackPtr The position in the stack where
52+ * the token was found.
4853 *
4954 * @return void
5055 */
51- public function process ( PHP_CodeSniffer_File $ phpcsFile , $ stackPtr ) {
56+ public function process ( File $ phpcsFile , $ stackPtr ) {
5257
5358 $ this ->_tokens = $ phpcsFile ->getTokens ();
5459
@@ -62,7 +67,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
6267 }
6368
6469 $ actionNamePtr = $ this ->_phpcsFile ->findNext (
65- array_merge ( PHP_CodeSniffer_Tokens ::$ emptyTokens , array ( T_OPEN_PARENTHESIS ) ), // types.
70+ array_merge ( Tokens ::$ emptyTokens , array ( T_OPEN_PARENTHESIS ) ), // types.
6671 $ stackPtr + 1 , // start.
6772 null , // end.
6873 true , // exclude.
@@ -81,7 +86,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
8186 }
8287
8388 $ callbackPtr = $ this ->_phpcsFile ->findNext (
84- array_merge ( PHP_CodeSniffer_Tokens ::$ emptyTokens , array ( T_COMMA ) ), // types.
89+ array_merge ( Tokens ::$ emptyTokens , array ( T_COMMA ) ), // types.
8590 $ actionNamePtr + 1 , // start.
8691 null , // end.
8792 true , // exclude.
@@ -96,9 +101,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
96101
97102 if ( 'PHPCS_T_CLOSURE ' === $ this ->_tokens [ $ callbackPtr ]['code ' ] ) {
98103 $ this ->processClosure ( $ callbackPtr );
99- } else if ( 'T_ARRAY ' === $ this ->_tokens [ $ callbackPtr ]['type ' ] ) {
104+ } elseif ( 'T_ARRAY ' === $ this ->_tokens [ $ callbackPtr ]['type ' ] ) {
100105 $ this ->processArray ( $ callbackPtr );
101- } else if ( true === in_array ( $ this ->_tokens [ $ callbackPtr ]['code ' ], PHP_CodeSniffer_Tokens ::$ stringTokens ) ) {
106+ } elseif ( true === in_array ( $ this ->_tokens [ $ callbackPtr ]['code ' ], Tokens ::$ stringTokens, true ) ) {
102107 $ this ->processString ( $ callbackPtr );
103108 }
104109
@@ -112,7 +117,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
112117 private function processArray ( $ stackPtr ) {
113118
114119 $ previous = $ this ->_phpcsFile ->findPrevious (
115- PHP_CodeSniffer_Tokens ::$ emptyTokens , // types
120+ Tokens ::$ emptyTokens , // types
116121 $ this ->_tokens [ $ stackPtr ]['parenthesis_closer ' ] - 1 , // start.
117122 null , // end.
118123 true , // exclude.
@@ -134,7 +139,7 @@ private function processString( $stackPtr ) {
134139 $ callbackFunctionName = substr ( $ this ->_tokens [ $ stackPtr ]['content ' ], 1 , -1 );
135140
136141 $ callbackFunctionPtr = $ this ->_phpcsFile ->findNext (
137- PHP_CodeSniffer_Tokens ::$ functionNameTokens , // types.
142+ Tokens ::$ functionNameTokens , // types.
138143 0 , // start.
139144 null , // end.
140145 false , // exclude.
@@ -242,12 +247,12 @@ private function processFunctionBody( $stackPtr, $variableName ) {
242247 if ( $ this ->isEarlyMainQueryCheck ( $ wpQueryVarUsed ) ) {
243248 return ;
244249 }
245- } else if ( $ this ->isInsideIfConditonal ( $ wpQueryVarUsed ) ) {
250+ } elseif ( $ this ->isInsideIfConditonal ( $ wpQueryVarUsed ) ) {
246251 if ( ! $ this ->isParentConditionalCheckingMainQuery ( $ wpQueryVarUsed ) ) {
247- $ this ->_phpcsFile ->addWarning ( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection. ' , $ wpQueryVarUsed );
252+ $ this ->_phpcsFile ->addWarning ( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection. ' , $ wpQueryVarUsed, ' PreGetPosts ' );
248253 }
249- } else if ( $ this ->isWPQueryMethodCall ( $ wpQueryVarUsed , 'set ' ) ) {
250- $ this ->_phpcsFile ->addWarning ( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection. ' , $ wpQueryVarUsed );
254+ } elseif ( $ this ->isWPQueryMethodCall ( $ wpQueryVarUsed , 'set ' ) ) {
255+ $ this ->_phpcsFile ->addWarning ( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection. ' , $ wpQueryVarUsed, ' PreGetPosts ' );
251256 }
252257 $ findStart = $ wpQueryVarUsed + 1 ;
253258 }
@@ -316,7 +321,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
316321 }
317322
318323 $ nestedParenthesisEnd = array_shift ( $ this ->_tokens [ $ stackPtr ]['nested_parenthesis ' ] );
319- if ( true === in_array ( 'PHPCS_T_CLOSURE ' , $ this ->_tokens [ $ stackPtr ]['conditions ' ] ) ) {
324+ if ( true === in_array ( 'PHPCS_T_CLOSURE ' , $ this ->_tokens [ $ stackPtr ]['conditions ' ], true ) ) {
320325 $ nestedParenthesisEnd = array_shift ( $ this ->_tokens [ $ stackPtr ]['nested_parenthesis ' ] );
321326 }
322327
@@ -346,7 +351,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
346351 */
347352 private function isWPQueryMethodCall ( $ stackPtr , $ method = null ) {
348353 $ next = $ this ->_phpcsFile ->findNext (
349- PHP_CodeSniffer_Tokens ::$ emptyTokens , // types.
354+ Tokens ::$ emptyTokens , // types.
350355 $ stackPtr + 1 , // start.
351356 null , // end.
352357 true , // exclude.
@@ -363,7 +368,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) {
363368 }
364369
365370 $ next = $ this ->_phpcsFile ->findNext (
366- PHP_CodeSniffer_Tokens ::$ emptyTokens , // types.
371+ Tokens ::$ emptyTokens , // types.
367372 $ next + 1 , // start.
368373 null , // end.
369374 true , // exclude.
@@ -372,7 +377,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) {
372377 );
373378
374379 if ( $ next &&
375- true === in_array ( $ this ->_tokens [ $ next ]['code ' ], PHP_CodeSniffer_Tokens ::$ functionNameTokens , true ) &&
380+ true === in_array ( $ this ->_tokens [ $ next ]['code ' ], Tokens ::$ functionNameTokens , true ) &&
376381 $ method === $ this ->_tokens [ $ next ]['content ' ]
377382 ) {
378383 return true ;
0 commit comments