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) |
||
213 | { |
||
214 | $calls = []; |
||
215 | |||
216 | if (!is_array($params[0])) { |
||
217 | $call = [ $params[0] ]; |
||
218 | unset($params[0]); |
||
219 | |||
220 | if (!empty($params[1])) { |
||
221 | $call[] = $params[1]; |
||
222 | unset($params[1]); |
||
223 | } |
||
224 | |||
225 | $params[] = $call; |
||
226 | } |
||
227 | |||
228 | foreach ($params as $arguments) { |
||
229 | if (is_string($arguments[0])) { |
||
230 | if (!empty($arguments[1]) && is_array($arguments[1])) { |
||
231 | $calls[$arguments[0]] = $this->buildParams($arguments[1]); |
||
232 | } else { |
||
233 | $calls[$arguments[0]] = null; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return $calls; |
||
239 | } |
||
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 |