Skip to content

Commit 262f783

Browse files
committed
Use appropriate namespaces in all Sniffs in the WordPressVIPMinimum standard.
Re-enable the PSR1 Class Declaration sniff for the code style of the project itself.
1 parent 1c38087 commit 262f783

14 files changed

+199
-146
lines changed

WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
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 Actions_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.
@@ -98,7 +103,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
98103
$this->processClosure( $callbackPtr );
99104
} elseif ( 'T_ARRAY' === $this->_tokens[ $callbackPtr ]['type'] ) {
100105
$this->processArray( $callbackPtr );
101-
} elseif ( true === in_array( $this->_tokens[ $callbackPtr ]['code'], PHP_CodeSniffer_Tokens::$stringTokens, true ) ) {
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.
@@ -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;

WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* @package VIPCS\WordPressVIPMinimum
66
*/
77

8+
namespace WordPressVIPMinimum\Sniffs\Cache;
9+
10+
use PHP_CodeSniffer_File as File;
11+
use PHP_CodeSniffer_Tokens as Tokens;
12+
813
/**
914
* This sniff enforces checking the return value of a function before passing it to anoher one.
1015
*
@@ -16,7 +21,7 @@
1621
*
1722
* @package VIPCS\WordPressVIPMinimum
1823
*/
19-
class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_CodeSniffer_Sniff {
24+
class CacheValueOverrideSniff implements \PHP_CodeSniffer_Sniff {
2025

2126
/**
2227
* Tokens of the file.
@@ -31,21 +36,21 @@ class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_Co
3136
* @return array(int)
3237
*/
3338
public function register() {
34-
return PHP_CodeSniffer_Tokens::$functionNameTokens;
39+
return Tokens::$functionNameTokens;
3540

3641
}//end register()
3742

3843

3944
/**
4045
* Processes the tokens that this sniff is interested in.
4146
*
42-
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
43-
* @param int $stackPtr The position in the stack where
44-
* the token was found.
47+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
48+
* @param int $stackPtr The position in the stack where
49+
* the token was found.
4550
*
4651
* @return void
4752
*/
48-
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
53+
public function process( File $phpcsFile, $stackPtr ) {
4954

5055
$this->_tokens = $phpcsFile->getTokens();
5156
$tokens = $phpcsFile->getTokens();
@@ -74,21 +79,21 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
7479
$variableName = $variableToken['content'];
7580

7681
// Find the next non-empty token.
77-
$openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true );
82+
$openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true );
7883

7984
// Find the closing bracket.
8085
$closeBracket = $tokens[ $openBracket ]['parenthesis_closer'];
8186

8287
$nextVariableOccurrence = $phpcsFile->findNext( T_VARIABLE, ($closeBracket + 1), null, false, $variableName, false );
8388

84-
$rightAfterNextVariableOccurence = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true );
89+
$rightAfterNextVariableOccurence = $phpcsFile->findNext( Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true );
8590

8691
if ( T_EQUAL !== $tokens[ $rightAfterNextVariableOccurence ]['code'] ) {
8792
// Not a value override.
8893
return;
8994
}
9095

91-
$valueAfterEqualSign = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true );
96+
$valueAfterEqualSign = $phpcsFile->findNext( Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true );
9297

9398
if ( T_FALSE === $tokens[ $valueAfterEqualSign ]['code'] ) {
9499
$phpcsFile->addError( sprintf( 'Obtained cached value in %s is being overriden. Disabling caching?', $variableName ), $nextVariableOccurrence, 'WordPressVIPMinimum.Cache.CacheValueOverride' );
@@ -108,20 +113,20 @@ private function isFunctionCall( $stackPtr ) {
108113
$tokens = $this->_tokens;
109114
$phpcsFile = $this->_phpcsFile;
110115

111-
if ( false === in_array( $tokens[ $stackPtr ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) ) {
116+
if ( false === in_array( $tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) ) {
112117
return false;
113118
}
114119

115120
// Find the next non-empty token.
116-
$openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true );
121+
$openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true );
117122

118123
if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) {
119124
// Not a function call.
120125
return false;
121126
}
122127

123128
// Find the previous non-empty token.
124-
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
129+
$search = Tokens::$emptyTokens;
125130
$search[] = T_BITWISE_AND;
126131
$previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true );
127132
if ( T_FUNCTION === $tokens[ $previous ]['code'] ) {
@@ -145,7 +150,7 @@ private function isVariableAssignment( $stackPtr ) {
145150
$phpcsFile = $this->_phpcsFile;
146151

147152
// Find the previous non-empty token.
148-
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
153+
$search = Tokens::$emptyTokens;
149154
$search[] = T_BITWISE_AND;
150155
$previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true );
151156

WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* @package VIPCS\WordPressVIPMinimum
66
*/
77

8+
namespace WordPressVIPMinimum\Sniffs\Classes;
9+
10+
use PHP_CodeSniffer_File as File;
11+
812
// WPCS pre 0.1.3 backwardcompatibility.
913
if ( ! class_exists( '\PHP_CodeSniffer_Standards_AbstractScopeSniff' ) ) {
1014
class_alias( 'PHP_CodeSniffer\Sniffs\AbstractScopeSniff', '\PHP_CodeSniffer_Standards_AbstractScopeSniff' );
@@ -15,7 +19,7 @@ class_alias( 'PHP_CodeSniffer\Sniffs\AbstractScopeSniff', '\PHP_CodeSniffer_Stan
1519
*
1620
* @package VIPCS\WordPressVIPMinimum
1721
*/
18-
class WordPressVIPMinimum_Sniffs_Classes_DeclarationCompatibilitySniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff {
22+
class DeclarationCompatibilitySniff extends \PHP_CodeSniffer_Standards_AbstractScopeSniff {
1923

2024
/**
2125
* The name of the class we are currently checking.
@@ -194,14 +198,14 @@ public function __construct() {
194198
/**
195199
* Processes this test when one of its tokens is encountered.
196200
*
197-
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
198-
* @param int $stackPtr The position of the current token
199-
* in the stack passed in $tokens.
200-
* @param int $currScope A pointer to the start of the scope.
201+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
202+
* @param int $stackPtr The position of the current token
203+
* in the stack passed in $tokens.
204+
* @param int $currScope A pointer to the start of the scope.
201205
*
202206
* @return void
203207
*/
204-
protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope ) {
208+
protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) {
205209

206210
$className = $phpcsFile->getDeclarationName( $currScope );
207211

@@ -288,7 +292,7 @@ protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $st
288292
* @param string $methodName The name of the method currently being examined.
289293
* @param array $currentMethodSignature The list of params and their options of the method which is being examined.
290294
* @param array $parentMethodSignature The list of params and their options of the parent class method.
291-
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
295+
* @param File $phpcsFile The file being scanned.
292296
* @param int $stackPtr The position of the current token in the stack.
293297
*
294298
* @return void
@@ -341,12 +345,12 @@ private function generateParamList( $methodSignature ) {
341345
/**
342346
* Extracts all the function names found in the given scope.
343347
*
344-
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
345-
* @param int $currScope A pointer to the start of the scope.
348+
* @param File $phpcsFile The current file being scanned.
349+
* @param int $currScope A pointer to the start of the scope.
346350
*
347351
* @return void
348352
*/
349-
protected function loadFunctionNamesInScope( PHP_CodeSniffer_File $phpcsFile, $currScope ) {
353+
protected function loadFunctionNamesInScope( File $phpcsFile, $currScope ) {
350354
$this->_functionList = array();
351355
$tokens = $phpcsFile->getTokens();
352356

@@ -364,8 +368,8 @@ protected function loadFunctionNamesInScope( PHP_CodeSniffer_File $phpcsFile, $c
364368
/**
365369
* Do nothing outside the scope. Has to be implemented accordingly to parent abstract class.
366370
*
367-
* @param PHP_CodeSniffer_File $phpcsFile PHPCS File.
368-
* @param int $stackPtr Stack position.
371+
* @param File $phpcsFile PHPCS File.
372+
* @param int $stackPtr Stack position.
369373
*/
370-
public function processTokenOutsideScope( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {}
374+
public function processTokenOutsideScope( File $phpcsFile, $stackPtr ) {}
371375
}

WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
* @link https://github.com/Automattic/VIP-Coding-Standards
77
*/
88

9+
namespace WordPressVIPMinimum\Sniffs\Constants;
10+
11+
use PHP_CodeSniffer_File as File;
12+
use PHP_CodeSniffer_Tokens as Tokens;
13+
914
/**
1015
* Restricts usage of some constants.
1116
*
1217
* @package VIPCS\WordPressVIPMinimum
1318
*/
14-
class WordPressVIPMinimum_Sniffs_Constants_ConstantRestrictionsSniff implements PHP_CodeSniffer_Sniff {
19+
class ConstantRestrictionsSniff implements \PHP_CodeSniffer_Sniff {
1520

1621
/**
1722
* List of restricted constant names.
@@ -37,12 +42,12 @@ public function register() {
3742
/**
3843
* Process this test when one of its tokens is encoutnered
3944
*
40-
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41-
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
45+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
4247
*
4348
* @return void
4449
*/
45-
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
50+
public function process( File $phpcsFile, $stackPtr ) {
4651

4752
$tokens = $phpcsFile->getTokens();
4853

@@ -63,7 +68,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
6368
}
6469

6570
// Find the previous non-empty token.
66-
$openBracket = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true );
71+
$openBracket = $phpcsFile->findPrevious( Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true );
6772

6873
if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) {
6974
// Not a function call.
@@ -76,15 +81,15 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
7681
}
7782

7883
// Find the previous non-empty token.
79-
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
84+
$search = Tokens::$emptyTokens;
8085
$search[] = T_BITWISE_AND;
8186
$previous = $phpcsFile->findPrevious( $search, ($openBracket - 1), null, true );
8287
if ( T_FUNCTION === $tokens[ $previous ]['code'] ) {
8388
// It's a function definition, not a function call.
8489
return;
8590
}
8691

87-
if ( true === in_array( $tokens[ $previous ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) ) {
92+
if ( true === in_array( $tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) ) {
8893
if ( 'define' === $tokens[ $previous ]['content'] ) {
8994
$phpcsFile->addError( sprintf( 'The definition of %s constant is prohibited. Please use a different name.', $constantName ), $previous, 'WordPressVIPMinimum.Constants.ConstantRestrictions' );
9095
} else {

0 commit comments

Comments
 (0)