Conditions | 14 |
Paths | 105 |
Total Lines | 59 |
Code Lines | 42 |
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 |
||
43 | public static function main($task) |
||
44 | { |
||
45 | $methodName = 'main'; |
||
46 | $dispatchable = null; |
||
47 | |||
48 | try { |
||
49 | if ($task instanceof Dispatchable) { |
||
50 | $dispatchable = $task; |
||
51 | } elseif ($task instanceof UnknownElement) { |
||
52 | $ue = $task; |
||
53 | $realThing = $ue->getRealThing(); |
||
54 | if (null != $realThing && $realThing instanceof Dispatchable && $realThing instanceof Task) { |
||
55 | $dispatchable = $realThing; |
||
56 | } |
||
57 | } |
||
58 | if (null != $dispatchable) { |
||
59 | $mName = null; |
||
|
|||
60 | |||
61 | $name = trim($dispatchable->getActionParameterName()); |
||
62 | if (empty($name)) { |
||
63 | throw new BuildException( |
||
64 | 'Action Parameter Name must not be empty for Dispatchable Task.' |
||
65 | ); |
||
66 | } |
||
67 | $mName = 'get' . ucfirst($name); |
||
68 | |||
69 | try { |
||
70 | $c = new ReflectionClass($dispatchable); |
||
71 | $actionM = $c->getMethod($mName); |
||
72 | $o = $actionM->invoke($dispatchable); |
||
73 | $methodName = trim((string) $o); |
||
74 | if (empty($methodName)) { |
||
75 | throw new ReflectionException(); |
||
76 | } |
||
77 | } catch (ReflectionException $re) { |
||
78 | throw new BuildException( |
||
79 | "Dispatchable Task attribute '" . $name . "' not set or value is empty." |
||
80 | ); |
||
81 | } |
||
82 | $executeM = $c->getMethod($methodName); |
||
83 | $executeM->invoke($dispatchable); |
||
84 | |||
85 | if ($task instanceof UnknownElement) { |
||
86 | $task->setRealThing(null); |
||
87 | } |
||
88 | } else { |
||
89 | try { |
||
90 | $refl = new ReflectionClass($task); |
||
91 | $executeM = $refl->getMethod($methodName); |
||
92 | } catch (ReflectionException $re) { |
||
93 | throw new BuildException('No public ' . $methodName . '() in ' . get_class($task)); |
||
94 | } |
||
95 | $executeM->invoke($task); |
||
96 | if ($task instanceof UnknownElement) { |
||
97 | $task->setRealThing(null); |
||
98 | } |
||
99 | } |
||
100 | } catch (ReflectionException $e) { |
||
101 | throw new BuildException($e); |
||
102 | } |
||
105 |