Conditions | 12 |
Paths | 24 |
Total Lines | 62 |
Code Lines | 33 |
Lines | 7 |
Ratio | 11.29 % |
Changes | 4 | ||
Bugs | 1 | Features | 2 |
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 |
||
147 | protected function produceDependency($alias, $fallback = '_michaels_no_fallback') |
||
148 | { |
||
149 | /* Get the registered factory (string, closure, object, container, NoItemFoundMessage) */ |
||
150 | $factory = $this->getIfExists($this->nameOfIocManifest . ".$alias"); |
||
151 | |||
152 | /* Manage not founds and fallback */ |
||
153 | View Code Duplication | if ($factory instanceof NoItemFoundMessage) { |
|
154 | if ($fallback !== '_michaels_no_fallback') { |
||
155 | return $fallback; |
||
156 | } else { |
||
157 | throw new ItemNotFoundException("$alias not found"); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /* Get any declared dependencies */ |
||
162 | $declared = $this->getIfExists($this->nameOfIocManifest . "._declarations.$alias"); |
||
163 | $dependencies = []; |
||
164 | |||
165 | // Now setup those dependencies into an array |
||
166 | if (!$declared instanceof NoItemFoundMessage) { |
||
167 | $dependencies = array_map(function(&$value) use ($alias) { |
||
168 | if (is_string($value) && $this->exists($this->nameOfIocManifest . ".$alias")) { |
||
169 | return $this->fetch($value); |
||
170 | } |
||
171 | return $value; |
||
172 | }, $declared); |
||
173 | } |
||
174 | |||
175 | /* Produce the object itself */ |
||
176 | if ($factory instanceof IocContainerInterface) { |
||
177 | $object = $factory->fetch($alias); |
||
178 | |||
179 | } elseif (is_string($factory)) { |
||
180 | $class = new \ReflectionClass($factory); |
||
181 | $object = $class->newInstanceArgs($dependencies); |
||
182 | |||
183 | } elseif (is_callable($factory)) { |
||
184 | array_unshift($dependencies, $this); |
||
185 | $object = call_user_func_array($factory, $dependencies); |
||
186 | |||
187 | } elseif (is_object($factory)) { |
||
188 | $object = $factory; |
||
189 | |||
190 | if (method_exists($object, "needs")) { |
||
191 | call_user_func_array([$object, 'needs'], $dependencies); |
||
192 | } |
||
193 | |||
194 | } else { |
||
195 | throw new \Exception("`fetch()` can only return from strings, callables, or objects"); |
||
196 | } |
||
197 | |||
198 | /* Run the object through the pipeline, if desired */ |
||
199 | $pipeline = $this->getIfExists($this->nameOfIocManifest . "._pipelines.$alias"); |
||
200 | |||
201 | if (!$pipeline instanceof NoItemFoundMessage) { |
||
202 | /** @var \Closure $pipeline */ |
||
203 | $object = $pipeline($object, $this); |
||
204 | } |
||
205 | |||
206 | /* Return the final object */ |
||
207 | return $object; |
||
208 | } |
||
209 | } |
||
210 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: