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 |
||
16 | class Container implements ContainerInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var \League\Container\Definition\DefinitionFactoryInterface |
||
20 | */ |
||
21 | protected $definitionFactory; |
||
22 | |||
23 | /** |
||
24 | * @var \League\Container\Definition\DefinitionInterface[] |
||
25 | */ |
||
26 | protected $definitions = []; |
||
27 | |||
28 | /** |
||
29 | * @var \League\Container\Definition\DefinitionInterface[] |
||
30 | */ |
||
31 | protected $sharedDefinitions = []; |
||
32 | |||
33 | /** |
||
34 | * @var \League\Container\Inflector\InflectorAggregateInterface |
||
35 | */ |
||
36 | protected $inflectors; |
||
37 | |||
38 | /** |
||
39 | * @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface |
||
40 | */ |
||
41 | protected $providers; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $shared = []; |
||
47 | |||
48 | /** |
||
49 | * @var \Interop\Container\ContainerInterface[] |
||
50 | */ |
||
51 | protected $delegates = []; |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $isProtected = false; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | * |
||
61 | * @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers |
||
62 | * @param \League\Container\Inflector\InflectorAggregateInterface|null $inflectors |
||
63 | * @param \League\Container\Definition\DefinitionFactoryInterface|null $definitionFactory |
||
64 | */ |
||
65 | 63 | public function __construct( |
|
66 | ServiceProviderAggregateInterface $providers = null, |
||
67 | InflectorAggregateInterface $inflectors = null, |
||
68 | DefinitionFactoryInterface $definitionFactory = null |
||
69 | ) { |
||
70 | // set required dependencies |
||
71 | 63 | $this->providers = (is_null($providers)) |
|
72 | 63 | ? (new ServiceProviderAggregate)->setContainer($this) |
|
73 | 63 | : $providers->setContainer($this); |
|
74 | |||
75 | 63 | $this->inflectors = (is_null($inflectors)) |
|
76 | 63 | ? (new InflectorAggregate)->setContainer($this) |
|
77 | 63 | : $inflectors->setContainer($this); |
|
78 | |||
79 | 63 | $this->definitionFactory = (is_null($definitionFactory)) |
|
80 | 63 | ? (new DefinitionFactory)->setContainer($this) |
|
81 | 63 | : $definitionFactory->setContainer($this); |
|
82 | 63 | } |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 33 | public function get($alias, array $args = []) |
|
88 | { |
||
89 | 33 | $service = $this->getFromThisContainer($alias, $args); |
|
90 | |||
91 | 33 | if ($service === false && $this->providers->provides($alias)) { |
|
92 | 12 | $this->providers->register($alias); |
|
93 | 12 | $service = $this->getFromThisContainer($alias, $args); |
|
94 | 12 | } |
|
95 | |||
96 | 33 | if ($service !== false) { |
|
97 | 27 | return $service; |
|
98 | } |
||
99 | |||
100 | 12 | if ($resolved = $this->getFromDelegate($alias, $args)) { |
|
101 | 6 | return $this->inflectors->inflect($resolved); |
|
102 | } |
||
103 | |||
104 | 6 | throw new NotFoundException( |
|
105 | 6 | sprintf('Alias (%s) is not being managed by the container', $alias) |
|
106 | 6 | ); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 21 | public function has($alias) |
|
113 | { |
||
114 | 21 | if (array_key_exists($alias, $this->definitions) || $this->hasShared($alias)) { |
|
115 | 15 | return true; |
|
116 | } |
||
117 | |||
118 | 9 | if ($this->providers->provides($alias)) { |
|
119 | 3 | return true; |
|
120 | } |
||
121 | |||
122 | 9 | return $this->hasInDelegate($alias); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns a boolean to determine if the container has a shared instance of an alias. |
||
127 | * |
||
128 | * @param string $alias |
||
129 | * @param boolean $resolved |
||
130 | * @return boolean |
||
131 | */ |
||
132 | 42 | public function hasShared($alias, $resolved = false) |
|
133 | { |
||
134 | 42 | $shared = ($resolved === false) ? array_merge($this->shared, $this->sharedDefinitions) : $this->shared; |
|
135 | |||
136 | 42 | return (array_key_exists($alias, $shared)); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 39 | public function add($alias, $concrete = null, $share = false) |
|
143 | { |
||
144 | 39 | if (is_null($concrete)) { |
|
145 | 3 | $concrete = $alias; |
|
146 | 3 | } |
|
147 | |||
148 | 39 | if ($this->isProtected() && $this->has($alias)) { |
|
149 | 3 | throw new ProtectedException('Container has been protected, overriding services is not allowed.'); |
|
150 | } |
||
151 | |||
152 | 39 | $definition = $this->definitionFactory->getDefinition($alias, $concrete); |
|
153 | |||
154 | 39 | if ($definition instanceof DefinitionInterface) { |
|
155 | 27 | if ($share === false) { |
|
156 | 9 | $this->definitions[$alias] = $definition; |
|
157 | 9 | } else { |
|
158 | 21 | $this->sharedDefinitions[$alias] = $definition; |
|
159 | } |
||
160 | |||
161 | 27 | return $definition; |
|
162 | } |
||
163 | |||
164 | // dealing with a value that cannot build a definition |
||
165 | 12 | $this->shared[$alias] = $concrete; |
|
166 | 12 | } |
|
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 30 | public function share($alias, $concrete = null) |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 15 | public function addServiceProvider($provider) |
|
180 | { |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 9 | public function extend($alias) |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 3 | public function inflector($type, callable $callback = null) |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function call(callable $callable, array $args = []) |
||
235 | |||
236 | /** |
||
237 | * Marks the container as protected. |
||
238 | * |
||
239 | * Prevents overriding services and delegating to other containers once the container is marked as protected. |
||
240 | */ |
||
241 | 18 | public function protect() |
|
245 | |||
246 | /** |
||
247 | * Marks the container as unprotected. |
||
248 | * |
||
249 | * Allows overriding services and delegating to other containers (this is the default). |
||
250 | */ |
||
251 | 3 | public function unprotect() |
|
255 | |||
256 | /** |
||
257 | * Returns true if the container is protected. |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | 60 | public function isProtected() |
|
265 | |||
266 | /** |
||
267 | * Delegate a backup container to be checked for services if it |
||
268 | * cannot be resolved via this container. |
||
269 | * |
||
270 | * @param \Interop\Container\ContainerInterface $container |
||
271 | * @return $this |
||
272 | */ |
||
273 | 12 | public function delegate(InteropContainerInterface $container) |
|
287 | |||
288 | /** |
||
289 | * Returns true if service is registered in one of the delegated backup containers. |
||
290 | * |
||
291 | * @param string $alias |
||
292 | * @return boolean |
||
293 | */ |
||
294 | 9 | public function hasInDelegate($alias) |
|
304 | |||
305 | /** |
||
306 | * Attempt to get a service from the stack of delegated backup containers. |
||
307 | * |
||
308 | * @param string $alias |
||
309 | * @param array $args |
||
310 | * @return mixed |
||
311 | */ |
||
312 | 12 | protected function getFromDelegate($alias, array $args = []) |
|
324 | |||
325 | /** |
||
326 | * Get a service that has been registered in this container. |
||
327 | * |
||
328 | * @param string $alias |
||
329 | * @param array $args |
||
330 | * @return mixed |
||
331 | */ |
||
332 | 33 | protected function getFromThisContainer($alias, array $args = []) |
|
352 | } |
||
353 |