| Total Complexity | 57 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 42 | ||
| 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 |
||
| 74 | } |
||
| 75 | |||
| 76 | public function has($configuration) |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | public function get($configuration) |
||
| 90 | { |
||
| 91 | if (!is_string($configuration)) { |
||
| 92 | throw new ContainerException($configuration); |
||
| 93 | } |
||
| 94 | |||
| 95 | $ret = null; |
||
| 96 | |||
| 97 | if ($this->isFirstLevelKey($configuration)) { |
||
| 98 | $ret = $this->configurations[$configuration]; |
||
| 99 | } |
||
| 100 | elseif ($this->isSettings($configuration)) { |
||
| 101 | $ret = $this->getSettings($configuration); |
||
| 102 | } |
||
| 103 | elseif (class_exists($configuration)) { |
||
| 104 | $ret = $this->getInstance($configuration); |
||
| 105 | } |
||
| 106 | elseif ($this->isInterface($configuration)) { |
||
| 107 | $ret = $this->wireInstance($configuration); |
||
| 108 | } |
||
| 109 | elseif ($this->hasModelOrController($configuration)) { |
||
|
|
|||
| 110 | // 5. is it cascadable ? |
||
| 111 | preg_match(self::RX_MVC, $configuration, $m); |
||
| 112 | |||
| 113 | $class_name = $this->cascadeNamespace($m[1] . '\\' . $m[2]); |
||
| 114 | |||
| 115 | if($this->hasClassNameModifier($configuration)){ |
||
| 116 | $ret = $class_name; |
||
| 117 | } |
||
| 118 | elseif($this->hasNewInstanceModifier($configuration)){ |
||
| 119 | $ret = $this->makeInstance($class_name); |
||
| 120 | } |
||
| 121 | $ret = $this->getInstance($class_name); |
||
| 122 | } |
||
| 123 | |||
| 124 | if(is_null($ret)) |
||
| 125 | throw new NotFoundException($configuration); |
||
| 126 | |||
| 127 | return $ret; |
||
| 128 | } |
||
| 129 | |||
| 130 | private function isFirstLevelKey($configuration){ |
||
| 131 | return isset($this->configurations[$configuration]); |
||
| 132 | } |
||
| 133 | |||
| 134 | private function isSettings($configuration){ |
||
| 135 | return preg_match(self::RX_SETTINGS, $configuration) === 1; |
||
| 136 | } |
||
| 137 | |||
| 138 | private function isInterface($configuration){ |
||
| 139 | return preg_match(self::RX_INTERFACE, $configuration) === 1; |
||
| 140 | } |
||
| 141 | |||
| 142 | private function isModelOrController($configuration){ |
||
| 143 | return preg_match(self::RX_MVC, $configuration) === 1; |
||
| 144 | } |
||
| 145 | |||
| 146 | private function hasClassNameModifier($configuration){ |
||
| 147 | return strpos($configuration, '::class') !== false; |
||
| 148 | } |
||
| 149 | |||
| 150 | private function hasNewInstanceModifier($configuration){ |
||
| 151 | return strpos($configuration, '::new') !== false; |
||
| 152 | } |
||
| 153 | |||
| 154 | private function getSettings($setting) |
||
| 155 | { |
||
| 156 | // vd(__FUNCTION__); |
||
| 157 | $ret = $this->configurations; |
||
| 158 | |||
| 159 | //dot based hierarchy, parse and climb |
||
| 160 | foreach (explode('.', $setting) as $k) { |
||
| 161 | if (!isset($ret[$k])) { |
||
| 162 | throw new NotFoundException($setting); |
||
| 163 | } |
||
| 164 | $ret = $ret[$k]; |
||
| 165 | } |
||
| 166 | |||
| 167 | return $ret; |
||
| 168 | } |
||
| 169 | |||
| 170 | private function resolved($clue, $solution = null) |
||
| 171 | { |
||
| 172 | if (!is_null($solution)) { |
||
| 173 | $this->resolved_cache[$clue] = $solution; |
||
| 174 | } |
||
| 175 | // vd($clue, __FUNCTION__); |
||
| 176 | return $this->resolved_cache[$clue] ?? null; |
||
| 177 | } |
||
| 178 | |||
| 179 | private function isResolved($clue): bool |
||
| 180 | { |
||
| 181 | return isset($this->resolved_cache[$clue]); |
||
| 182 | } |
||
| 183 | |||
| 184 | private function cascadeNamespace($class_name) |
||
| 185 | { |
||
| 186 | if ($this->isResolved($class_name)) { |
||
| 187 | return $this->resolved($class_name); |
||
| 188 | } |
||
| 189 | |||
| 190 | // not fully namespaced, lets cascade |
||
| 191 | foreach ($this->namespace_cascade as $ns) { |
||
| 192 | if (class_exists($fully_namespaced = $ns . $class_name)) { |
||
| 193 | $this->resolved($class_name, $fully_namespaced); |
||
| 194 | return $fully_namespaced; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | throw new NotFoundException($class_name); |
||
| 198 | } |
||
| 199 | |||
| 200 | private function wireInstance($interface) |
||
| 201 | { |
||
| 202 | if (!isset($this->interface_wiring[$interface])) { |
||
| 203 | throw new NotFoundException($interface); |
||
| 204 | } |
||
| 205 | |||
| 206 | $wire = $this->interface_wiring[$interface]; |
||
| 207 | |||
| 208 | // interface + constructor params |
||
| 209 | if ($this->hasEmbeddedConstructorParameters($wire)) { |
||
| 210 | $class = array_shift($wire); |
||
| 211 | $args = $wire; |
||
| 212 | } else { |
||
| 213 | $class = $wire; |
||
| 214 | $args = null; |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($this->isResolved($class) && $this->hasPrivateContructor($class)) { |
||
| 218 | return $this->resolved($class); |
||
| 219 | } |
||
| 220 | |||
| 221 | return $this->getInstance($class, $args); |
||
| 222 | } |
||
| 223 | |||
| 224 | private function hasPrivateContructor($class_name): bool |
||
| 225 | { |
||
| 226 | $rc = new \ReflectionClass($class_name); |
||
| 227 | return !is_null($constructor = $rc->getConstructor()) && $constructor->isPrivate(); |
||
| 228 | } |
||
| 229 | |||
| 230 | private function hasEmbeddedConstructorParameters($wire) |
||
| 231 | { |
||
| 232 | return is_array($wire); |
||
| 233 | } |
||
| 234 | |||
| 235 | private function getInstance($class, $construction_args = []) |
||
| 236 | { |
||
| 237 | if (isset($this->instance_cache[$class])) { |
||
| 238 | return $this->instance_cache[$class]; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $this->makeInstance($class, $construction_args); |
||
| 242 | } |
||
| 243 | |||
| 244 | private function makeInstance($class, $construction_args = []) |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | private function getConstructorParameters(\ReflectionMethod $constructor, $construction_args = []) |
||
| 277 | { |
||
| 278 | // vd(__FUNCTION__); |
||
| 279 | |||
| 295 | } |
||
| 296 | } |
||
| 297 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.