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 | 25 | public function __construct( |
|
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | 1 | public function addDefinitionClass(string $name, string $class): DefinitionInterface |
|
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | 12 | public function addDefinition(string $name, DefinitionInterface $definition): DefinitionInterface |
|
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | 2 | public function addScalar(string $name, $value): void |
|
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | 1 | public function addReference(string $name, string $class): void |
|
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | 1 | public function addAliases(string $name, array $aliases = []): void |
|
112 | |||
113 | /** |
||
114 | * {@inheritDoc} |
||
115 | */ |
||
116 | 1 | public function addAlias(string $name, string $alias): void |
|
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | */ |
||
124 | 2 | public function addInflection(InflectionInterface $inflection): InflectionInterface |
|
129 | |||
130 | |||
131 | /** |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | 2 | public function addDelegate(PsrContainerInterface $container, bool $applyInflection = true): void |
|
138 | |||
139 | /** |
||
140 | * Create object by DefinitionInterface object or retrieve it from shared |
||
141 | * |
||
142 | * @param DefinitionInterface $definition |
||
143 | * @param string|null $id |
||
144 | * @return object |
||
145 | * @throws CircularException |
||
146 | */ |
||
147 | 9 | protected function resolveDefinition(DefinitionInterface $definition, ?string $id = null): object |
|
159 | |||
160 | /** |
||
161 | * Set that certain service are loading and checks circular exception |
||
162 | * |
||
163 | * @param string|null $id |
||
164 | * @throws CircularException |
||
165 | */ |
||
166 | 9 | protected function setLoading(?string $id): void |
|
177 | |||
178 | /** |
||
179 | * Unset that certain service are loading |
||
180 | * |
||
181 | * @param string|null $id |
||
182 | */ |
||
183 | 8 | protected function unsetLoading(?string $id): void |
|
190 | |||
191 | /** |
||
192 | * Apply all necessary inflections to object |
||
193 | * |
||
194 | * @param object $object |
||
195 | * @return object |
||
196 | */ |
||
197 | 9 | protected function inflect(object $object): object |
|
206 | |||
207 | /** |
||
208 | * @inheritDoc |
||
209 | */ |
||
210 | 12 | public function get($id) |
|
232 | |||
233 | /** |
||
234 | * Check that autowire is enabled and class can be loaded |
||
235 | * |
||
236 | * @param string $class |
||
237 | * @return bool |
||
238 | */ |
||
239 | 7 | protected function isAutoWireAvailiableForClass(string $class): bool |
|
243 | |||
244 | /** |
||
245 | * Try to find entry in definitions or shared |
||
246 | * |
||
247 | * @param string $id |
||
248 | * @return object |
||
249 | * @throws CircularException |
||
250 | * @throws NotFoundException |
||
251 | */ |
||
252 | 8 | protected function getFromDefinitions(string $id): object |
|
257 | |||
258 | /** |
||
259 | * Try to find entry in delegates |
||
260 | * |
||
261 | * @param $id |
||
262 | * @return object|null |
||
263 | */ |
||
264 | 2 | protected function getFromDelegates($id) |
|
277 | |||
278 | /** |
||
279 | * Check that entry exists in delegates |
||
280 | * |
||
281 | * @param $id |
||
282 | * @return bool |
||
283 | */ |
||
284 | 3 | protected function hasInDelegates($id): bool |
|
293 | |||
294 | /** |
||
295 | * @inheritDoc |
||
296 | */ |
||
297 | 13 | public function has($id): bool |
|
313 | |||
314 | /** |
||
315 | * @inheritDoc |
||
316 | */ |
||
317 | 1 | public function invoke(callable $callable, array $arguments = []) |
|
321 | |||
322 | /** |
||
323 | * Set that all dependencies will be analyzed for constructors, callable objects and methods |
||
324 | * |
||
325 | * @param bool $autoWire |
||
326 | * @return $this |
||
327 | */ |
||
328 | 1 | public function setAutoWire(bool $autoWire): self |
|
333 | } |
||
334 |