5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: UpperCaseConstantNameSniff.php,v 1.10 2008/05/07 23:13:49 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff.
|
|
|
19 |
*
|
|
|
20 |
* Ensures that constant names are all uppercase.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.0RC1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Returns an array of tokens this test wants to listen for.
|
|
|
37 |
*
|
|
|
38 |
* @return array
|
|
|
39 |
*/
|
|
|
40 |
public function register()
|
|
|
41 |
{
|
|
|
42 |
return array(T_STRING);
|
|
|
43 |
|
|
|
44 |
}//end register()
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Processes this test, when one of its tokens is encountered.
|
|
|
49 |
*
|
|
|
50 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
51 |
* @param int $stackPtr The position of the current token in the
|
|
|
52 |
* stack passed in $tokens.
|
|
|
53 |
*
|
|
|
54 |
* @return void
|
|
|
55 |
*/
|
|
|
56 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
57 |
{
|
|
|
58 |
$tokens = $phpcsFile->getTokens();
|
|
|
59 |
$constName = $tokens[$stackPtr]['content'];
|
|
|
60 |
|
|
|
61 |
// If this token is in a heredoc, ignore it.
|
|
|
62 |
if ($phpcsFile->hasCondition($stackPtr, T_START_HEREDOC) === true) {
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// If the next non-whitespace token after this token
|
|
|
67 |
// is not an opening parenthesis then it is not a function call.
|
|
|
68 |
$openBracket = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
|
|
|
69 |
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
|
|
|
70 |
$functionKeyword = $phpcsFile->findPrevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING), ($stackPtr - 1), null, true);
|
|
|
71 |
|
|
|
72 |
$declarations = array(
|
|
|
73 |
T_FUNCTION,
|
|
|
74 |
T_CLASS,
|
|
|
75 |
T_INTERFACE,
|
|
|
76 |
T_IMPLEMENTS,
|
|
|
77 |
T_EXTENDS,
|
|
|
78 |
T_INSTANCEOF,
|
|
|
79 |
T_NEW,
|
|
|
80 |
);
|
|
|
81 |
if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) {
|
|
|
82 |
// This is just a declaration; no constants here.
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if ($tokens[$functionKeyword]['code'] === T_CONST) {
|
|
|
87 |
// This is a class constant.
|
|
|
88 |
if (strtoupper($constName) !== $constName) {
|
|
|
89 |
$error = 'Class constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
|
|
|
90 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
return;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Is this a class name?
|
|
|
97 |
$nextPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
|
|
|
98 |
if ($tokens[$nextPtr]['code'] === T_DOUBLE_COLON) {
|
|
|
99 |
return;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Is this a type hint?
|
|
|
103 |
if ($tokens[$nextPtr]['code'] === T_VARIABLE) {
|
|
|
104 |
return;
|
|
|
105 |
} else if ($phpcsFile->isReference($nextPtr) === true) {
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// Is this a member var name?
|
|
|
110 |
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
|
|
|
111 |
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
|
|
|
112 |
return;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// Is this an instance of declare()
|
|
|
116 |
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackPtr - 1), null, true);
|
|
|
117 |
if ($tokens[$prevPtr]['code'] === T_DECLARE) {
|
|
|
118 |
return;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// This is a real constant.
|
|
|
122 |
if (strtoupper($constName) !== $constName) {
|
|
|
123 |
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
|
|
|
124 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
} else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') {
|
|
|
128 |
|
|
|
129 |
/*
|
|
|
130 |
This may be a "define" or "constant" function call.
|
|
|
131 |
*/
|
|
|
132 |
|
|
|
133 |
// The next non-whitespace token must be the constant name.
|
|
|
134 |
$constPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($openBracket + 1), null, true);
|
|
|
135 |
if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
|
|
|
136 |
return;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
$constName = $tokens[$constPtr]['content'];
|
|
|
140 |
if (strtoupper($constName) !== $constName) {
|
|
|
141 |
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName";
|
|
|
142 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
143 |
}
|
|
|
144 |
}//end if
|
|
|
145 |
|
|
|
146 |
}//end process()
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
}//end class
|
|
|
150 |
|
|
|
151 |
?>
|