Conditions | 31 |
Paths | 139 |
Total Lines | 127 |
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 |
||
125 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
126 | { |
||
127 | if ($this->supportsAbove('7.0') === false) { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | $tokens = $phpcsFile->getTokens(); |
||
132 | $tokenCode = $tokens[$stackPtr]['code']; |
||
133 | $tokenType = $tokens[$stackPtr]['type']; |
||
134 | $tokenContentLc = strtolower($tokens[$stackPtr]['content']); |
||
135 | |||
136 | // For string tokens we only care about 'trait' as that is the only one |
||
137 | // which may not be correctly recognized as it's own token. |
||
138 | // This only happens in older versions of PHP where the token doesn't exist yet as a keyword. |
||
139 | if ($tokenCode === T_STRING && $tokenContentLc !== 'trait') { |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | if (in_array($tokenType, array('T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) { |
||
144 | // Check for the declared name being a name which is not tokenized as T_STRING. |
||
145 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
146 | if ($nextNonEmpty !== false && isset($this->forbiddenTokens[$tokens[$nextNonEmpty]['code']]) === true) { |
||
147 | $name = $tokens[$nextNonEmpty]['content']; |
||
148 | } else { |
||
149 | // Get the declared name if it's a T_STRING. |
||
150 | $name = $phpcsFile->getDeclarationName($stackPtr); |
||
151 | } |
||
152 | unset($nextNonEmpty); |
||
153 | |||
154 | if (isset($name) === false || is_string($name) === false || $name === '') { |
||
155 | return; |
||
156 | } |
||
157 | |||
158 | $nameLc = strtolower($name); |
||
159 | if (isset($this->allForbiddenNames[$nameLc]) === false) { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | } elseif ($tokenCode === T_NAMESPACE) { |
||
164 | $namespaceName = $this->getDeclaredNamespaceName($phpcsFile, $stackPtr); |
||
165 | |||
166 | if ($namespaceName === false || $namespaceName === '') { |
||
167 | return; |
||
168 | } |
||
169 | |||
170 | $namespaceParts = explode('\\', $namespaceName); |
||
171 | foreach ($namespaceParts as $namespacePart) { |
||
172 | $partLc = strtolower($namespacePart); |
||
173 | if (isset($this->allForbiddenNames[$partLc]) === true) { |
||
174 | $name = $namespacePart; |
||
175 | $nameLc = $partLc; |
||
176 | break; |
||
177 | } |
||
178 | } |
||
179 | } elseif ($tokenCode === T_STRING) { |
||
180 | // Traits which are not yet tokenized as T_TRAIT. |
||
181 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
182 | if ($nextNonEmpty === false) { |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | $nextNonEmptyCode = $tokens[$nextNonEmpty]['code']; |
||
187 | |||
188 | if ($nextNonEmptyCode !== T_STRING && isset($this->forbiddenTokens[$nextNonEmptyCode]) === true) { |
||
189 | $name = $tokens[$nextNonEmpty]['content']; |
||
190 | $nameLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
191 | } elseif ($nextNonEmptyCode === T_STRING) { |
||
192 | $endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1)); |
||
193 | if ($endOfStatement === false) { |
||
194 | return; |
||
195 | } |
||
196 | |||
197 | do { |
||
198 | $nextNonEmptyLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
199 | |||
200 | if (isset($this->allForbiddenNames[$nextNonEmptyLc]) === true) { |
||
201 | $name = $tokens[$nextNonEmpty]['content']; |
||
202 | $nameLc = $nextNonEmptyLc; |
||
203 | break; |
||
204 | } |
||
205 | |||
206 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), $endOfStatement, true); |
||
207 | } while ($nextNonEmpty !== false); |
||
208 | } |
||
209 | unset($nextNonEmptyCode, $nextNonEmptyLc, $endOfStatement); |
||
210 | } |
||
211 | |||
212 | if (isset($name, $nameLc) === false) { |
||
213 | return; |
||
214 | } |
||
215 | |||
216 | // Still here, so this is one of the reserved words. |
||
217 | // Build up the error message. |
||
218 | $error = "'%s' is a"; |
||
219 | $isError = null; |
||
220 | $errorCode = $this->stringToErrorCode($nameLc).'Found'; |
||
|
|||
221 | $data = array( |
||
222 | $nameLc, |
||
223 | ); |
||
224 | |||
225 | if (isset($this->softReservedNames[$nameLc]) === true |
||
226 | && $this->supportsAbove($this->softReservedNames[$nameLc]) === true |
||
227 | ) { |
||
228 | $error .= ' soft reserved keyword as of PHP version %s'; |
||
229 | $isError = false; |
||
230 | $data[] = $this->softReservedNames[$nameLc]; |
||
231 | } |
||
232 | |||
233 | if (isset($this->forbiddenNames[$nameLc]) === true |
||
234 | && $this->supportsAbove($this->forbiddenNames[$nameLc]) === true |
||
235 | ) { |
||
236 | if (isset($isError) === true) { |
||
237 | $error .= ' and a'; |
||
238 | } |
||
239 | $error .= ' reserved keyword as of PHP version %s'; |
||
240 | $isError = true; |
||
241 | $data[] = $this->forbiddenNames[$nameLc]; |
||
242 | } |
||
243 | |||
244 | if (isset($isError) === true) { |
||
245 | $error .= ' and should not be used to name a class, interface or trait or as part of a namespace (%s)'; |
||
246 | $data[] = $tokens[$stackPtr]['type']; |
||
247 | |||
248 | $this->addMessage($phpcsFile, $error, $stackPtr, $isError, $errorCode, $data); |
||
249 | } |
||
250 | |||
251 | }//end process() |
||
252 | |||
254 |
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: