Conditions | 9 |
Paths | 15 |
Total Lines | 52 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
75 | private function callback(array $matches) |
||
76 | { |
||
77 | $places = explode('.', $this->prefix.$matches[1]); |
||
78 | |||
79 | $c = $this->data; |
||
80 | $processedPath = []; |
||
81 | foreach ($places as $place) |
||
82 | { |
||
83 | $processedPath[] = $place; |
||
84 | |||
85 | if (is_object($c)) |
||
86 | { |
||
87 | $reflection = new \ReflectionClass(get_class($c)); |
||
88 | $methodName = 'get'.$this->camelizer->camelize($place); |
||
89 | |||
90 | try |
||
91 | { |
||
92 | $property = $reflection->getProperty($place); |
||
93 | } |
||
94 | catch (\Exception $e) |
||
95 | { |
||
96 | $property = null; |
||
97 | } |
||
98 | |||
99 | |||
100 | if ($property && $property->isPublic()) |
||
101 | { |
||
102 | $c = $c->{$place}; |
||
103 | } |
||
104 | else if ($reflection->hasMethod($methodName)) |
||
105 | { |
||
106 | $c = call_user_func(array($c, $methodName)); |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | $message = "Class %s has not property or getter for: %s. Full path: %s"; |
||
111 | throw new \Exception(sprintf($message, get_class($c), $place, join('.', $places))); |
||
112 | } |
||
113 | } |
||
114 | else if (is_array($c)) |
||
115 | { |
||
116 | $c = $c[$place]; |
||
117 | } |
||
118 | |||
119 | if (is_null($c)) |
||
120 | { |
||
121 | throw new \Exception('Null value for: '.join('.', $processedPath)); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | return $c; |
||
126 | } |
||
127 | } |
||
128 |