| Total Complexity | 71 | 
| Total Lines | 468 | 
| Duplicated Lines | 0 % | 
| Coverage | 98.67% | 
| Changes | 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  | 
            ||
| 49 | class Container implements ContainerInterface, ArrayAccess, IteratorAggregate, Countable  | 
            ||
| 50 | { | 
            ||
| 51 | /**  | 
            ||
| 52 | * 容器对象实例  | 
            ||
| 53 | * @var Container|Closure  | 
            ||
| 54 | */  | 
            ||
| 55 | protected static $instance;  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * 容器中的对象实例  | 
            ||
| 59 | * @var array  | 
            ||
| 60 | */  | 
            ||
| 61 | protected $instances = [];  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * 容器绑定标识  | 
            ||
| 65 | * @var array  | 
            ||
| 66 | */  | 
            ||
| 67 | protected $bind = [  | 
            ||
| 68 | 'app' => App::class,  | 
            ||
| 69 | 'cache' => Cache::class,  | 
            ||
| 70 | 'config' => Config::class,  | 
            ||
| 71 | 'console' => Console::class,  | 
            ||
| 72 | 'cookie' => Cookie::class,  | 
            ||
| 73 | 'db' => Db::class,  | 
            ||
| 74 | 'debug' => Debug::class,  | 
            ||
| 75 | 'env' => Env::class,  | 
            ||
| 76 | 'event' => Event::class,  | 
            ||
| 77 | 'http' => Http::class,  | 
            ||
| 78 | 'lang' => Lang::class,  | 
            ||
| 79 | 'log' => Log::class,  | 
            ||
| 80 | 'middleware' => Middleware::class,  | 
            ||
| 81 | 'request' => Request::class,  | 
            ||
| 82 | 'response' => Response::class,  | 
            ||
| 83 | 'route' => Route::class,  | 
            ||
| 84 | 'session' => Session::class,  | 
            ||
| 85 | 'validate' => Validate::class,  | 
            ||
| 86 | 'view' => View::class,  | 
            ||
| 87 | |||
| 88 | // 接口依赖注入  | 
            ||
| 89 | 'Psr\Log\LoggerInterface' => Log::class,  | 
            ||
| 90 | ];  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * 获取当前容器的实例(单例)  | 
            ||
| 94 | * @access public  | 
            ||
| 95 | * @return static  | 
            ||
| 96 | */  | 
            ||
| 97 | 3 | public static function getInstance()  | 
            |
| 98 |     { | 
            ||
| 99 | 3 |         if (is_null(static::$instance)) { | 
            |
| 100 | 1 | static::$instance = new static;  | 
            |
| 101 | }  | 
            ||
| 102 | |||
| 103 | 3 |         if (static::$instance instanceof Closure) { | 
            |
| 104 | 1 | return (static::$instance)();  | 
            |
| 105 | }  | 
            ||
| 106 | |||
| 107 | 3 | return static::$instance;  | 
            |
| 108 | }  | 
            ||
| 109 | |||
| 110 | /**  | 
            ||
| 111 | * 设置当前容器的实例  | 
            ||
| 112 | * @access public  | 
            ||
| 113 | * @param object|Closure $instance  | 
            ||
| 
                                                                                                                                                        
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 114 | * @return void  | 
            ||
| 115 | */  | 
            ||
| 116 | 29 | public static function setInstance($instance): void  | 
            |
| 119 | 29 | }  | 
            |
| 120 | |||
| 121 | /**  | 
            ||
| 122 | * 获取容器中的对象实例 不存在则创建  | 
            ||
| 123 | * @access public  | 
            ||
| 124 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 125 | * @param array|true $vars 变量  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 126 | * @param bool $newInstance 是否每次创建新的实例  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 127 | * @return object  | 
            ||
| 128 | */  | 
            ||
| 129 | 1 | public static function pull(string $abstract, array $vars = [], bool $newInstance = false)  | 
            |
| 132 | }  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * 获取容器中的对象实例  | 
            ||
| 136 | * @access public  | 
            ||
| 137 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 138 | * @return object  | 
            ||
| 139 | */  | 
            ||
| 140 | 12 | public function get($abstract)  | 
            |
| 147 | }  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * 绑定一个类、闭包、实例、接口实现到容器  | 
            ||
| 151 | * @access public  | 
            ||
| 152 | * @param string|array $abstract 类标识、接口  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 153 | * @param mixed $concrete 要绑定的类、闭包或者实例  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 154 | * @return $this  | 
            ||
| 155 | */  | 
            ||
| 156 | 9 | public function bind($abstract, $concrete = null)  | 
            |
| 169 | }  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * 绑定一个类实例到容器  | 
            ||
| 173 | * @access public  | 
            ||
| 174 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 175 | * @param object $instance 类的实例  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 176 | * @return $this  | 
            ||
| 177 | */  | 
            ||
| 178 | 22 | public function instance(string $abstract, $instance)  | 
            |
| 179 |     { | 
            ||
| 180 | 22 |         if (isset($this->bind[$abstract])) { | 
            |
| 181 | 19 | $bind = $this->bind[$abstract];  | 
            |
| 182 | |||
| 183 | 19 |             if (is_string($bind)) { | 
            |
| 184 | 19 | return $this->instance($bind, $instance);  | 
            |
| 185 | }  | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | 22 | $this->instances[$abstract] = $instance;  | 
            |
| 189 | |||
| 190 | 22 | return $this;  | 
            |
| 191 | }  | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * 判断容器中是否存在类及标识  | 
            ||
| 195 | * @access public  | 
            ||
| 196 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 197 | * @return bool  | 
            ||
| 198 | */  | 
            ||
| 199 | 12 | public function bound(string $abstract): bool  | 
            |
| 202 | }  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | * 判断容器中是否存在类及标识  | 
            ||
| 206 | * @access public  | 
            ||
| 207 | * @param string $name 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 208 | * @return bool  | 
            ||
| 209 | */  | 
            ||
| 210 | 12 | public function has($name): bool  | 
            |
| 213 | }  | 
            ||
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * 判断容器中是否存在对象实例  | 
            ||
| 217 | * @access public  | 
            ||
| 218 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 219 | * @return bool  | 
            ||
| 220 | */  | 
            ||
| 221 | 4 | public function exists(string $abstract): bool  | 
            |
| 222 |     { | 
            ||
| 223 | 4 |         if (isset($this->bind[$abstract])) { | 
            |
| 224 | 3 | $bind = $this->bind[$abstract];  | 
            |
| 225 | |||
| 226 | 3 |             if (is_string($bind)) { | 
            |
| 227 | 2 | return $this->exists($bind);  | 
            |
| 228 | }  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | 4 | return isset($this->instances[$abstract]);  | 
            |
| 232 | }  | 
            ||
| 233 | |||
| 234 | /**  | 
            ||
| 235 | * 创建类的实例 已经存在则直接获取  | 
            ||
| 236 | * @access public  | 
            ||
| 237 | * @param string $abstract 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 238 | * @param array $vars 变量  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 239 | * @param bool $newInstance 是否每次创建新的实例  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 240 | * @return mixed  | 
            ||
| 241 | */  | 
            ||
| 242 | 16 | public function make(string $abstract, array $vars = [], bool $newInstance = false)  | 
            |
| 265 | }  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * 删除容器中的对象实例  | 
            ||
| 269 | * @access public  | 
            ||
| 270 | * @param string $name 类名或者标识  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 271 | * @return void  | 
            ||
| 272 | */  | 
            ||
| 273 | 2 | public function delete($name)  | 
            |
| 285 | }  | 
            ||
| 286 | 2 | }  | 
            |
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * 执行函数或者闭包方法 支持参数调用  | 
            ||
| 290 | * @access public  | 
            ||
| 291 | * @param mixed $function 函数或者闭包  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 292 | * @param array $vars 参数  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 293 | 1 | * @return mixed  | 
            |
| 294 | */  | 
            ||
| 295 | 1 | public function invokeFunction($function, array $vars = [])  | 
            |
| 296 |     { | 
            ||
| 297 |         try { | 
            ||
| 298 | $reflect = new ReflectionFunction($function);  | 
            ||
| 299 | |||
| 300 | $args = $this->bindParams($reflect, $vars);  | 
            ||
| 301 | |||
| 302 | return call_user_func_array($function, $args);  | 
            ||
| 303 | 1 |         } catch (ReflectionException $e) { | 
            |
| 304 |             throw new Exception('function not exists: ' . $function . '()'); | 
            ||
| 305 | 1 | }  | 
            |
| 306 | 1 | }  | 
            |
| 307 | |||
| 308 | 1 | /**  | 
            |
| 309 | * 调用反射执行类的方法 支持参数绑定  | 
            ||
| 310 | * @access public  | 
            ||
| 311 | * @param mixed $method 方法  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 312 | * @param array $vars 参数  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 313 | * @return mixed  | 
            ||
| 314 | */  | 
            ||
| 315 | public function invokeMethod($method, array $vars = [])  | 
            ||
| 316 |     { | 
            ||
| 317 | 5 |         try { | 
            |
| 318 |             if (is_array($method)) { | 
            ||
| 319 | $class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);  | 
            ||
| 320 | 5 | $reflect = new ReflectionMethod($class, $method[1]);  | 
            |
| 321 |             } else { | 
            ||
| 322 | 4 | // 静态方法  | 
            |
| 323 | $reflect = new ReflectionMethod($method);  | 
            ||
| 324 | 4 | }  | 
            |
| 325 | 1 | ||
| 326 | 1 | $args = $this->bindParams($reflect, $vars);  | 
            |
| 327 | |||
| 328 | return $reflect->invokeArgs($class ?? null, $args);  | 
            ||
| 329 |         } catch (ReflectionException $e) { | 
            ||
| 330 |             if (is_array($method)) { | 
            ||
| 331 | $class = is_object($method[0]) ? get_class($method[0]) : $method[0];  | 
            ||
| 332 | $callback = $class . '::' . $method[1];  | 
            ||
| 333 |             } else { | 
            ||
| 334 | $callback = $method;  | 
            ||
| 335 | }  | 
            ||
| 336 | |||
| 337 | 3 |             throw new Exception('method not exists: ' . $callback . '()'); | 
            |
| 338 | }  | 
            ||
| 339 | }  | 
            ||
| 340 | 3 | ||
| 341 | 3 | /**  | 
            |
| 342 | 3 | * 调用反射执行类的方法 支持参数绑定  | 
            |
| 343 | * @access public  | 
            ||
| 344 | * @param object $instance 对象实例  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 345 | 1 | * @param mixed $reflect 反射类  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 346 | * @param array $vars 参数  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 347 | * @return mixed  | 
            ||
| 348 | 2 | */  | 
            |
| 349 | public function invokeReflectMethod($instance, $reflect, array $vars = [])  | 
            ||
| 350 | 2 |     { | 
            |
| 351 | 1 | $args = $this->bindParams($reflect, $vars);  | 
            |
| 352 | 1 | ||
| 353 | 1 | return $reflect->invokeArgs($instance, $args);  | 
            |
| 354 | 1 | }  | 
            |
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * 调用反射执行callable 支持参数绑定  | 
            ||
| 358 | * @access public  | 
            ||
| 359 | 1 | * @param mixed $callable  | 
            |
| 
                                                                                                                                                        
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 360 | * @param array $vars 参数  | 
            ||
| 
                                                                                                                                                        
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 361 | * @return mixed  | 
            ||
| 362 | */  | 
            ||
| 363 | public function invoke($callable, array $vars = [])  | 
            ||
| 364 |     { | 
            ||
| 365 |         if ($callable instanceof Closure) { | 
            ||
| 366 | return $this->invokeFunction($callable, $vars);  | 
            ||
| 367 | }  | 
            ||
| 368 | |||
| 369 | return $this->invokeMethod($callable, $vars);  | 
            ||
| 370 | }  | 
            ||
| 371 | 1 | ||
| 372 | /**  | 
            ||
| 373 | 1 | * 调用反射执行类的实例化 支持依赖注入  | 
            |
| 374 | * @access public  | 
            ||
| 375 | 1 | * @param string $class 类名  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 376 | * @param array $vars 参数  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 377 | * @return mixed  | 
            ||
| 378 | */  | 
            ||
| 379 | public function invokeClass(string $class, array $vars = [])  | 
            ||
| 380 |     { | 
            ||
| 381 |         try { | 
            ||
| 382 | $reflect = new ReflectionClass($class);  | 
            ||
| 383 | |||
| 384 |             if ($reflect->hasMethod('__make')) { | 
            ||
| 385 | 2 | $method = new ReflectionMethod($class, '__make');  | 
            |
| 386 | |||
| 387 | 2 |                 if ($method->isPublic() && $method->isStatic()) { | 
            |
| 388 | 1 | $args = $this->bindParams($method, $vars);  | 
            |
| 389 | return $method->invokeArgs(null, $args);  | 
            ||
| 390 | }  | 
            ||
| 391 | 2 | }  | 
            |
| 392 | |||
| 393 | $constructor = $reflect->getConstructor();  | 
            ||
| 394 | |||
| 395 | $args = $constructor ? $this->bindParams($constructor, $vars) : [];  | 
            ||
| 396 | |||
| 397 | return $reflect->newInstanceArgs($args);  | 
            ||
| 398 |         } catch (ReflectionException $e) { | 
            ||
| 399 |             throw new ClassNotFoundException('class not exists: ' . $class, $class); | 
            ||
| 400 | }  | 
            ||
| 401 | 15 | }  | 
            |
| 402 | |||
| 403 | /**  | 
            ||
| 404 | 15 | * 绑定参数  | 
            |
| 405 | * @access protected  | 
            ||
| 406 | 14 | * @param \ReflectionMethod|\ReflectionFunction $reflect 反射类  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 407 | 11 | * @param array $vars 参数  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 408 | * @return array  | 
            ||
| 409 | 11 | */  | 
            |
| 410 | 11 | protected function bindParams($reflect, array $vars = []): array  | 
            |
| 411 | 11 |     { | 
            |
| 412 |         if ($reflect->getNumberOfParameters() == 0) { | 
            ||
| 413 | return [];  | 
            ||
| 414 | }  | 
            ||
| 415 | 11 | ||
| 416 | // 判断数组类型 数字数组时按顺序绑定参数  | 
            ||
| 417 | 11 | reset($vars);  | 
            |
| 418 | $type = key($vars) === 0 ? 1 : 0;  | 
            ||
| 419 | 11 | $params = $reflect->getParameters();  | 
            |
| 420 | 1 | $args = [];  | 
            |
| 421 | 1 | ||
| 422 |         foreach ($params as $param) { | 
            ||
| 423 | $name = $param->getName();  | 
            ||
| 424 | $lowerName = App::parseName($name);  | 
            ||
| 425 | $class = $param->getClass();  | 
            ||
| 426 | |||
| 427 |             if ($class) { | 
            ||
| 428 | $args[] = $this->getObjectParam($class->getName(), $vars);  | 
            ||
| 429 |             } elseif (1 == $type && !empty($vars)) { | 
            ||
| 430 | $args[] = array_shift($vars);  | 
            ||
| 431 |             } elseif (0 == $type && isset($vars[$name])) { | 
            ||
| 432 | 17 | $args[] = $vars[$name];  | 
            |
| 433 |             } elseif (0 == $type && isset($vars[$lowerName])) { | 
            ||
| 434 | 17 | $args[] = $vars[$lowerName];  | 
            |
| 435 | 6 |             } elseif ($param->isDefaultValueAvailable()) { | 
            |
| 436 | $args[] = $param->getDefaultValue();  | 
            ||
| 437 |             } else { | 
            ||
| 438 |                 throw new InvalidArgumentException('method param miss:' . $name); | 
            ||
| 439 | 11 | }  | 
            |
| 440 | 11 | }  | 
            |
| 441 | 11 | ||
| 442 | 11 | return $args;  | 
            |
| 443 | }  | 
            ||
| 444 | 11 | ||
| 445 | 11 | /**  | 
            |
| 446 | 11 | * 获取对象类型的参数值  | 
            |
| 447 | 11 | * @access protected  | 
            |
| 448 | * @param string $className 类名  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 449 | 11 | * @param array $vars 参数  | 
            |
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 450 | 11 | * @return mixed  | 
            |
| 451 | 9 | */  | 
            |
| 452 | 1 | protected function getObjectParam(string $className, array &$vars)  | 
            |
| 453 | 9 |     { | 
            |
| 454 | 1 | $array = $vars;  | 
            |
| 455 | 9 | $value = array_shift($array);  | 
            |
| 456 | 1 | ||
| 457 | 9 |         if ($value instanceof $className) { | 
            |
| 458 | 9 | $result = $value;  | 
            |
| 459 | array_shift($vars);  | 
            ||
| 460 |         } else { | 
            ||
| 461 | $result = $this->make($className);  | 
            ||
| 462 | }  | 
            ||
| 463 | |||
| 464 | 11 | return $result;  | 
            |
| 465 | }  | 
            ||
| 466 | |||
| 467 | public function __set($name, $value)  | 
            ||
| 470 | }  | 
            ||
| 471 | |||
| 472 | public function __get($name)  | 
            ||
| 473 |     { | 
            ||
| 474 | 11 | return $this->get($name);  | 
            |
| 475 | }  | 
            ||
| 476 | 11 | ||
| 477 | 11 | public function __isset($name): bool  | 
            |
| 480 | 1 | }  | 
            |
| 481 | 1 | ||
| 482 | public function __unset($name)  | 
            ||
| 485 | }  | 
            ||
| 486 | 11 | ||
| 487 | public function offsetExists($key)  | 
            ||
| 490 | }  | 
            ||
| 491 | 1 | ||
| 492 | 1 | public function offsetGet($key)  | 
            |
| 495 | }  | 
            ||
| 496 | 11 | ||
| 497 | public function offsetSet($key, $value)  | 
            ||
| 498 |     { | 
            ||
| 499 | 1 | $this->bind($key, $value);  | 
            |
| 500 | }  | 
            ||
| 501 | 1 | ||
| 502 | public function offsetUnset($key)  | 
            ||
| 503 |     { | 
            ||
| 504 | 2 | $this->delete($key);  | 
            |
| 505 | }  | 
            ||
| 506 | 2 | ||
| 507 | 2 | //Countable  | 
            |
| 508 | public function count()  | 
            ||
| 509 | 1 |     { | 
            |
| 510 | return count($this->instances);  | 
            ||
| 511 | 1 | }  | 
            |
| 512 | |||
| 513 | //IteratorAggregate  | 
            ||
| 514 | 1 | public function getIterator()  | 
            |
| 517 | }  | 
            ||
| 518 | }  | 
            ||
| 519 |