Conditions | 15 |
Paths | 32 |
Total Lines | 66 |
Code Lines | 37 |
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 |
||
144 | public function resolveDependencies(RecipeInterface $recipe, \ReflectionClass $reflector, $arguments = array()) |
||
145 | { |
||
146 | // if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
||
147 | // else wrap it in an additional array so that it doesn't get split into multiple parameters |
||
148 | $arguments = $this->array_helper->is_array_numerically_and_sequentially_indexed($arguments) |
||
149 | ? $arguments |
||
150 | : array($arguments); |
||
151 | $resolved_parameters = array(); |
||
152 | // let's examine the constructor |
||
153 | // let's examine the constructor |
||
154 | $constructor = $this->getConstructor($reflector); |
||
155 | // whu? huh? nothing? |
||
156 | if ( ! $constructor) { |
||
157 | return $arguments; |
||
158 | } |
||
159 | // get constructor parameters |
||
160 | $params = $this->getParameters($constructor); |
||
161 | if (empty($params)) { |
||
162 | return $resolved_parameters; |
||
163 | } |
||
164 | $ingredients = $recipe->ingredients(); |
||
165 | // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
||
166 | $argument_keys = array_keys($arguments); |
||
167 | // now loop thru all of the constructors expected parameters |
||
168 | foreach ($params as $index => $param) { |
||
169 | if ( ! $param instanceof \ReflectionParameter) { |
||
170 | continue; |
||
171 | } |
||
172 | // is this a dependency for a specific class ? |
||
173 | $param_class = $param->getClass() ? $param->getClass()->name : null; |
||
174 | if ( |
||
175 | // param is specified in the list of ingredients for this Recipe |
||
176 | isset($ingredients[$param_class]) |
||
177 | ) { |
||
178 | // attempt to inject the dependency |
||
179 | $resolved_parameters[$index] = $this->injectDependency($ingredients[$param_class]); |
||
180 | } else if ( |
||
181 | // param is not even a class |
||
182 | empty($param_class) |
||
183 | // and something already exists in the incoming arguments for this param |
||
184 | && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
||
185 | ) { |
||
186 | // add parameter from incoming arguments |
||
187 | $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
||
188 | } else if ( |
||
189 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
||
190 | ! empty($param_class) |
||
191 | && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
||
192 | && $arguments[$argument_keys[$index]] instanceof $param_class |
||
193 | ) { |
||
194 | // add parameter from incoming arguments |
||
195 | $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
||
196 | } else if ( |
||
197 | // parameter is type hinted as a class, and should be injected |
||
198 | ! empty($param_class) |
||
199 | ) { |
||
200 | // attempt to inject the dependency |
||
201 | $resolved_parameters[$index] = $this->injectDependency($param_class); |
||
202 | } else if ($param->isOptional()) { |
||
203 | $resolved_parameters[$index] = $param->getDefaultValue(); |
||
204 | } else { |
||
205 | $resolved_parameters[$index] = null; |
||
206 | } |
||
207 | } |
||
208 | return $resolved_parameters; |
||
209 | } |
||
210 | |||
236 | // Location: /DependencyInjector.php |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.