Total Complexity | 40 |
Total Lines | 199 |
Duplicated Lines | 0 % |
Changes | 29 | ||
Bugs | 7 | Features | 2 |
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 | // public const RX_CONTROLLER_NAME = '/([a-zA-Z]+)Controller$/'; |
||
20 | // public const RX_MODEL_CLASS = '/([a-zA-Z]+)(Class|Model)$/'; |
||
21 | const RX_SETTINGS = '/^settings\./'; |
||
22 | // public const RX_INTERFACE_NAME = '/([a-zA-Z]+)Interface$/'; |
||
23 | const RX_CLASS_NAME = '/([a-zA-Z]+)(Class|Model|Controller|Interface)$/'; |
||
24 | |||
25 | |||
26 | public static function box($settings = null): ContainerInterface |
||
36 | } |
||
37 | |||
38 | |||
39 | private function __construct($settings) |
||
40 | { |
||
41 | $this->configurations['settings'] = $settings; |
||
42 | if(isset($settings['LeMarchand'])) |
||
43 | { |
||
44 | $this->namespace_cascade = $settings['LeMarchand']['cascade'] ?? []; |
||
45 | $this->interface_wiring = $settings['LeMarchand']['wiring'] ?? []; |
||
46 | } |
||
47 | } |
||
48 | |||
49 | public function __debugInfo(): array |
||
50 | { |
||
51 | $dbg = get_object_vars($this); |
||
52 | if (isset($dbg['configurations']['template_engine'])) { |
||
53 | // way too long of an object |
||
54 | $dbg['configurations']['template_engine'] = get_class($dbg['configurations']['template_engine']); |
||
55 | } |
||
56 | return $dbg; |
||
57 | } |
||
58 | |||
59 | public function put($configuration, $instance) |
||
60 | { |
||
61 | if (!is_string($configuration)) { |
||
62 | throw new LamentException($configuration); |
||
63 | } |
||
64 | $this->configurations[$configuration] = $instance; |
||
65 | } |
||
66 | |||
67 | public function has($configuration) |
||
68 | { |
||
69 | try { |
||
70 | $this->get($configuration); |
||
71 | return true; |
||
72 | } catch (NotFoundExceptionInterface $e) { |
||
73 | return false; |
||
74 | } catch (ContainerExceptionInterface $e) { |
||
75 | return false; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | |||
80 | public function get($configuration) |
||
81 | { |
||
82 | |||
83 | if (!is_string($configuration)) { |
||
84 | throw new LamentException($configuration); |
||
85 | } |
||
86 | // 1. is it a first level key ? |
||
87 | if (isset($this->configurations[$configuration])) { |
||
88 | return $this->configurations[$configuration]; |
||
89 | } |
||
90 | |||
91 | // 2. is it configuration data ? |
||
92 | if (preg_match(self::RX_SETTINGS, $configuration, $m) === 1) { |
||
93 | return $this->getSettings($configuration); |
||
94 | } |
||
95 | // 3. is it a class |
||
96 | if (preg_match(self::RX_CLASS_NAME, $configuration, $m) === 1) { |
||
97 | return $this->classification($m[1], $m[2]); |
||
98 | } |
||
99 | |||
100 | throw new ConfigurationException($configuration); |
||
101 | } |
||
102 | |||
103 | |||
104 | private function getSettings($setting) |
||
105 | { |
||
106 | // vdt(__FUNCTION__); |
||
107 | $ret = $this->configurations; |
||
108 | |||
109 | //dot based hierarchy, parse and climb |
||
110 | foreach (explode('.', $setting) as $k) { |
||
111 | if (!isset($ret[$k])) { |
||
112 | throw new ConfigurationException($setting); |
||
113 | } |
||
114 | $ret = $ret[$k]; |
||
115 | } |
||
116 | |||
117 | return $ret; |
||
118 | } |
||
119 | |||
120 | private function classification($name, $type) |
||
133 | } |
||
134 | |||
135 | private function cascadeNamespace($class_name, $mvc_type = null) |
||
158 | } |
||
159 | |||
160 | private function wireInstance($interface) |
||
161 | { |
||
162 | if (!isset($this->interface_wiring[$interface])) { |
||
163 | throw new ConfigurationException($interface); |
||
164 | } |
||
165 | $wire = $this->interface_wiring[$interface]; |
||
166 | if(is_array($wire)){ |
||
167 | $class = array_shift($wire); |
||
168 | return $this->getInstance($class, $wire); |
||
169 | } |
||
170 | return $this->getInstance($this->interface_wiring[$interface]); |
||
171 | } |
||
172 | |||
173 | private function getInstance($class, $construction_args = []) |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 |