Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class Container extends Component implements ContainerInterface, FactoryInterface, ResolverInterface |
||
34 | { |
||
35 | /** |
||
36 | * Parent container if any. |
||
37 | * |
||
38 | * @var ContainerInterface |
||
39 | */ |
||
40 | private $parent = null; |
||
41 | |||
42 | /** |
||
43 | * IoC bindings. |
||
44 | * |
||
45 | * @invisible |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $bindings = []; |
||
49 | |||
50 | /** |
||
51 | * Registered injectors. |
||
52 | * |
||
53 | * @invisible |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $injectors = []; |
||
57 | |||
58 | /** |
||
59 | * @todo see has() |
||
60 | * @param ContainerInterface $parent |
||
61 | */ |
||
62 | public function __construct(ContainerInterface $parent = null) |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function has($alias) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | * |
||
83 | * Context parameter will be passed to class injectors, which makes possible to use this method |
||
84 | * as: |
||
85 | * $this->container->get(DatabaseInterface::class, 'default'); |
||
86 | * |
||
87 | * @param string|null $context Call context. |
||
88 | */ |
||
89 | public function get($alias, $context = null) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | * |
||
103 | * @param string|null $context Related to parameter caused injection if any. |
||
104 | */ |
||
105 | public function make($class, $parameters = [], $context = null) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function resolveArguments(ContextFunction $reflection, array $parameters = []) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | View Code Duplication | public function bind($alias, $resolver) |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | View Code Duplication | public function bindSingleton($alias, $resolver) |
|
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | * |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function bindInjector($class, $injector) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function replace($alias, $resolver) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function restore($replacePayload) |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function hasInstance($alias) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function removeBinding($alias) |
||
329 | |||
330 | /** |
||
331 | * Every declared Container binding. Must not be used in production code due container format is |
||
332 | * vary. |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | public function getBindings() |
||
340 | |||
341 | /** |
||
342 | * Every binded injector. |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | public function getInjectors() |
||
350 | |||
351 | /** |
||
352 | * Automatically create class. |
||
353 | * |
||
354 | * @param string $class |
||
355 | * @param array $parameters |
||
356 | * @param string $context |
||
357 | * @return object |
||
358 | * @throws AutowireException |
||
359 | */ |
||
360 | protected function autowire($class, array $parameters, $context) |
||
386 | |||
387 | /** |
||
388 | * Check if given class has associated injector. |
||
389 | * |
||
390 | * @param \ReflectionClass $reflection |
||
391 | * @return bool |
||
392 | */ |
||
393 | protected function hasInjector(\ReflectionClass $reflection) |
||
401 | |||
402 | /** |
||
403 | * Get injector associated with given class. |
||
404 | * |
||
405 | * @param \ReflectionClass $reflection |
||
406 | * @return InjectorInterface |
||
407 | */ |
||
408 | protected function getInjector(\ReflectionClass $reflection) |
||
416 | |||
417 | /** |
||
418 | * Create instance of desired class. |
||
419 | * |
||
420 | * @param string $class |
||
421 | * @param array $parameters Constructor parameters. |
||
422 | * @param string|null $context |
||
423 | * @param \ReflectionClass $reflection Instance of reflection associated with class, |
||
424 | * reference. |
||
425 | * @return object |
||
426 | * @throws ContainerException |
||
427 | */ |
||
428 | private function createInstance( |
||
475 | } |
||
476 |