Conditions | 11 |
Paths | 80 |
Total Lines | 59 |
Code Lines | 39 |
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 |
||
38 | public function buildExtensionCode(string $functionName) : string |
||
39 | { |
||
40 | $returnTypeCode = ''; |
||
41 | $returnType = $this->reflectionMethod->getReturnType(); |
||
42 | if ($returnType) { |
||
43 | $allowsNull = $returnType->allowsNull() ? '?':''; |
||
44 | if ($returnType->isBuiltin()) { |
||
45 | $returnTypeCode = ': '.$allowsNull.$returnType; |
||
46 | } else { |
||
47 | $returnTypeCode = ': '.$allowsNull.'\\'.$returnType; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | $parameters = $this->reflectionMethod->getParameters(); |
||
52 | |||
53 | $previousParameterCode = ''; |
||
54 | $previousTypeCode = ''; |
||
55 | if (count($parameters) >= 1) { |
||
56 | $previousParameter = $this->reflectionMethod->getParameters()[0]; |
||
57 | $previousType = $previousParameter->getType(); |
||
58 | if ($previousType) { |
||
59 | if ($previousType->isBuiltin()) { |
||
60 | $previousTypeCode = (string) $previousType.' '; |
||
61 | } else { |
||
62 | $previousTypeCode = '\\'.(string) $previousType.' '; |
||
63 | } |
||
64 | if ($previousParameter->allowsNull()) { |
||
65 | $previousTypeCode = '?'.$previousTypeCode; |
||
66 | } |
||
67 | } |
||
68 | $previousParameterCode = ', '.$previousTypeCode.'$previous'; |
||
69 | if ($previousParameter->isDefaultValueAvailable()) { |
||
70 | $previousParameterCode .= ' = '.var_export($previousParameter->getDefaultValue(), true); |
||
71 | } elseif ($previousParameter->allowsNull()) { |
||
72 | // If a first argument has no default null value but is nullable (because of ?), |
||
73 | // we still put the null default value. |
||
74 | $previousParameterCode .= ' = null'; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | |||
79 | return sprintf( |
||
80 | <<<EOF |
||
81 | public static function %s(ContainerInterface \$container%s)%s |
||
82 | { |
||
83 | return %s::%s(%s%s); |
||
84 | } |
||
85 | |||
86 | EOF |
||
87 | , |
||
88 | $functionName, |
||
89 | $previousParameterCode, |
||
90 | $returnTypeCode, |
||
91 | '\\'.$this->reflectionMethod->getDeclaringClass()->getName(), |
||
92 | $this->reflectionMethod->getName(), |
||
93 | $previousParameterCode ? '$previous': '', |
||
94 | implode('', array_map(function (Injection $injection) { |
||
95 | return ', '.$injection->getCode(); |
||
96 | }, $this->getInjections())) |
||
97 | ); |
||
112 |