Skip to content

Commit 4af1311

Browse files
committed
Generic UpperCaseConstantNameSniff no longer reports errors where constants are used (request #20090)
1 parent 2c884a0 commit 4af1311

File tree

3 files changed

+48
-152
lines changed

3 files changed

+48
-152
lines changed

CodeSniffer/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php

Lines changed: 41 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -91,153 +91,63 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9191
true
9292
);
9393

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) {
11595
return;
11696
}
11797

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.
19399
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';
195101
$data = array(
196102
strtoupper($constName),
197103
$constName,
198104
);
199-
$phpcsFile->addError($error, $stackPtr, 'ConstantNotUpperCase', $data);
105+
$phpcsFile->addError($error, $stackPtr, 'ClassConstantNotUpperCase', $data);
200106
}
201107

202-
} else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') {
108+
return;
109+
}
203110

204-
/*
205-
This may be a "define" or "constant" function call.
206-
*/
111+
if (strtolower($constName) !== 'define') {
112+
return;
113+
}
207114

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+
*/
215118

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+
}
221126

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+
}
223132

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'];
231134

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+
}
241151

242152
}//end process()
243153

CodeSniffer/Standards/Generic/Tests/NamingConventions/UpperCaseConstantNameUnitTest.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,11 @@ class Generic_Tests_NamingConventions_UpperCaseConstantNameUnitTest extends Abst
4242
*/
4343
public function getErrorList()
4444
{
45-
$errors = array(
46-
8 => 1,
47-
10 => 1,
48-
15 => 1,
49-
25 => 1,
50-
26 => 1,
51-
27 => 1,
52-
28 => 1,
53-
29 => 1,
54-
32 => 1,
55-
35 => 1,
56-
100 => 1,
57-
);
58-
59-
// The trait insteadof test will only work in PHP version where traits exist
60-
// and will throw errors in earlier versions.
61-
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
62-
$errors[131] = 3;
63-
}
64-
65-
return $errors;
45+
return array(
46+
8 => 1,
47+
10 => 1,
48+
15 => 1,
49+
);
6650

6751
}//end getErrorList()
6852

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3030
-- E.g., to ignore comments, override a property using:
3131
-- name="ignoreIndentationTokens" type="array" value="T_COMMENT,T_DOC_COMMENT"
3232
- PSR2 standard now ignores comments when checking indentation rules
33+
- Generic UpperCaseConstantNameSniff no longer reports errors where constants are used (request #20090)
34+
-- It still reports errors where constants are defined
3335
- Fixed bug #20093 : Bug with ternary operator token
3436
- Fixed bug #20097 : CLI.php throws error in php 5.2
3537
- Fixed bug #20100 : incorrect Function mysql() has been deprecated report

0 commit comments

Comments
 (0)