Total Complexity | 51 |
Total Lines | 266 |
Duplicated Lines | 0 % |
Changes | 41 | ||
Bugs | 8 | Features | 4 |
Complex classes like LeMarchand 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 LeMarchand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class LeMarchand implements ContainerInterface |
||
10 | { |
||
11 | private static $instance = null; |
||
12 | // stores all the settings |
||
13 | private $configurations = []; |
||
14 | |||
15 | // stores the namespace cascade |
||
16 | private $namespace_cascade = []; |
||
17 | |||
18 | // stores the interface to class wiring |
||
19 | private $interface_wiring = []; |
||
20 | |||
21 | // store the resolved names for performance |
||
22 | private $resolved_cache = []; |
||
23 | |||
24 | // stores the automatically created instances, by class name |
||
25 | private $instance_cache = []; |
||
26 | |||
27 | |||
28 | public const RX_SETTINGS = '/^settings\./'; |
||
29 | |||
30 | public const RX_MVC = '/(Models|Controllers)\\\([a-zA-Z]+)(::class|::new)?/'; |
||
31 | |||
32 | public const RX_INTERFACE = '/([a-zA-Z]+)Interface$/'; |
||
33 | |||
34 | |||
35 | public static function box($settings = null): ContainerInterface |
||
36 | { |
||
37 | if (is_null(self::$instance)) { |
||
38 | if (is_array($settings)) { |
||
39 | return (self::$instance = new LeMarchand($settings)); |
||
40 | } |
||
41 | throw new ContainerException('UNABLE_TO_OPEN_BOX'); |
||
42 | } |
||
43 | |||
44 | return self::$instance; |
||
45 | } |
||
46 | |||
47 | |||
48 | private function __construct($settings) |
||
56 | } |
||
57 | |||
58 | public function __debugInfo(): array |
||
59 | { |
||
60 | $dbg = get_object_vars($this); |
||
61 | |||
62 | foreach ($dbg['instance_cache'] as $class => $instance) { |
||
63 | $dbg['instance_cache'][$class] = true; |
||
64 | } |
||
65 | |||
66 | foreach ($dbg['interface_wiring'] as $interface => $wire) { |
||
67 | if (is_array($wire)) { |
||
68 | $wire = array_shift($wire) . ' --array #' . count($wire); |
||
69 | } |
||
70 | $dbg['interface_wiring'][$interface] = $wire; |
||
71 | } |
||
72 | |||
73 | return $dbg; |
||
74 | } |
||
75 | |||
76 | public function has($configuration) |
||
77 | { |
||
78 | try { |
||
79 | $this->get($configuration); |
||
80 | return true; |
||
81 | } catch (NotFoundExceptionInterface $e) { |
||
82 | return false; |
||
83 | } catch (ContainerExceptionInterface $e) { |
||
84 | return false; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public function get($configuration) |
||
89 | { |
||
90 | if (!is_string($configuration)) { |
||
91 | throw new ContainerException($configuration); |
||
92 | } |
||
93 | // 1. is it a first level key ? |
||
94 | if (isset($this->configurations[$configuration])) { |
||
95 | return $this->configurations[$configuration]; |
||
96 | } |
||
97 | |||
98 | // 2. is it configuration data ? |
||
99 | if (preg_match(self::RX_SETTINGS, $configuration) === 1) { |
||
100 | return $this->getSettings($configuration); |
||
101 | } |
||
102 | |||
103 | // 3. is it an existing class ? |
||
104 | if (class_exists($configuration)) { |
||
105 | return $this->getInstance($configuration); |
||
106 | } |
||
107 | |||
108 | // 4. is it an interface ? |
||
109 | if (preg_match(self::RX_INTERFACE, $configuration) === 1) { |
||
110 | return $this->wireInstance($configuration); |
||
111 | } |
||
112 | |||
113 | // 5. is it cascadable ? |
||
114 | if (preg_match(self::RX_MVC, $configuration, $m) === 1) { |
||
115 | $class_name = $this->cascadeNamespace($m[1] . '\\' . $m[2]); |
||
116 | |||
117 | if (!isset($m[3])) { |
||
118 | return $this->getInstance($class_name); |
||
119 | } |
||
120 | |||
121 | if ($m[3] === '::class') { |
||
122 | return $class_name; |
||
123 | } |
||
124 | |||
125 | if ($m[3] === '::new') { |
||
126 | return $this->makeInstance($class_name); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | throw new NotFoundException($configuration); |
||
131 | } |
||
132 | |||
133 | |||
134 | private function getSettings($setting) |
||
135 | { |
||
136 | // vd(__FUNCTION__); |
||
137 | $ret = $this->configurations; |
||
138 | |||
139 | //dot based hierarchy, parse and climb |
||
140 | foreach (explode('.', $setting) as $k) { |
||
141 | if (!isset($ret[$k])) { |
||
142 | throw new NotFoundException($setting); |
||
143 | } |
||
144 | $ret = $ret[$k]; |
||
145 | } |
||
146 | |||
147 | return $ret; |
||
148 | } |
||
149 | |||
150 | private function resolved($clue, $solution = null) |
||
151 | { |
||
152 | if (!is_null($solution)) { |
||
153 | $this->resolved_cache[$clue] = $solution; |
||
154 | } |
||
155 | // vd($clue, __FUNCTION__); |
||
156 | return $this->resolved_cache[$clue] ?? null; |
||
157 | } |
||
158 | |||
159 | private function isResolved($clue): bool |
||
160 | { |
||
161 | return isset($this->resolved_cache[$clue]); |
||
162 | } |
||
163 | |||
164 | private function cascadeNamespace($class_name) |
||
165 | { |
||
166 | if ($this->isResolved($class_name)) { |
||
167 | return $this->resolved($class_name); |
||
168 | } |
||
169 | |||
170 | // not fully namespaced, lets cascade |
||
171 | foreach ($this->namespace_cascade as $ns) { |
||
172 | if (class_exists($fully_namespaced = $ns . $class_name)) { |
||
173 | $this->resolved($class_name, $fully_namespaced); |
||
174 | return $fully_namespaced; |
||
175 | } |
||
176 | } |
||
177 | throw new NotFoundException($class_name); |
||
178 | } |
||
179 | |||
180 | private function wireInstance($interface) |
||
181 | { |
||
182 | if (!isset($this->interface_wiring[$interface])) { |
||
183 | throw new NotFoundException($interface); |
||
184 | } |
||
185 | |||
186 | $wire = $this->interface_wiring[$interface]; |
||
187 | |||
188 | // interface + constructor params |
||
189 | if ($this->hasEmbeddedConstructorParameters($wire)) { |
||
190 | $class = array_shift($wire); |
||
191 | $args = $wire; |
||
192 | } else { |
||
193 | $class = $wire; |
||
194 | $args = null; |
||
195 | } |
||
196 | |||
197 | if ($this->isResolved($class) && $this->hasPrivateContructor($class)) { |
||
198 | return $this->resolved($class); |
||
199 | } |
||
200 | |||
201 | return $this->getInstance($class, $args); |
||
202 | } |
||
203 | |||
204 | private function hasPrivateContructor($class_name): bool |
||
205 | { |
||
206 | $rc = new \ReflectionClass($class_name); |
||
207 | return !is_null($constructor = $rc->getConstructor()) && $constructor->isPrivate(); |
||
208 | } |
||
209 | |||
210 | private function hasEmbeddedConstructorParameters($wire) |
||
211 | { |
||
212 | return is_array($wire); |
||
213 | } |
||
214 | |||
215 | private function getInstance($class, $construction_args = []) |
||
216 | { |
||
217 | if (isset($this->instance_cache[$class])) { |
||
218 | return $this->instance_cache[$class]; |
||
219 | } |
||
220 | |||
221 | return $this->makeInstance($class, $construction_args); |
||
222 | } |
||
223 | |||
224 | private function makeInstance($class, $construction_args = []) |
||
253 | } |
||
254 | } |
||
255 | |||
256 | private function getConstructorParameters(\ReflectionMethod $constructor, $construction_args = []) |
||
257 | { |
||
275 | } |
||
276 | } |
||
277 |