Conditions | 32 |
Paths | 139 |
Total Lines | 126 |
Code Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
243 | }//end process() |
||
244 | |||
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: