Conditions | 11 |
Paths | 21 |
Total Lines | 69 |
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 |
||
7 | public static function read($tokens) |
||
8 | { |
||
9 | $i = 0; |
||
10 | $class = [ |
||
11 | 'name' => null, |
||
12 | 'methods' => [], |
||
13 | 'type' => '', |
||
14 | ]; |
||
15 | $methods = []; |
||
16 | |||
17 | while (isset($tokens[$i])) { |
||
18 | $token = $tokens[$i]; |
||
19 | |||
20 | if ($token[0] == T_CLASS && $tokens[$i - 1][0] !== T_DOUBLE_COLON) { |
||
21 | $class['name'] = $tokens[$i + 2]; |
||
22 | $class['type'] = T_CLASS; |
||
23 | $class['is_abstract'] = ($tokens[$i - 2][0] === T_ABSTRACT); |
||
24 | } elseif ($token[0] == T_INTERFACE) { |
||
25 | $class['name'] = $tokens[$i + 2]; |
||
26 | $class['type'] = T_INTERFACE; |
||
27 | } elseif ($token[0] == T_TRAIT) { |
||
28 | $class['name'] = $tokens[$i + 2]; |
||
29 | $class['type'] = T_TRAIT; |
||
30 | } |
||
31 | |||
32 | if ($class['name'] === null || $tokens[$i][0] != T_FUNCTION) { |
||
33 | $i++; |
||
34 | continue; |
||
35 | } |
||
36 | |||
37 | if (! \is_array($name = $tokens[$i + 2])) { |
||
38 | $i++; |
||
39 | continue; |
||
40 | } |
||
41 | |||
42 | [$visibility, $isStatic, $isAbstract] = self::findVisibility($tokens, $i - 2); |
||
43 | [, $signature, $endSignature] = Ifs::readCondition($tokens, $i + 2); |
||
44 | [$char, $charIndex] = TokenManager::forwardTo($tokens, $endSignature, [':', ';', '{']); |
||
45 | |||
46 | [$returnType, $hasNullableReturnType, $char, $charIndex] = self::processReturnType($char, $tokens, $charIndex); |
||
47 | |||
48 | if ($char == '{') { |
||
49 | [$body, $i] = TokenManager::readBody($tokens, $charIndex); |
||
50 | } elseif ($char == ';') { |
||
51 | $body = []; |
||
52 | } else { |
||
53 | $code = Refactor::toString($tokens); |
||
54 | self::requestIssue($code); |
||
55 | break; |
||
56 | } |
||
57 | |||
58 | $i++; |
||
59 | $methods[] = [ |
||
60 | 'name' => $name, |
||
61 | 'visibility' => $visibility, |
||
62 | 'signature' => $signature, |
||
63 | 'body' => Refactor::toString($body), |
||
64 | 'startBodyIndex' => [$charIndex, $i], |
||
65 | 'returnType' => $returnType, |
||
66 | 'nullable_return_type' => $hasNullableReturnType, |
||
67 | 'is_static' => $isStatic, |
||
68 | 'is_abstract' => $isAbstract, |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | $class['methods'] = $methods; |
||
73 | |||
74 | return $class; |
||
75 | } |
||
76 | |||
130 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.