Conditions | 15 |
Paths | 105 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 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 /** MicroContainer */ |
||
153 | public function loadComponent($name, $options) |
||
154 | { |
||
155 | if (empty($options['class']) || !class_exists($options['class'])) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | $className = $options['class']; |
||
160 | $this->data[$name] = null; |
||
161 | |||
162 | $options['arguments'] = !empty($options['arguments']) ? $this->buildParams($options['arguments']) : null; |
||
163 | $options['property'] = !empty($options['property']) ? $this->buildParams($options['property']) : null; |
||
164 | $options['calls'] = !empty($options['calls']) ? $this->buildCalls($options['calls']) : null; |
||
165 | |||
166 | try { // create object |
||
167 | $reflection = new \ReflectionClass($className); |
||
168 | $reflectionMethod = new \ReflectionMethod($className, '__construct'); |
||
169 | |||
170 | if ($reflectionMethod->getNumberOfParameters() === 0) { |
||
171 | $this->data[$name] = new $className; |
||
172 | } else { |
||
173 | $this->data[$name] = $reflection->newInstanceArgs($options['arguments']); |
||
174 | } |
||
175 | |||
176 | unset($reflection, $reflectionMethod); |
||
177 | } catch (Exception $e) { |
||
178 | return false; |
||
179 | } |
||
180 | |||
181 | if (!empty($options['property'])) { // load properties |
||
182 | foreach ($options['property'] as $property => $value) { |
||
183 | if (property_exists($this->data[$name], $property)) { |
||
184 | $this->data[$name]->$property = $value; |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if (!empty($options['calls'])) { // run methods |
||
190 | foreach ($options['calls'] as $method => $arguments) { |
||
191 | if (method_exists($this->data['name'], $method)) { |
||
192 | $reflectionMethod = new \ReflectionMethod($className, $method); |
||
193 | if ($reflectionMethod->getNumberOfParameters() === 0) { |
||
194 | $this->data['name']->$method(); |
||
195 | } else { |
||
196 | call_user_func_array([$this->data['name'], $method], $arguments); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return true; |
||
203 | } |
||
204 | |||
261 |