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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 13 | class Container implements ArrayAccess |
||
| 14 | { |
||
| 15 | // 容器对象实例 |
||
| 16 | protected static $instance; |
||
| 17 | // 容器中的对象实例 |
||
| 18 | protected $instances=[]; |
||
| 19 | // 容器中绑定的对象标识 |
||
| 20 | protected $bind=[]; |
||
| 21 | // 正则绑定 |
||
| 22 | protected $regexBind = []; |
||
| 23 | /** |
||
| 24 | * 获取当前容器的实例(单例) |
||
| 25 | * @access public |
||
| 26 | * @return \puck\Container |
||
| 27 | */ |
||
| 28 | 21 | public static function getInstance() |
|
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * 设置共享容器实例 |
||
| 40 | * @param ArrayAccess $container |
||
| 41 | * @return static |
||
| 42 | */ |
||
| 43 | public static function setInstance(ArrayAccess $container) |
||
| 47 | |||
| 48 | |||
| 49 | 2 | public function regexBind($regex, $class) { |
|
| 52 | |||
| 53 | /** |
||
| 54 | * 绑定一个类实例当容器 |
||
| 55 | * @access public |
||
| 56 | * @param string $abstract 类名或者标识 |
||
| 57 | * @param object $instance 类的实例 |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function instance($abstract, $instance) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * 调用反射执行callable 支持参数绑定 |
||
| 67 | * @access public |
||
| 68 | * @param mixed $callable |
||
| 69 | * @param array $vars 变量 |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function invoke($callable, $vars=[]) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * 执行函数或者闭包方法 支持参数调用 |
||
| 84 | * @access public |
||
| 85 | * @param \Closure $function 函数或者闭包 |
||
| 86 | * @param array $vars 变量 |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | public function invokeFunction($function, $vars = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * 绑定参数 |
||
| 98 | * @access protected |
||
| 99 | * @param \ReflectionMethod|\ReflectionFunction $reflect 反射类 |
||
| 100 | * @param array $vars 变量 |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | 3 | protected function bindParams($reflect, $vars=[]) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * 创建类的实例 |
||
| 133 | * @access public |
||
| 134 | * @param array $vars 变量 |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | 20 | public function make($abstract, $vars = [], $newInstance = false) { |
|
| 161 | |||
| 162 | 5 | private function makeObject($concrete, $vars = []) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * 调用反射执行类的实例化 支持依赖注入 |
||
| 173 | * @access public |
||
| 174 | * @param string $class 类名 |
||
| 175 | * @param array $vars 变量 |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | 5 | public function invokeClass($class, $vars = []) { |
|
| 198 | |||
| 199 | /** |
||
| 200 | * 调用反射执行类的方法 支持参数绑定 |
||
| 201 | * @access public |
||
| 202 | * @param string|array $method 方法 |
||
| 203 | * @param array $vars 变量 |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function invokeMethod($method, $vars = []) { |
||
| 217 | |||
| 218 | public function offsetExists($key) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * 判断容器中是否存在类及标识 |
||
| 225 | * @access public |
||
| 226 | * @param string $abstract 类名或者标识 |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function bound($abstract) { |
||
| 232 | |||
| 233 | public function offsetGet($key) |
||
| 237 | |||
| 238 | public function offsetSet($key, $value) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * 绑定一个类到容器 |
||
| 245 | * @access public |
||
| 246 | * @param string $abstract 类标识、接口 |
||
| 247 | * @param string|\Closure $concrete 要绑定的类或者闭包 |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function bind($abstract, $concrete = null) { |
||
| 257 | |||
| 258 | public function offsetUnset($key) |
||
| 262 | |||
| 263 | public function __unset($name) |
||
| 267 | |||
| 268 | public function __get($name) |
||
| 272 | |||
| 273 | public function __set($name, $value) { |
||
| 276 | |||
| 277 | public function __isset($name) |
||
| 281 | |||
| 282 | } |