Conditions | 15 |
Paths | 105 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 $key => $val) { |
||
183 | if (property_exists($this->data[$name], $key)) { |
||
184 | $this->data[$name]->$key = $val; |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if (!empty($options['calls'])) { // run methods |
||
190 | foreach ($options['calls'] as $key => $val) { |
||
191 | if (method_exists($this->data['name'], $key)) { |
||
192 | $reflectionMethod = new \ReflectionMethod($className, $key); |
||
193 | if ($reflectionMethod->getNumberOfParameters() === 0) { |
||
194 | $this->data['name']->$key(); |
||
195 | } else { |
||
196 | call_user_func_array([$this->data['name'], $key], $val); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return true; |
||
203 | } |
||
204 | |||
267 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.