|
13 | 13 | use PHP_CodeSniffer\Sniffs\Sniff;
|
14 | 14 | use PHP_CodeSniffer\Util\Tokens;
|
15 | 15 |
|
16 |
| -class UpperCaseConstantSniff implements Sniff |
| 16 | +class UpperCaseConstantSniff extends LowerCaseConstantSniff |
17 | 17 | {
|
18 | 18 |
|
19 |
| - /** |
20 |
| - * The tokens this sniff is targetting. |
21 |
| - * |
22 |
| - * @var array |
23 |
| - */ |
24 |
| - private $targets = [ |
25 |
| - T_TRUE => T_TRUE, |
26 |
| - T_FALSE => T_FALSE, |
27 |
| - T_NULL => T_NULL, |
28 |
| - ]; |
29 |
| - |
30 |
| - |
31 |
| - /** |
32 |
| - * Returns an array of tokens this test wants to listen for. |
33 |
| - * |
34 |
| - * @return array |
35 |
| - */ |
36 |
| - public function register() |
37 |
| - { |
38 |
| - $targets = $this->targets; |
39 |
| - |
40 |
| - // Register function keywords to filter out type declarations. |
41 |
| - $targets[] = T_FUNCTION; |
42 |
| - $targets[] = T_CLOSURE; |
43 |
| - $targets[] = T_FN; |
44 |
| - |
45 |
| - return $targets; |
46 |
| - |
47 |
| - }//end register() |
48 |
| - |
49 |
| - |
50 |
| - /** |
51 |
| - * Processes this sniff, when one of its tokens is encountered. |
52 |
| - * |
53 |
| - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
54 |
| - * @param int $stackPtr The position of the current token in the |
55 |
| - * stack passed in $tokens. |
56 |
| - * |
57 |
| - * @return void |
58 |
| - */ |
59 |
| - public function process(File $phpcsFile, $stackPtr) |
60 |
| - { |
61 |
| - $tokens = $phpcsFile->getTokens(); |
62 |
| - |
63 |
| - // Handle function declarations separately as they may contain the keywords in type declarations. |
64 |
| - if ($tokens[$stackPtr]['code'] === T_FUNCTION |
65 |
| - || $tokens[$stackPtr]['code'] === T_CLOSURE |
66 |
| - || $tokens[$stackPtr]['code'] === T_FN |
67 |
| - ) { |
68 |
| - if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) { |
69 |
| - return; |
70 |
| - } |
71 |
| - |
72 |
| - $end = $tokens[$stackPtr]['parenthesis_closer']; |
73 |
| - if (isset($tokens[$stackPtr]['scope_opener']) === true) { |
74 |
| - $end = $tokens[$stackPtr]['scope_opener']; |
75 |
| - } |
76 |
| - |
77 |
| - // Do a quick check if any of the targets exist in the declaration. |
78 |
| - $found = $phpcsFile->findNext($this->targets, $tokens[$stackPtr]['parenthesis_opener'], $end); |
79 |
| - if ($found === false) { |
80 |
| - // Skip forward, no need to examine these tokens again. |
81 |
| - return $end; |
82 |
| - } |
83 |
| - |
84 |
| - // Handle the whole function declaration in one go. |
85 |
| - $params = $phpcsFile->getMethodParameters($stackPtr); |
86 |
| - foreach ($params as $param) { |
87 |
| - if (isset($param['default_token']) === false) { |
88 |
| - continue; |
89 |
| - } |
90 |
| - |
91 |
| - $paramEnd = $param['comma_token']; |
92 |
| - if ($param['comma_token'] === false) { |
93 |
| - $paramEnd = $tokens[$stackPtr]['parenthesis_closer']; |
94 |
| - } |
95 |
| - |
96 |
| - for ($i = $param['default_token']; $i < $paramEnd; $i++) { |
97 |
| - if (isset($this->targets[$tokens[$i]['code']]) === true) { |
98 |
| - $this->processConstant($phpcsFile, $i); |
99 |
| - } |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - // Skip over return type declarations. |
104 |
| - return $end; |
105 |
| - }//end if |
106 |
| - |
107 |
| - // Handle property declarations separately as they may contain the keywords in type declarations. |
108 |
| - if (isset($tokens[$stackPtr]['conditions']) === true) { |
109 |
| - $conditions = $tokens[$stackPtr]['conditions']; |
110 |
| - $lastCondition = end($conditions); |
111 |
| - if (isset(Tokens::$ooScopeTokens[$lastCondition]) === true) { |
112 |
| - // This can only be an OO constant or property declaration as methods are handled above. |
113 |
| - $equals = $phpcsFile->findPrevious(T_EQUAL, ($stackPtr - 1), null, false, null, true); |
114 |
| - if ($equals !== false) { |
115 |
| - $this->processConstant($phpcsFile, $stackPtr); |
116 |
| - } |
117 |
| - |
118 |
| - return; |
119 |
| - } |
120 |
| - } |
121 |
| - |
122 |
| - // Handle everything else. |
123 |
| - $this->processConstant($phpcsFile, $stackPtr); |
124 |
| - |
125 |
| - }//end process() |
126 |
| - |
127 | 19 |
|
128 | 20 | /**
|
129 | 21 | * Processes a non-type declaration constant.
|
|
0 commit comments