Conditions | 13 |
Paths | 25 |
Total Lines | 64 |
Code Lines | 35 |
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 | protected function produceDependency($alias, $fallback = '_michaels_no_fallback') |
||
145 | { |
||
146 | /* Get the registered factory (string, closure, object, container, NoItemFoundMessage) */ |
||
147 | $factory = $this->getIfExists("$alias"); |
||
148 | |||
149 | /* Manage not founds and fallback */ |
||
150 | if ($factory instanceof NoItemFoundMessage) { |
||
151 | if ($fallback !== '_michaels_no_fallback') { |
||
152 | return $fallback; |
||
153 | } elseif (class_exists($alias)) { |
||
154 | return new $alias; |
||
155 | } else { |
||
156 | throw new ItemNotFoundException("$alias not found"); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /* Get any declared dependencies */ |
||
161 | $declared = $this->getIfExists("_declarations.$alias"); |
||
162 | $dependencies = []; |
||
163 | |||
164 | // Now setup those dependencies into an array |
||
165 | if (!$declared instanceof NoItemFoundMessage) { |
||
166 | $dependencies = array_map(function(&$value) use ($alias) { |
||
167 | if (is_string($value) && $this->exists("$alias")) { |
||
168 | return $this->get($value); |
||
169 | } |
||
170 | return $value; |
||
171 | }, $declared); |
||
172 | } |
||
173 | |||
174 | /* Produce the object itself */ |
||
175 | if ($factory instanceof IocContainerInterface) { |
||
176 | $object = $factory->get($alias); |
||
177 | |||
178 | } elseif (is_string($factory)) { |
||
179 | $class = new \ReflectionClass($factory); |
||
180 | $object = $class->newInstanceArgs($dependencies); |
||
181 | |||
182 | } elseif (is_callable($factory)) { |
||
183 | array_unshift($dependencies, $this); |
||
184 | $object = call_user_func_array($factory, $dependencies); |
||
185 | |||
186 | } elseif (is_object($factory)) { |
||
187 | $object = $factory; |
||
188 | |||
189 | if (method_exists($object, "needs")) { |
||
190 | call_user_func_array([$object, 'needs'], $dependencies); |
||
191 | } |
||
192 | |||
193 | } else { |
||
194 | throw new \Exception("`get()` can only return from strings, callables, or objects"); |
||
195 | } |
||
196 | |||
197 | /* Run the object through the pipeline, if desired */ |
||
198 | $pipeline = $this->getIfExists("_pipelines.$alias"); |
||
199 | |||
200 | if (!$pipeline instanceof NoItemFoundMessage) { |
||
201 | /** @var \Closure $pipeline */ |
||
202 | $object = $pipeline($object, $this); |
||
203 | } |
||
204 | |||
205 | /* Return the final object */ |
||
206 | return $object; |
||
207 | } |
||
208 | } |
||
209 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.