Total Complexity | 49 |
Total Lines | 264 |
Duplicated Lines | 0 % |
Changes | 31 | ||
Bugs | 7 | Features | 3 |
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 |
||
7 | class LeMarchand implements ContainerInterface |
||
8 | { |
||
9 | private static $instance = null; |
||
10 | // stores all the settings |
||
11 | private $configurations = []; |
||
12 | |||
13 | // stores the namespace cascade |
||
14 | private $namespace_cascade = []; |
||
15 | |||
16 | // stores the interface to class wiring |
||
17 | private $interface_wiring = []; |
||
18 | |||
19 | // store the resolved names for performance |
||
20 | private $resolved_cache = []; |
||
21 | // public const RX_CONTROLLER_NAME = '/([a-zA-Z]+)Controller$/'; |
||
22 | // public const RX_MODEL_CLASS = '/([a-zA-Z]+)(Class|Model)$/'; |
||
23 | const RX_SETTINGS = '/^settings\./'; |
||
24 | // public const RX_INTERFACE_NAME = '/([a-zA-Z]+)Interface$/'; |
||
25 | const RX_CLASS_NAME = '/([a-zA-Z]+)(Class|Model|Controller|Interface)$/'; |
||
26 | const RX_INTERFACE = '/([a-zA-Z]+)Interface$/'; |
||
27 | |||
28 | const RX_MVC = '/(Models|Controllers)\\\([a-zA-Z]+)(::class)?/'; |
||
29 | |||
30 | public static function box($settings = null): ContainerInterface |
||
31 | { |
||
32 | if (is_null(self::$instance)) { |
||
33 | if (is_array($settings)) { |
||
34 | return (self::$instance = new LeMarchand($settings)); |
||
35 | } |
||
36 | throw new LamentException('UNABLE_TO_OPEN_BOX'); |
||
37 | } |
||
38 | |||
39 | return self::$instance; |
||
40 | } |
||
41 | |||
42 | |||
43 | private function __construct($settings) |
||
44 | { |
||
45 | $this->configurations['settings'] = $settings; |
||
46 | if (isset($settings['LeMarchand'])) { |
||
47 | $this->namespace_cascade = $settings['LeMarchand']['cascade'] ?? []; |
||
48 | $this->interface_wiring = $settings['LeMarchand']['wiring'] ?? []; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public function __debugInfo(): array |
||
53 | { |
||
54 | $dbg = get_object_vars($this); |
||
55 | if (isset($dbg['configurations']['template_engine'])) { |
||
56 | // way too long of an object |
||
57 | $dbg['configurations']['template_engine'] = get_class($dbg['configurations']['template_engine']); |
||
58 | } |
||
59 | return $dbg; |
||
60 | } |
||
61 | |||
62 | public function put($configuration, $instance) |
||
63 | { |
||
64 | if (!is_string($configuration)) { |
||
65 | throw new LamentException($configuration); |
||
66 | } |
||
67 | $this->configurations[$configuration] = $instance; |
||
68 | } |
||
69 | |||
70 | public function has($configuration) |
||
71 | { |
||
72 | try { |
||
73 | $this->get($configuration); |
||
74 | return true; |
||
75 | } catch (NotFoundExceptionInterface $e) { |
||
76 | return false; |
||
77 | } catch (ContainerExceptionInterface $e) { |
||
78 | return false; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | |||
83 | public function get($configuration) |
||
84 | { |
||
85 | if (!is_string($configuration)) { |
||
86 | throw new LamentException($configuration); |
||
87 | } |
||
88 | // 1. is it a first level key ? |
||
89 | if (isset($this->configurations[$configuration])) { |
||
90 | return $this->configurations[$configuration]; |
||
91 | } |
||
92 | |||
93 | // 2. is it configuration data ? |
||
94 | if (preg_match(self::RX_SETTINGS, $configuration, $m) === 1) { |
||
95 | return $this->getSettings($configuration); |
||
96 | } |
||
97 | |||
98 | // 3. is it an existing class |
||
99 | if (class_exists($configuration)) { |
||
100 | return $this->getInstance($configuration); |
||
101 | } |
||
102 | |||
103 | |||
104 | // vdt($configuration, __FUNCTION__); |
||
105 | if (preg_match(self::RX_MVC, $configuration, $m) === 1) { |
||
106 | return $this->classification($m[2], $m[1], isset($m[3])); |
||
107 | } |
||
108 | |||
109 | // if it is an interface, we respond with an instance |
||
110 | if (preg_match(self::RX_INTERFACE, $configuration, $m) === 1) { |
||
111 | // vdt($configuration,__FUNCTION__); |
||
112 | return $this->wireInstance($configuration); |
||
113 | // return $this->classification($m[2], $m[1], isset($m[3])); |
||
114 | } |
||
115 | |||
116 | // 3. is it a class |
||
117 | // vdt($configuration, __FUNCTION__); |
||
118 | // if (preg_match(self::RX_CLASS_NAME, $configuration, $m) === 1) { |
||
119 | // return $this->classification($m[1], $m[2]); |
||
120 | // } |
||
121 | |||
122 | throw new ConfigurationException($configuration); |
||
123 | } |
||
124 | |||
125 | |||
126 | private function getSettings($setting) |
||
127 | { |
||
128 | // vd(__FUNCTION__); |
||
129 | $ret = $this->configurations; |
||
130 | |||
131 | //dot based hierarchy, parse and climb |
||
132 | foreach (explode('.', $setting) as $k) { |
||
133 | if (!isset($ret[$k])) { |
||
134 | throw new ConfigurationException($setting); |
||
135 | } |
||
136 | $ret = $ret[$k]; |
||
137 | } |
||
138 | |||
139 | return $ret; |
||
140 | } |
||
141 | |||
142 | private function classification($name, $type, $only_class_name = false) |
||
151 | } |
||
152 | |||
153 | private function resolved($clue, $solution = null) |
||
154 | { |
||
155 | if (!is_null($solution)) { |
||
156 | $this->resolved_cache[$clue] = $solution; |
||
157 | } |
||
158 | // vd($clue, __FUNCTION__); |
||
159 | return $this->resolved_cache[$clue] ?? null; |
||
160 | } |
||
161 | |||
162 | private function isResolved($clue): bool |
||
165 | } |
||
166 | |||
167 | private function cascadeNamespace($class_name, $mvc_type = null) |
||
181 | } |
||
182 | |||
183 | private function wireInstance($interface) |
||
184 | { |
||
185 | // vd($interface, __FUNCTION__); |
||
186 | |||
187 | if (!isset($this->interface_wiring[$interface])) { |
||
188 | throw new ConfigurationException($interface); |
||
189 | } |
||
190 | |||
191 | $wire = $this->interface_wiring[$interface]; |
||
192 | |||
193 | // interface + constructor params |
||
194 | if ($this->hasEmbeddedConstructorParameters($wire)) { |
||
195 | $class = array_shift($wire); |
||
196 | $args = $wire; |
||
197 | } else { |
||
198 | $class = $wire; |
||
199 | $args = null; |
||
200 | } |
||
201 | |||
202 | if ($this->isResolved($class) && $this->hasPrivateContructor($class)) { |
||
203 | return $this->resolved($class); |
||
204 | } |
||
205 | |||
206 | return $this->getInstance($class, $args); |
||
207 | } |
||
208 | |||
209 | private function hasPrivateContructor($class_name): bool |
||
213 | } |
||
214 | |||
215 | private function hasEmbeddedConstructorParameters($wire) |
||
216 | { |
||
217 | return is_array($wire); |
||
218 | } |
||
219 | |||
220 | private function getInstance($class, $construction_args = []) |
||
221 | { |
||
222 | try { |
||
223 | $rc = new \ReflectionClass($class); |
||
224 | $instance = null; |
||
225 | |||
226 | if (!is_null($constructor = $rc->getConstructor())) { |
||
227 | $construction_args = $this->getConstructorParameters($constructor, $construction_args); |
||
228 | |||
229 | if ($constructor->isPrivate()) { // singleton ? |
||
230 | // first argument is the static instance-making method |
||
231 | $singleton_method = $rc->getMethod(array_shift($construction_args)); |
||
232 | // invoke the method with remaining constructor args |
||
233 | $instance = $this->resolved($class, $singleton_method->invoke(null, $construction_args)); |
||
234 | } else { |
||
235 | $instance = $rc->newInstanceArgs($construction_args); |
||
236 | } |
||
237 | } else { |
||
238 | $instance = $rc->newInstanceArgs(); |
||
239 | } |
||
240 | |||
241 | if ($rc->hasMethod('set_container')) { |
||
242 | $instance->set_container($this); |
||
243 | } |
||
244 | |||
245 | return $instance; |
||
246 | } catch (\ReflectionException $e) { |
||
247 | throw new LamentException($e->getMessage()); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | |||
252 | private function getConstructorParameters(\ReflectionMethod $constructor, $construction_args = []) |
||
271 | } |
||
272 | } |
||
273 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.