-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathConstantStringSniff.php
78 lines (64 loc) · 2.11 KB
/
ConstantStringSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* WordPress-VIP-Minimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://github.com/Automattic/VIP-Coding-Standards
*/
namespace WordPressVIPMinimum\Sniffs\Constants;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\PassedParameters;
use WordPressVIPMinimum\Sniffs\Sniff;
/**
* Sniff for properly using constant name when checking whether a constant is defined.
*/
class ConstantStringSniff extends Sniff {
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
return [
T_STRING,
];
}
/**
* Process this test when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process_token( $stackPtr ) {
if ( in_array( $this->tokens[ $stackPtr ]['content'], [ 'define', 'defined' ], true ) === false ) {
return;
}
// Find the next non-empty token.
$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true );
if ( $this->tokens[ $nextToken ]['code'] !== T_OPEN_PARENTHESIS ) {
// Not a function call.
return;
}
if ( isset( $this->tokens[ $nextToken ]['parenthesis_closer'] ) === false ) {
// Not a function call.
return;
}
$param = PassedParameters::getParameter( $this->phpcsFile, $stackPtr, 1, 'constant_name' );
if ( $param === false ) {
// Target parameter not found.
return;
}
$search = Tokens::$emptyTokens;
$search[ T_STRING ] = T_STRING;
$has_only_tstring = $this->phpcsFile->findNext( $search, $param['start'], $param['end'] + 1, true );
if ( $has_only_tstring !== false ) {
// Came across something other than a T_STRING token. Ignore.
return;
}
$tstring_token = $this->phpcsFile->findNext( T_STRING, $param['start'], $param['end'] + 1 );
$message = 'Constant name, as a string, should be used along with `%s()`.';
$data = [ $this->tokens[ $stackPtr ]['content'] ];
$this->phpcsFile->addError( $message, $tstring_token, 'NotCheckingConstantName', $data );
}
}