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 declare(strict_types=1); |
||
| 18 | class Container implements ContainerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var DefinitionAggregate |
||
| 22 | */ |
||
| 23 | protected $definitionAggregate; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $scalars = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Resolved shared instances |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $shared = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var InflectionInterface[] |
||
| 39 | */ |
||
| 40 | protected $inflections = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Delegate[] |
||
| 44 | */ |
||
| 45 | protected $delegates = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Now loading services |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $loading = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $autoWire = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var BuilderInterface |
||
| 60 | */ |
||
| 61 | protected $builder; |
||
| 62 | |||
| 63 | 22 | public function __construct( |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritDoc} |
||
| 81 | */ |
||
| 82 | 1 | public function addDefinitionClass(string $name, string $class): DefinitionInterface |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritDoc} |
||
| 89 | */ |
||
| 90 | 12 | public function addDefinition(string $name, DefinitionInterface $definition): DefinitionInterface |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritDoc} |
||
| 97 | */ |
||
| 98 | 2 | public function addScalar(string $name, $value): void |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | public function addReference(string $name, string $class): void |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritDoc} |
||
| 113 | */ |
||
| 114 | public function addAliases(string $name, array $aliases = []): void |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | */ |
||
| 122 | public function addAlias(string $name, string $alias): void |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 2 | public function addInflection(InflectionInterface $inflection): InflectionInterface |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Resolve definition by provided id from definitions, aliases and references |
||
| 138 | * |
||
| 139 | * @param string $id |
||
| 140 | * @return object |
||
| 141 | * @throws CircularException |
||
| 142 | * @throws NotFoundException |
||
| 143 | */ |
||
| 144 | 8 | protected function resolveDefinitionById(string $id): object |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Create object by DefinitionInterface object or retrieve it from shared |
||
| 151 | * |
||
| 152 | * @param DefinitionInterface $definition |
||
| 153 | * @param string|null $id |
||
| 154 | * @return object |
||
| 155 | * @throws CircularException |
||
| 156 | */ |
||
| 157 | 9 | protected function resolveDefinition(DefinitionInterface $definition, ?string $id = null): object |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set that certain service are loading and checks circular exception |
||
| 172 | * |
||
| 173 | * @param string|null $id |
||
| 174 | * @throws CircularException |
||
| 175 | */ |
||
| 176 | 9 | protected function setLoading(?string $id): void |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Unset that certain service are loading |
||
| 190 | * |
||
| 191 | * @param string|null $id |
||
| 192 | */ |
||
| 193 | 8 | protected function unsetLoading(?string $id): void |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Apply all necessary inflections to object |
||
| 203 | * |
||
| 204 | * @param object $object |
||
| 205 | * @return object |
||
| 206 | */ |
||
| 207 | 9 | protected function inflect(object $object): object |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @inheritDoc |
||
| 219 | */ |
||
| 220 | 12 | public function get($id) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Try to find entry in delegates |
||
| 246 | * |
||
| 247 | * @param $id |
||
| 248 | * @return mixed|object|null |
||
| 249 | */ |
||
| 250 | 2 | public function getFromDelegates($id) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @inheritDoc |
||
| 266 | */ |
||
| 267 | 13 | public function has($id): bool |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritDoc |
||
| 296 | */ |
||
| 297 | 1 | public function invoke(callable $callable, array $arguments = []) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @inheritDoc |
||
| 304 | */ |
||
| 305 | 2 | public function addDelegate(PsrContainerInterface $container, bool $applyInflection = true): void |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Set that all dependencies will be analyzed for constructors, callable objects and methods |
||
| 312 | * |
||
| 313 | * @param bool $autoWire |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | 1 | public function setAutoWire(bool $autoWire): self |
|
| 321 | } |
||
| 322 |