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 /** MicroContainer */ |
||
| 17 | class Container extends \stdClass implements IContainer |
||
| 18 | { |
||
| 19 | /** @var array $data data */ |
||
| 20 | protected $data = []; |
||
| 21 | /** @var array $config Configs */ |
||
| 22 | protected $config = []; |
||
| 23 | /** @var array $components Components config */ |
||
| 24 | protected $components = []; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Load more configs from file |
||
| 29 | * |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @param string $filename |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function load($filename) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Is set component or option name into Container |
||
| 48 | * |
||
| 49 | * @access public |
||
| 50 | * |
||
| 51 | * @param string $name Name attribute |
||
| 52 | * |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | public function __isset($name) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get Container value |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * |
||
| 75 | * @param string $name element name |
||
| 76 | * |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | public function __get($name = '') |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set attribute |
||
| 94 | * |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param string $name Name attribute |
||
| 98 | * @param mixed $component Component or option |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function __set($name, $component) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get component's |
||
| 109 | * |
||
| 110 | * @access public |
||
| 111 | * |
||
| 112 | * @param string|null $name name element to initialize |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function configure($name = null) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Load component |
||
| 145 | * |
||
| 146 | * @access public |
||
| 147 | * |
||
| 148 | * @param string $name component name |
||
| 149 | * @param array $options component configs |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function loadComponent($name, $options) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Make object with arguments |
||
| 197 | * |
||
| 198 | * @access private |
||
| 199 | * |
||
| 200 | * @param string $className |
||
| 201 | * @param array $arguments |
||
| 202 | * |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | private function makeObject($className, array $arguments = []) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Build calls arguments |
||
| 223 | * |
||
| 224 | * @access private |
||
| 225 | * @param array $params |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | private function buildCalls(array $params) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Build params from array |
||
| 253 | * |
||
| 254 | * @access private |
||
| 255 | * @param array $params |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | private function buildParams(array $params) |
||
| 276 | } |
||
| 277 |