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 |
||
57 | class Container implements IntrospectableContainerInterface { |
||
58 | |||
59 | /** |
||
60 | * The parameters of the container. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $parameters = array(); |
||
65 | |||
66 | /** |
||
67 | * The aliases of the container. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $aliases = array(); |
||
72 | |||
73 | /** |
||
74 | * The service definitions of the container. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $serviceDefinitions = array(); |
||
79 | |||
80 | /** |
||
81 | * The instantiated services. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $services = array(); |
||
86 | |||
87 | /** |
||
88 | * The instantiated private services. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $privateServices = array(); |
||
93 | |||
94 | /** |
||
95 | * The currently loading services. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $loading = array(); |
||
100 | |||
101 | /** |
||
102 | * Whether the container parameters can still be changed. |
||
103 | * |
||
104 | * For testing purposes the container needs to be changed. |
||
105 | * |
||
106 | * @var bool |
||
107 | */ |
||
108 | protected $frozen = TRUE; |
||
109 | |||
110 | /** |
||
111 | * Constructs a new Container instance. |
||
112 | * |
||
113 | * @param array $container_definition |
||
114 | * An array containing the following keys: |
||
115 | * - aliases: The aliases of the container. |
||
116 | * - parameters: The parameters of the container. |
||
117 | * - services: The service definitions of the container. |
||
118 | * - frozen: Whether the container definition came from a frozen |
||
119 | * container builder or not. |
||
120 | * - machine_format: Whether this container definition uses the optimized |
||
121 | * machine-readable container format. |
||
122 | */ |
||
123 | View Code Duplication | public function __construct(array $container_definition = array()) { |
|
124 | if (!empty($container_definition) && (!isset($container_definition['machine_format']) || $container_definition['machine_format'] !== TRUE)) { |
||
125 | throw new InvalidArgumentException('The non-optimized format is not supported by this class. Use an optimized machine-readable format instead, e.g. as produced by \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper.'); |
||
126 | } |
||
127 | |||
128 | $this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : array(); |
||
129 | $this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : array(); |
||
130 | $this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : array(); |
||
131 | $this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE; |
||
132 | |||
133 | // Register the service_container with itself. |
||
134 | $this->services['service_container'] = $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { |
||
141 | if (isset($this->aliases[$id])) { |
||
142 | $id = $this->aliases[$id]; |
||
143 | } |
||
144 | |||
145 | // Re-use shared service instance if it exists. |
||
146 | View Code Duplication | if (isset($this->services[$id]) || ($invalid_behavior === ContainerInterface::NULL_ON_INVALID_REFERENCE && array_key_exists($id, $this->services))) { |
|
|
|||
147 | return $this->services[$id]; |
||
148 | } |
||
149 | |||
150 | if (isset($this->loading[$id])) { |
||
151 | throw new ServiceCircularReferenceException($id, array_keys($this->loading)); |
||
152 | } |
||
153 | |||
154 | $definition = isset($this->serviceDefinitions[$id]) ? $this->serviceDefinitions[$id] : NULL; |
||
155 | |||
156 | if (!$definition && $invalid_behavior === ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { |
||
157 | if (!$id) { |
||
158 | throw new ServiceNotFoundException($id); |
||
159 | } |
||
160 | |||
161 | throw new ServiceNotFoundException($id, NULL, NULL, $this->getServiceAlternatives($id)); |
||
162 | } |
||
163 | |||
164 | // In case something else than ContainerInterface::NULL_ON_INVALID_REFERENCE |
||
165 | // is used, the actual wanted behavior is to re-try getting the service at a |
||
166 | // later point. |
||
167 | if (!$definition) { |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | // Definition is a keyed array, so [0] is only defined when it is a |
||
172 | // serialized string. |
||
173 | if (isset($definition[0])) { |
||
174 | $definition = unserialize($definition); |
||
175 | } |
||
176 | |||
177 | // Now create the service. |
||
178 | $this->loading[$id] = TRUE; |
||
179 | |||
180 | try { |
||
181 | $service = $this->createService($definition, $id); |
||
182 | } |
||
183 | catch (\Exception $e) { |
||
184 | unset($this->loading[$id]); |
||
185 | |||
186 | // Remove a potentially shared service that was constructed incompletely. |
||
187 | if (array_key_exists($id, $this->services)) { |
||
188 | unset($this->services[$id]); |
||
189 | } |
||
190 | |||
191 | if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | throw $e; |
||
196 | } |
||
197 | |||
198 | unset($this->loading[$id]); |
||
199 | |||
200 | return $service; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Creates a service from a service definition. |
||
205 | * |
||
206 | * @param array $definition |
||
207 | * The service definition to create a service from. |
||
208 | * @param string $id |
||
209 | * The service identifier, necessary so it can be shared if its public. |
||
210 | * |
||
211 | * @return object |
||
212 | * The service described by the service definition. |
||
213 | * |
||
214 | * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException |
||
215 | * Thrown when the service is a synthetic service. |
||
216 | * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
||
217 | * Thrown when the configurator callable in $definition['configurator'] is |
||
218 | * not actually a callable. |
||
219 | * @throws \ReflectionException |
||
220 | * Thrown when the service class takes more than 10 parameters to construct, |
||
221 | * and cannot be instantiated. |
||
222 | */ |
||
223 | protected function createService(array $definition, $id) { |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function set($id, $service, $scope = ContainerInterface::SCOPE_CONTAINER) { |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | public function has($id) { |
||
369 | return isset($this->aliases[$id]) || isset($this->services[$id]) || isset($this->serviceDefinitions[$id]) || array_key_exists($id, $this->services); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function getParameter($name) { |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | public function hasParameter($name) { |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function setParameter($name, $value) { |
||
398 | if ($this->frozen) { |
||
399 | throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); |
||
400 | } |
||
401 | |||
402 | $this->parameters[$name] = $value; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function initialized($id) { |
||
409 | if (isset($this->aliases[$id])) { |
||
410 | $id = $this->aliases[$id]; |
||
411 | } |
||
412 | |||
413 | return isset($this->services[$id]) || array_key_exists($id, $this->services); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Resolves arguments that represent services or variables to the real values. |
||
418 | * |
||
419 | * @param array|\stdClass $arguments |
||
420 | * The arguments to resolve. |
||
421 | * |
||
422 | * @return array |
||
423 | * The resolved arguments. |
||
424 | * |
||
425 | * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException |
||
426 | * If a parameter/service could not be resolved. |
||
427 | * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
||
428 | * If an unknown type is met while resolving parameters and services. |
||
429 | */ |
||
430 | protected function resolveServicesAndParameters($arguments) { |
||
431 | // Check if this collection needs to be resolved. |
||
432 | if ($arguments instanceof \stdClass) { |
||
433 | if ($arguments->type !== 'collection') { |
||
434 | throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $arguments->type)); |
||
435 | } |
||
436 | // In case there is nothing to resolve, we are done here. |
||
437 | if (!$arguments->resolve) { |
||
438 | return $arguments->value; |
||
439 | } |
||
440 | $arguments = $arguments->value; |
||
441 | } |
||
442 | |||
443 | // Process the arguments. |
||
444 | foreach ($arguments as $key => $argument) { |
||
445 | // For this machine-optimized format, only \stdClass arguments are |
||
446 | // processed and resolved. All other values are kept as is. |
||
447 | if ($argument instanceof \stdClass) { |
||
448 | $type = $argument->type; |
||
449 | |||
450 | // Check for parameter. |
||
451 | if ($type == 'parameter') { |
||
452 | $name = $argument->name; |
||
453 | if (!isset($this->parameters[$name])) { |
||
454 | $arguments[$key] = $this->getParameter($name); |
||
455 | // This can never be reached as getParameter() throws an Exception, |
||
456 | // because we already checked that the parameter is not set above. |
||
457 | } |
||
458 | |||
459 | // Update argument. |
||
460 | $argument = $arguments[$key] = $this->parameters[$name]; |
||
461 | |||
462 | // In case there is not a machine readable value (e.g. a service) |
||
463 | // behind this resolved parameter, continue. |
||
464 | if (!($argument instanceof \stdClass)) { |
||
465 | continue; |
||
466 | } |
||
467 | |||
468 | // Fall through. |
||
469 | $type = $argument->type; |
||
470 | } |
||
471 | |||
472 | // Create a service. |
||
473 | if ($type == 'service') { |
||
474 | $id = $argument->id; |
||
475 | |||
476 | // Does the service already exist? |
||
477 | if (isset($this->aliases[$id])) { |
||
478 | $id = $this->aliases[$id]; |
||
479 | } |
||
480 | |||
481 | if (isset($this->services[$id])) { |
||
482 | $arguments[$key] = $this->services[$id]; |
||
483 | continue; |
||
484 | } |
||
485 | |||
486 | // Return the service. |
||
487 | $arguments[$key] = $this->get($id, $argument->invalidBehavior); |
||
488 | |||
489 | continue; |
||
490 | } |
||
491 | // Create private service. |
||
492 | elseif ($type == 'private_service') { |
||
493 | $id = $argument->id; |
||
494 | |||
495 | // Does the private service already exist. |
||
496 | if (isset($this->privateServices[$id])) { |
||
497 | $arguments[$key] = $this->privateServices[$id]; |
||
498 | continue; |
||
499 | } |
||
500 | |||
501 | // Create the private service. |
||
502 | $arguments[$key] = $this->createService($argument->value, $id); |
||
503 | if ($argument->shared) { |
||
504 | $this->privateServices[$id] = $arguments[$key]; |
||
505 | } |
||
506 | |||
507 | continue; |
||
508 | } |
||
509 | // Check for collection. |
||
510 | elseif ($type == 'collection') { |
||
511 | $value = $argument->value; |
||
512 | |||
513 | // Does this collection need resolving? |
||
514 | if ($argument->resolve) { |
||
515 | $arguments[$key] = $this->resolveServicesAndParameters($value); |
||
516 | } |
||
517 | else { |
||
518 | $arguments[$key] = $value; |
||
519 | } |
||
520 | |||
521 | continue; |
||
522 | } |
||
523 | |||
524 | if ($type !== NULL) { |
||
525 | throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type)); |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | |||
530 | return $arguments; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Provides alternatives for a given array and key. |
||
535 | * |
||
536 | * @param string $search_key |
||
537 | * The search key to get alternatives for. |
||
538 | * @param array $keys |
||
539 | * The search space to search for alternatives in. |
||
540 | * |
||
541 | * @return string[] |
||
542 | * An array of strings with suitable alternatives. |
||
543 | */ |
||
544 | protected function getAlternatives($search_key, array $keys) { |
||
555 | |||
556 | /** |
||
557 | * Provides alternatives in case a service was not found. |
||
558 | * |
||
559 | * @param string $id |
||
560 | * The service to get alternatives for. |
||
561 | * |
||
562 | * @return string[] |
||
563 | * An array of strings with suitable alternatives. |
||
564 | */ |
||
565 | protected function getServiceAlternatives($id) { |
||
569 | |||
570 | /** |
||
571 | * Provides alternatives in case a parameter was not found. |
||
572 | * |
||
573 | * @param string $name |
||
574 | * The parameter to get alternatives for. |
||
575 | * |
||
576 | * @return string[] |
||
577 | * An array of strings with suitable alternatives. |
||
578 | */ |
||
579 | protected function getParameterAlternatives($name) { |
||
582 | |||
583 | |||
584 | /** |
||
585 | * {@inheritdoc} |
||
586 | */ |
||
587 | public function enterScope($name) { |
||
590 | |||
591 | /** |
||
592 | * {@inheritdoc} |
||
593 | */ |
||
594 | public function leaveScope($name) { |
||
597 | |||
598 | /** |
||
599 | * {@inheritdoc} |
||
600 | */ |
||
601 | public function addScope(ScopeInterface $scope) { |
||
604 | |||
605 | /** |
||
606 | * {@inheritdoc} |
||
607 | */ |
||
608 | public function hasScope($name) { |
||
611 | |||
612 | /** |
||
613 | * {@inheritdoc} |
||
614 | */ |
||
615 | public function isScopeActive($name) { |
||
618 | |||
619 | /** |
||
620 | * Gets all defined service IDs. |
||
621 | * |
||
622 | * @return array |
||
623 | * An array of all defined service IDs. |
||
624 | */ |
||
625 | public function getServiceIds() { |
||
628 | |||
629 | } |
||
630 |
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.