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) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Build calls arguments |
||
| 207 | * |
||
| 208 | * @access private |
||
| 209 | * @param array $params |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | private function buildCalls(array $params) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Build params from array |
||
| 243 | * |
||
| 244 | * @access private |
||
| 245 | * @param array $params |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | private function buildParams(array $params) |
||
| 266 | } |
||
| 267 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.