Conditions | 39 |
Paths | 39 |
Total Lines | 143 |
Lines | 7 |
Ratio | 4.9 % |
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 |
||
192 | public function processNonString(File $phpcsFile, $stackPtr, $tokens) |
||
193 | { |
||
194 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
195 | if ($nextNonEmpty === false) { |
||
196 | return; |
||
197 | } |
||
198 | |||
199 | /* |
||
200 | * Deal with anonymous classes - `class` before a reserved keyword is sometimes |
||
201 | * misidentified as `T_ANON_CLASS`. |
||
202 | * In PHPCS < 2.3.4 these were tokenized as T_CLASS no matter what. |
||
203 | */ |
||
204 | if ($tokens[$stackPtr]['type'] === 'T_ANON_CLASS' || $tokens[$stackPtr]['type'] === 'T_CLASS') { |
||
205 | $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
206 | if ($prevNonEmpty !== false && $tokens[$prevNonEmpty]['type'] === 'T_NEW') { |
||
207 | return; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /* |
||
212 | * PHP 5.6 allows for use const and use function, but only if followed by the function/constant name. |
||
213 | * - `use function HelloWorld` => move to the next token (HelloWorld) to verify. |
||
214 | * - `use const HelloWorld` => move to the next token (HelloWorld) to verify. |
||
215 | */ |
||
216 | elseif ($tokens[$stackPtr]['type'] === 'T_USE' |
||
217 | && isset($this->validUseNames[strtolower($tokens[$nextNonEmpty]['content'])]) === true |
||
218 | ) { |
||
219 | $maybeUseNext = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
220 | if ($maybeUseNext !== false && $this->isEndOfUseStatement($tokens[$maybeUseNext]) === false) { |
||
221 | $nextNonEmpty = $maybeUseNext; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /* |
||
226 | * Deal with visibility modifiers. |
||
227 | * - `use HelloWorld { sayHello as protected; }` => valid, bow out. |
||
228 | * - `use HelloWorld { sayHello as private myPrivateHello; }` => move to the next token to verify. |
||
229 | */ |
||
230 | elseif ($tokens[$stackPtr]['type'] === 'T_AS' |
||
231 | && isset($this->allowedModifiers[$tokens[$nextNonEmpty]['code']]) === true |
||
232 | && $phpcsFile->hasCondition($stackPtr, \T_USE) === true |
||
233 | ) { |
||
234 | $maybeUseNext = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
235 | if ($maybeUseNext === false || $this->isEndOfUseStatement($tokens[$maybeUseNext]) === true) { |
||
236 | return; |
||
237 | } |
||
238 | |||
239 | $nextNonEmpty = $maybeUseNext; |
||
240 | } |
||
241 | |||
242 | /* |
||
243 | * Deal with foreach ( ... as list() ). |
||
244 | */ |
||
245 | elseif ($tokens[$stackPtr]['type'] === 'T_AS' |
||
246 | && isset($tokens[$stackPtr]['nested_parenthesis']) === true |
||
247 | && $tokens[$nextNonEmpty]['code'] === \T_LIST |
||
248 | ) { |
||
249 | $parentheses = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true); |
||
250 | View Code Duplication | foreach ($parentheses as $open => $close) { |
|
|
|||
251 | if (isset($tokens[$open]['parenthesis_owner']) |
||
252 | && $tokens[$tokens[$open]['parenthesis_owner']]['code'] === \T_FOREACH |
||
253 | ) { |
||
254 | return; |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | /* |
||
260 | * Deal with functions declared to return by reference. |
||
261 | */ |
||
262 | elseif ($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
263 | && $tokens[$nextNonEmpty]['type'] === 'T_BITWISE_AND' |
||
264 | ) { |
||
265 | $maybeUseNext = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
266 | if ($maybeUseNext === false) { |
||
267 | // Live coding. |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | $nextNonEmpty = $maybeUseNext; |
||
272 | } |
||
273 | |||
274 | /* |
||
275 | * Deal with nested namespaces. |
||
276 | */ |
||
277 | elseif ($tokens[$stackPtr]['type'] === 'T_NAMESPACE') { |
||
278 | if ($tokens[$stackPtr + 1]['code'] === \T_NS_SEPARATOR) { |
||
279 | // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
||
280 | return; |
||
281 | } |
||
282 | |||
283 | $endToken = $phpcsFile->findNext(array(\T_SEMICOLON, \T_OPEN_CURLY_BRACKET), ($stackPtr + 1), null, false, null, true); |
||
284 | $namespaceName = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($endToken - $stackPtr - 1))); |
||
285 | if (empty($namespaceName) === true) { |
||
286 | return; |
||
287 | } |
||
288 | |||
289 | $namespaceParts = explode('\\', $namespaceName); |
||
290 | foreach ($namespaceParts as $namespacePart) { |
||
291 | $partLc = strtolower($namespacePart); |
||
292 | if (isset($this->invalidNames[$partLc]) === false) { |
||
293 | continue; |
||
294 | } |
||
295 | |||
296 | // Find the token position of the part which matched. |
||
297 | for ($i = ($stackPtr + 1); $i < $endToken; $i++) { |
||
298 | if ($tokens[$i]['content'] === $namespacePart) { |
||
299 | $nextNonEmpty = $i; |
||
300 | break; |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | unset($i, $namespacePart, $partLc); |
||
305 | } |
||
306 | |||
307 | $nextContentLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
308 | if (isset($this->invalidNames[$nextContentLc]) === false) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | /* |
||
313 | * Deal with PHP 7 relaxing the rules. |
||
314 | * "As of PHP 7.0.0 these keywords are allowed as property, constant, and method names |
||
315 | * of classes, interfaces and traits, except that class may not be used as constant name." |
||
316 | */ |
||
317 | if ((($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
318 | && $this->inClassScope($phpcsFile, $stackPtr, false) === true) |
||
319 | || ($tokens[$stackPtr]['type'] === 'T_CONST' |
||
320 | && $this->isClassConstant($phpcsFile, $stackPtr) === true |
||
321 | && $nextContentLc !== 'class')) |
||
322 | && $this->supportsBelow('5.6') === false |
||
323 | ) { |
||
324 | return; |
||
325 | } |
||
326 | |||
327 | if ($this->supportsAbove($this->invalidNames[$nextContentLc])) { |
||
328 | $data = array( |
||
329 | $tokens[$nextNonEmpty]['content'], |
||
330 | $this->invalidNames[$nextContentLc], |
||
331 | ); |
||
332 | $this->addError($phpcsFile, $stackPtr, $tokens[$nextNonEmpty]['content'], $data); |
||
333 | } |
||
334 | } |
||
335 | |||
417 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.