Conditions | 16 |
Paths | 27 |
Total Lines | 66 |
Lines | 6 |
Ratio | 9.09 % |
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 |
||
23 | public static function expendReferences($classes, $imports) |
||
24 | { |
||
25 | // here we implode the tokens to form the full namespaced class path |
||
26 | $results = []; |
||
27 | $namespace = ''; |
||
28 | $c = 0; |
||
29 | foreach ($classes as $importeds) { |
||
30 | if ($importeds[0][0] == T_NAMESPACE) { |
||
31 | unset($importeds[0]); |
||
32 | foreach ($importeds as $row) { |
||
33 | $namespace .= $row[1]; |
||
34 | } |
||
35 | continue; |
||
36 | } |
||
37 | |||
38 | $results[$c]['class'] = ''; |
||
39 | |||
40 | // attach the current namespace if it does not begin with '\' |
||
41 | if ($importeds[0][1][0] != '\\') { |
||
42 | $results[$c]['class'] = $namespace ? $namespace.'\\' : ''; |
||
43 | } |
||
44 | |||
45 | foreach ($importeds as $row) { |
||
46 | if (self::isBuiltinType($row[1])) { |
||
47 | unset($results[$c]); |
||
48 | continue; |
||
49 | } |
||
50 | |||
51 | // if starts with "\" or is not imported by the "use" |
||
52 | if ($importeds[0][1][0] != '\\' && Str::contains($importeds[0][1], '\\')) { |
||
53 | // for php 8.x |
||
54 | $tmp = explode('\\', $importeds[0][1]); |
||
55 | } else { |
||
56 | $tmp = [$importeds[0][1]]; |
||
57 | } |
||
58 | |||
59 | if ($importeds[0][1] == '\\' || ! isset(array_values($imports)[0][$tmp[0]][0])) { |
||
60 | $results[$c]['class'] .= $row[1]; |
||
61 | $results[$c]['line'] = $row[2]; |
||
62 | continue; |
||
63 | } |
||
64 | |||
65 | // reads the import from the top |
||
66 | $results[$c]['class'] = array_values($imports)[0][$tmp[0]][0]; |
||
67 | |||
68 | // for half imported references |
||
69 | if ($importeds[0][1][0] != '\\' && Str::contains($importeds[0][1], '\\')) { |
||
70 | // for php 8.x |
||
71 | $tmp = explode('\\', $importeds[0][1]); |
||
72 | View Code Duplication | for ($j = 1; $j < count($tmp); $j++) { |
|
73 | $results[$c]['class'] .= '\\'.$tmp[$j]; |
||
74 | } |
||
75 | } else { |
||
76 | // for php 7.x |
||
77 | View Code Duplication | for ($j = 1; $j < count($importeds); $j++) { |
|
78 | $results[$c]['class'] .= $importeds[$j][1]; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $results[$c]['line'] = $row[2]; |
||
83 | } |
||
84 | $c++; |
||
85 | } |
||
86 | |||
87 | return [$results, $namespace]; |
||
88 | } |
||
89 | } |
||
90 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: