Total Complexity | 41 |
Total Lines | 237 |
Duplicated Lines | 0 % |
Coverage | 98.98% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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.
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 | private ?DependencyResolver $dependencyResolver = null; |
||
19 | |||
20 | /** @var array<class-string,list<mixed>> */ |
||
21 | private array $cachedDependencies = []; |
||
22 | |||
23 | /** @var array<string,mixed> */ |
||
24 | private array $instances = []; |
||
25 | |||
26 | private SplObjectStorage $factoryInstances; |
||
27 | |||
28 | private SplObjectStorage $protectedInstances; |
||
29 | |||
30 | /** @var array<string,bool> */ |
||
31 | private array $frozenInstances = []; |
||
32 | |||
33 | private ?string $currentlyExtending = null; |
||
34 | |||
35 | /** |
||
36 | * @param array<class-string, class-string|callable|object> $bindings |
||
37 | * @param array<string, list<Closure>> $instancesToExtend |
||
38 | */ |
||
39 | 37 | public function __construct( |
|
40 | private array $bindings = [], |
||
41 | private array $instancesToExtend = [], |
||
42 | ) { |
||
43 | 37 | $this->factoryInstances = new SplObjectStorage(); |
|
44 | 37 | $this->protectedInstances = new SplObjectStorage(); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param class-string $className |
||
49 | */ |
||
50 | 5 | public static function create(string $className): mixed |
|
51 | { |
||
52 | 5 | return (new self())->get($className); |
|
53 | } |
||
54 | |||
55 | 37 | public function has(string $id): bool |
|
56 | { |
||
57 | 37 | return isset($this->instances[$id]); |
|
58 | } |
||
59 | |||
60 | 19 | public function set(string $id, mixed $instance): void |
|
61 | { |
||
62 | 19 | if (!empty($this->frozenInstances[$id])) { |
|
63 | 1 | throw ContainerException::frozenInstanceOverride($id); |
|
64 | } |
||
65 | |||
66 | 19 | $this->instances[$id] = $instance; |
|
67 | |||
68 | 19 | if ($this->currentlyExtending === $id) { |
|
69 | 3 | return; |
|
70 | } |
||
71 | |||
72 | 19 | $this->extendService($id); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param class-string|string $id |
||
77 | */ |
||
78 | 33 | public function get(string $id): mixed |
|
79 | { |
||
80 | 33 | if ($this->has($id)) { |
|
81 | 15 | return $this->getInstance($id); |
|
82 | } |
||
83 | |||
84 | 18 | return $this->createInstance($id); |
|
85 | } |
||
86 | |||
87 | 1 | public function factory(Closure $instance): Closure |
|
88 | { |
||
89 | 1 | $this->factoryInstances->attach($instance); |
|
90 | |||
91 | 1 | return $instance; |
|
92 | } |
||
93 | |||
94 | 1 | public function remove(string $id): void |
|
95 | { |
||
96 | 1 | unset( |
|
97 | 1 | $this->instances[$id], |
|
98 | 1 | $this->frozenInstances[$id] |
|
99 | 1 | ); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @psalm-suppress MixedAssignment |
||
104 | */ |
||
105 | 11 | public function extend(string $id, Closure $instance): Closure |
|
106 | { |
||
107 | 11 | if (!$this->has($id)) { |
|
108 | 4 | $this->extendLater($id, $instance); |
|
109 | |||
110 | 4 | return $instance; |
|
111 | } |
||
112 | |||
113 | 10 | if (isset($this->frozenInstances[$id])) { |
|
114 | 4 | throw ContainerException::frozenInstanceExtend($id); |
|
115 | } |
||
116 | |||
117 | 8 | if (is_object($this->instances[$id]) && isset($this->protectedInstances[$this->instances[$id]])) { |
|
118 | 1 | throw ContainerException::instanceProtected($id); |
|
119 | } |
||
120 | |||
121 | 7 | $factory = $this->instances[$id]; |
|
122 | 7 | $extended = $this->generateExtendedInstance($instance, $factory); |
|
123 | 6 | $this->set($id, $extended); |
|
124 | |||
125 | 6 | return $extended; |
|
126 | } |
||
127 | |||
128 | 2 | public function protect(Closure $instance): Closure |
|
133 | } |
||
134 | |||
135 | 15 | private function getInstance(string $id): mixed |
|
136 | { |
||
137 | 15 | $this->frozenInstances[$id] = true; |
|
138 | |||
139 | 15 | if (!is_object($this->instances[$id]) |
|
140 | 14 | || isset($this->protectedInstances[$this->instances[$id]]) |
|
141 | 15 | || !method_exists($this->instances[$id], '__invoke') |
|
142 | ) { |
||
143 | 8 | return $this->instances[$id]; |
|
144 | } |
||
145 | |||
146 | 12 | if (isset($this->factoryInstances[$this->instances[$id]])) { |
|
147 | 1 | return $this->instances[$id]($this); |
|
148 | } |
||
149 | |||
150 | 11 | $rawService = $this->instances[$id]; |
|
151 | |||
152 | /** @var mixed $resolvedService */ |
||
153 | 11 | $resolvedService = $rawService($this); |
|
154 | |||
155 | 11 | $this->instances[$id] = $resolvedService; |
|
156 | |||
157 | 11 | return $resolvedService; |
|
158 | } |
||
159 | |||
160 | 18 | private function createInstance(string $class): ?object |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param class-string $class |
||
187 | */ |
||
188 | 12 | private function instantiateClass(string $class): ?object |
|
202 | } |
||
203 | |||
204 | 4 | private function extendLater(string $id, Closure $instance): void |
|
205 | { |
||
206 | 4 | $this->instancesToExtend[$id][] = $instance; |
|
207 | } |
||
208 | |||
209 | 12 | private function getDependencyResolver(): DependencyResolver |
|
210 | { |
||
211 | 12 | if ($this->dependencyResolver === null) { |
|
212 | 12 | $this->dependencyResolver = new DependencyResolver( |
|
213 | 12 | $this->bindings, |
|
214 | 12 | ); |
|
215 | } |
||
216 | |||
217 | 12 | return $this->dependencyResolver; |
|
1 ignored issue
–
show
|
|||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @psalm-suppress MissingClosureReturnType,MixedAssignment |
||
222 | */ |
||
223 | 7 | private function generateExtendedInstance(Closure $instance, mixed $factory): Closure |
|
224 | { |
||
225 | 7 | if (is_callable($factory)) { |
|
226 | 5 | return static function (self $container) use ($instance, $factory) { |
|
227 | 5 | $result = $factory($container); |
|
228 | |||
229 | 5 | return $instance($result, $container) ?? $result; |
|
230 | 5 | }; |
|
231 | } |
||
232 | |||
233 | 4 | if (is_object($factory) || is_array($factory)) { |
|
234 | 3 | return static fn (self $container) => $instance($factory, $container) ?? $factory; |
|
235 | } |
||
236 | |||
237 | 1 | throw ContainerException::instanceNotExtendable(); |
|
238 | } |
||
239 | |||
240 | 19 | private function extendService(string $id): void |
|
253 | } |
||
254 | } |
||
255 |