@@ -91,153 +91,63 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
91
91
true
92
92
);
93
93
94
- $ declarations = array (
95
- T_FUNCTION ,
96
- T_CLASS ,
97
- T_INTERFACE ,
98
- T_TRAIT ,
99
- T_IMPLEMENTS ,
100
- T_EXTENDS ,
101
- T_INSTANCEOF ,
102
- T_NEW ,
103
- T_NAMESPACE ,
104
- T_USE ,
105
- T_AS ,
106
- T_GOTO ,
107
- T_INSTEADOF ,
108
- T_PUBLIC ,
109
- T_PRIVATE ,
110
- T_PROTECTED ,
111
- );
112
-
113
- if (in_array ($ tokens [$ functionKeyword ]['code ' ], $ declarations ) === true ) {
114
- // This is just a declaration; no constants here.
94
+ if ($ tokens [$ functionKeyword ]['code ' ] !== T_CONST ) {
115
95
return ;
116
96
}
117
97
118
- if ($ tokens [$ functionKeyword ]['code ' ] === T_CONST ) {
119
- // This is a class constant.
120
- if (strtoupper ($ constName ) !== $ constName ) {
121
- $ error = 'Class constants must be uppercase; expected %s but found %s ' ;
122
- $ data = array (
123
- strtoupper ($ constName ),
124
- $ constName ,
125
- );
126
- $ phpcsFile ->addError ($ error , $ stackPtr , 'ClassConstantNotUpperCase ' , $ data );
127
- }
128
-
129
- return ;
130
- }
131
-
132
- // Is this a class name?
133
- $ nextPtr = $ phpcsFile ->findNext (T_WHITESPACE , ($ stackPtr + 1 ), null , true );
134
- if ($ tokens [$ nextPtr ]['code ' ] === T_DOUBLE_COLON ) {
135
- return ;
136
- }
137
-
138
- // Is this a namespace name?
139
- if ($ tokens [$ nextPtr ]['code ' ] === T_NS_SEPARATOR ) {
140
- return ;
141
- }
142
-
143
- // Is this an insteadof name?
144
- if ($ tokens [$ nextPtr ]['code ' ] === T_INSTEADOF ) {
145
- return ;
146
- }
147
-
148
- // Is this an as name?
149
- if ($ tokens [$ nextPtr ]['code ' ] === T_AS ) {
150
- return ;
151
- }
152
-
153
- // Is this a type hint?
154
- if ($ tokens [$ nextPtr ]['code ' ] === T_VARIABLE
155
- || $ phpcsFile ->isReference ($ nextPtr ) === true
156
- ) {
157
- return ;
158
- }
159
-
160
- // Is this a member var name?
161
- $ prevPtr = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
162
- if ($ tokens [$ prevPtr ]['code ' ] === T_OBJECT_OPERATOR ) {
163
- return ;
164
- }
165
-
166
- // Is this a variable name, in the form ${varname} ?
167
- if ($ tokens [$ prevPtr ]['code ' ] === T_OPEN_CURLY_BRACKET ) {
168
- $ nextPtr = $ phpcsFile ->findNext (T_WHITESPACE , ($ stackPtr + 1 ), null , true );
169
- if ($ tokens [$ nextPtr ]['code ' ] === T_CLOSE_CURLY_BRACKET ) {
170
- return ;
171
- }
172
- }
173
-
174
- // Is this a namespace name?
175
- if ($ tokens [$ prevPtr ]['code ' ] === T_NS_SEPARATOR ) {
176
- return ;
177
- }
178
-
179
- // Is this an instance of declare()
180
- $ prevPtrDeclare = $ phpcsFile ->findPrevious (array (T_WHITESPACE , T_OPEN_PARENTHESIS ), ($ stackPtr - 1 ), null , true );
181
- if ($ tokens [$ prevPtrDeclare ]['code ' ] === T_DECLARE ) {
182
- return ;
183
- }
184
-
185
- // Is this a goto label target?
186
- if ($ tokens [$ nextPtr ]['code ' ] === T_COLON ) {
187
- if (in_array ($ tokens [$ prevPtr ]['code ' ], array (T_SEMICOLON , T_OPEN_CURLY_BRACKET , T_COLON ), true )) {
188
- return ;
189
- }
190
- }
191
-
192
- // This is a real constant.
98
+ // This is a class constant.
193
99
if (strtoupper ($ constName ) !== $ constName ) {
194
- $ error = 'Constants must be uppercase; expected %s but found %s ' ;
100
+ $ error = 'Class constants must be uppercase; expected %s but found %s ' ;
195
101
$ data = array (
196
102
strtoupper ($ constName ),
197
103
$ constName ,
198
104
);
199
- $ phpcsFile ->addError ($ error , $ stackPtr , 'ConstantNotUpperCase ' , $ data );
105
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'ClassConstantNotUpperCase ' , $ data );
200
106
}
201
107
202
- } else if (strtolower ($ constName ) === 'define ' || strtolower ($ constName ) === 'constant ' ) {
108
+ return ;
109
+ }
203
110
204
- /*
205
- This may be a "define" or "constant" function call.
206
- */
111
+ if ( strtolower ( $ constName ) !== ' define ' ) {
112
+ return ;
113
+ }
207
114
208
- // Make sure this is not a method call.
209
- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
210
- if ($ tokens [$ prev ]['code ' ] === T_OBJECT_OPERATOR
211
- || $ tokens [$ prev ]['code ' ] === T_DOUBLE_COLON
212
- ) {
213
- return ;
214
- }
115
+ /*
116
+ This may be a "define" function call.
117
+ */
215
118
216
- // The next non-whitespace token must be the constant name.
217
- $ constPtr = $ phpcsFile ->findNext (T_WHITESPACE , ($ openBracket + 1 ), null , true );
218
- if ($ tokens [$ constPtr ]['code ' ] !== T_CONSTANT_ENCAPSED_STRING ) {
219
- return ;
220
- }
119
+ // Make sure this is not a method call.
120
+ $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
121
+ if ($ tokens [$ prev ]['code ' ] === T_OBJECT_OPERATOR
122
+ || $ tokens [$ prev ]['code ' ] === T_DOUBLE_COLON
123
+ ) {
124
+ return ;
125
+ }
221
126
222
- $ constName = $ tokens [$ constPtr ]['content ' ];
127
+ // The next non-whitespace token must be the constant name.
128
+ $ constPtr = $ phpcsFile ->findNext (T_WHITESPACE , ($ openBracket + 1 ), null , true );
129
+ if ($ tokens [$ constPtr ]['code ' ] !== T_CONSTANT_ENCAPSED_STRING ) {
130
+ return ;
131
+ }
223
132
224
- // Check for constants like self::CONSTANT.
225
- $ prefix = '' ;
226
- $ splitPos = strpos ($ constName , ':: ' );
227
- if ($ splitPos !== false ) {
228
- $ prefix = substr ($ constName , 0 , ($ splitPos + 2 ));
229
- $ constName = substr ($ constName , ($ splitPos + 2 ));
230
- }
133
+ $ constName = $ tokens [$ constPtr ]['content ' ];
231
134
232
- if (strtoupper ($ constName ) !== $ constName ) {
233
- $ error = 'Constants must be uppercase; expected %s but found %s ' ;
234
- $ data = array (
235
- $ prefix .strtoupper ($ constName ),
236
- $ prefix .$ constName ,
237
- );
238
- $ phpcsFile ->addError ($ error , $ stackPtr , 'ConstantNotUpperCase ' , $ data );
239
- }
240
- }//end if
135
+ // Check for constants like self::CONSTANT.
136
+ $ prefix = '' ;
137
+ $ splitPos = strpos ($ constName , ':: ' );
138
+ if ($ splitPos !== false ) {
139
+ $ prefix = substr ($ constName , 0 , ($ splitPos + 2 ));
140
+ $ constName = substr ($ constName , ($ splitPos + 2 ));
141
+ }
142
+
143
+ if (strtoupper ($ constName ) !== $ constName ) {
144
+ $ error = 'Constants must be uppercase; expected %s but found %s ' ;
145
+ $ data = array (
146
+ $ prefix .strtoupper ($ constName ),
147
+ $ prefix .$ constName ,
148
+ );
149
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'ConstantNotUpperCase ' , $ data );
150
+ }
241
151
242
152
}//end process()
243
153
0 commit comments