1 | <?php |
||
26 | * @category PHP |
||
27 | * @package PHPCompatibility |
||
28 | * @author Juliette Reinders Folmer <[email protected]> |
||
29 | */ |
||
30 | class ForbiddenNamesAsDeclaredSniff extends Sniff |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * List of tokens which can not be used as class, interface, trait names or as part of a namespace. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $forbiddenTokens = array( |
||
39 | T_NULL => '7.0', |
||
40 | T_TRUE => '7.0', |
||
41 | T_FALSE => '7.0', |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * T_STRING keywords to recognize as forbidden names. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $forbiddenNames = array( |
||
50 | 'null' => '7.0', |
||
51 | 'true' => '7.0', |
||
52 | 'false' => '7.0', |
||
53 | 'bool' => '7.0', |
||
54 | 'int' => '7.0', |
||
55 | 'float' => '7.0', |
||
56 | 'string' => '7.0', |
||
57 | 'iterable' => '7.1', |
||
58 | 'void' => '7.1', |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * T_STRING keywords to recognize as soft reserved names. |
||
63 | * |
||
64 | * Using any of these keywords to name a class, interface, trait or namespace |
||
65 | * is highly discouraged since they may be used in future versions of PHP. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $softReservedNames = array( |
||
70 | 'resource' => '7.0', |
||
71 | 'object' => '7.0', |
||
72 | 'mixed' => '7.0', |
||
73 | 'numeric' => '7.0', |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Combined list of the two lists above. |
||
78 | * |
||
79 | * Used for quick check whether or not something is a reserved |
||
80 | * word. |
||
81 | * Set from the `register()` method. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | private $allForbiddenNames = array(); |
||
86 | |||
87 | |||
88 | /** |
||
89 | * Returns an array of tokens this test wants to listen for. |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | public function register() |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Processes this test, when one of its tokens is encountered. |
||
111 | * |
||
112 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
113 | * @param int $stackPtr The position of the current token in the |
||
114 | * stack passed in $tokens. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
119 | { |
||
120 | if ($this->supportsAbove('7.0') === false) { |
||
121 | return; |
||
122 | } |
||
123 | |||
124 | $tokens = $phpcsFile->getTokens(); |
||
125 | $tokenCode = $tokens[$stackPtr]['code']; |
||
126 | $tokenContentLc = strtolower($tokens[$stackPtr]['content']); |
||
127 | |||
128 | // For string tokens we only care about 'trait' and 'namespace' as those are |
||
129 | // the only ones which may not be correctly recognized as it's own token. |
||
130 | // This only happens in older versions of PHP where the token doesn't exist yet as a keyword. |
||
131 | if ($tokenCode === T_STRING && ($tokenContentLc !== 'trait' && $tokenContentLc !== 'namespace')) { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | if (in_array($tokenCode, array(T_CLASS, T_INTERFACE, T_TRAIT), true)) { |
||
136 | // Check for the declared name being a name which is not tokenized as T_STRING. |
||
137 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
138 | if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
||
139 | $name = $tokens[$nextNonEmpty]['content']; |
||
140 | } else { |
||
141 | // Get the declared name if it's a T_STRING. |
||
142 | $name = $phpcsFile->getDeclarationName($stackPtr); |
||
143 | } |
||
144 | unset($nextNonEmpty); |
||
145 | |||
146 | if (isset($name) === false || is_string($name) === false || $name === '') { |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | $nameLc = strtolower($name); |
||
151 | if (isset($this->allForbiddenNames[$nameLc]) === false) { |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | } elseif ($tokenCode === T_NAMESPACE) { |
||
156 | $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr); |
||
157 | |||
158 | if ($namespaceName === false || $namespaceName === '') { |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | $namespaceParts = explode('\\', $namespaceName); |
||
163 | foreach ($namespaceParts as $namespacePart) { |
||
164 | $partLc = strtolower($namespacePart); |
||
165 | if (isset($this->allForbiddenNames[$partLc]) === true) { |
||
166 | $name = $namespacePart; |
||
167 | $nameLc = $partLc; |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | } elseif ($tokenCode === T_STRING) { |
||
172 | // Traits and namespaces which are not yet tokenized as T_TRAIT/T_NAMESPACE. |
||
173 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
174 | if ($nextNonEmpty === false) { |
||
175 | return; |
||
176 | } |
||
177 | |||
178 | $nextNonEmptyCode = $tokens[$nextNonEmpty]['code']; |
||
179 | |||
180 | if ($nextNonEmptyCode !== T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) { |
||
181 | $name = $tokens[$nextNonEmpty]['content']; |
||
182 | $nameLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
183 | } elseif ($nextNonEmptyCode === T_STRING) { |
||
184 | $endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1)); |
||
185 | if ($endOfStatement === false) { |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | do { |
||
190 | $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
191 | |||
192 | if (isset($this->allForbiddenNames[$nextNonEmptyLc]) === true) { |
||
193 | $name = $tokens[$nextNonEmpty]['content']; |
||
194 | $nameLc = $nextNonEmptyLc; |
||
195 | break; |
||
196 | } |
||
197 | |||
198 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true); |
||
199 | } while ($nextNonEmpty !== false); |
||
200 | } |
||
201 | unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement); |
||
202 | } |
||
203 | |||
204 | if (isset($name, $nameLc) === false) { |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | // Still here, so this is one of the reserved words. |
||
209 | // Build up the error message. |
||
210 | $error = "'%s' is a"; |
||
211 | $isError = null; |
||
212 | $errorCode = $this->stringToErrorCode($nameLc).'Found'; |
||
|
|||
213 | $data = array( |
||
214 | $nameLc, |
||
215 | ); |
||
216 | |||
217 | if (isset($this->softReservedNames[$nameLc]) === true |
||
218 | && $this->supportsAbove($this->softReservedNames[$nameLc]) === true |
||
219 | ) { |
||
220 | $error .= ' soft reserved keyword as of PHP version %s'; |
||
221 | $isError = false; |
||
222 | $data[] = $this->softReservedNames[$nameLc]; |
||
223 | } |
||
224 | |||
225 | if (isset($this->forbiddenNames[$nameLc]) === true |
||
226 | && $this->supportsAbove($this->forbiddenNames[$nameLc]) === true |
||
227 | ) { |
||
228 | if (isset($isError) === true) { |
||
229 | $error .= ' and a'; |
||
230 | } |
||
231 | $error .= ' reserved keyword as of PHP version %s'; |
||
232 | $isError = true; |
||
233 | $data[] = $this->forbiddenNames[$nameLc]; |
||
234 | } |
||
235 | |||
236 | if (isset($isError) === true) { |
||
237 | $error .= ' and should not be used to name a class, interface or trait or as part of a namespace (%s)'; |
||
238 | $data[] = $tokens[$stackPtr]['type']; |
||
239 | |||
240 | $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data); |
||
241 | } |
||
242 | |||
246 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: