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 |
||
11 | class Container extends ArrayObject implements ContainerInterface |
||
12 | { |
||
13 | protected $configurator; |
||
14 | |||
15 | 37 | public function __construct($configurator = null) |
|
19 | |||
20 | 2 | public function __isset($name) |
|
24 | |||
25 | 3 | public function has($name) |
|
26 | { |
||
27 | 3 | if ($this->configurator) { |
|
28 | 2 | $this->configure(); |
|
29 | 2 | } |
|
30 | |||
31 | 3 | return parent::offsetExists($name); |
|
|
|||
32 | } |
||
33 | |||
34 | 5 | public function __invoke($spec) |
|
35 | { |
||
36 | 5 | if (is_callable($spec)) { |
|
37 | 3 | if (is_array($spec)) { |
|
38 | 1 | list($class, $method) = $spec; |
|
39 | 1 | $class = new ReflectionClass($class); |
|
40 | 1 | $object = $class->newInstance(); |
|
41 | 1 | $mirror = $class->getMethod($method); |
|
42 | 1 | } else { |
|
43 | 2 | $object = false; |
|
44 | 2 | $mirror = new ReflectionFunction($spec); |
|
45 | } |
||
46 | 3 | $container = $this; |
|
47 | 3 | $arguments = array_map( |
|
48 | function ($param) use ($container) { |
||
49 | 3 | if ($paramClass = $param->getClass()) { |
|
50 | 3 | $paramClassName = $paramClass->getName(); |
|
51 | |||
52 | 3 | return $container->getItem($paramClassName); |
|
53 | } |
||
54 | 3 | }, |
|
55 | 3 | $mirror->getParameters() |
|
56 | 3 | ); |
|
57 | 3 | if ($object) { |
|
58 | 1 | return $mirror->invokeArgs($object, $arguments); |
|
59 | } |
||
60 | |||
61 | 2 | return $mirror->invokeArgs($arguments); |
|
62 | } |
||
63 | 4 | if ((bool) array_filter(func_get_args(), 'is_object')) { |
|
64 | 2 | foreach (func_get_args() as $dependency) { |
|
65 | 2 | $this[get_class($dependency)] = $dependency; |
|
66 | 2 | } |
|
67 | 2 | } |
|
68 | |||
69 | 4 | foreach ($spec as $name => $item) { |
|
70 | 4 | parent::offsetSet($name, $item); |
|
71 | 4 | } |
|
72 | |||
73 | 4 | if ($this->configurator) { |
|
74 | 2 | $this->configure(); |
|
75 | 2 | } |
|
76 | |||
77 | 4 | return $this; |
|
78 | } |
||
79 | |||
80 | 1 | public function __call($name, $dict) |
|
86 | |||
87 | 13 | protected function configure() |
|
110 | |||
111 | 37 | public function getItem($name, $raw = false) |
|
112 | { |
||
113 | 37 | if ($this->configurator) { |
|
114 | 9 | $this->configure(); |
|
115 | 7 | } |
|
116 | |||
117 | 35 | if (!isset($this[$name])) { |
|
118 | 1 | throw new NotFoundException("Item $name not found"); |
|
119 | } |
||
120 | |||
121 | 34 | if ($raw || !is_callable($this[$name])) { |
|
122 | 28 | return $this[$name]; |
|
123 | } |
||
124 | |||
125 | 13 | return $this->lazyLoad($name); |
|
126 | } |
||
127 | |||
128 | 2 | public function get($name) |
|
132 | |||
133 | 4 | public function loadString($configurator) |
|
142 | |||
143 | 1 | public function loadFile($configurator) |
|
152 | |||
153 | 32 | protected function state() |
|
159 | |||
160 | 32 | public function loadArray(array $configurator) |
|
161 | { |
||
162 | 32 | foreach ($this->state() + $configurator as $key => $value) { |
|
163 | 32 | if ($value instanceof \Closure) { |
|
164 | 1 | continue; |
|
165 | } |
||
166 | 32 | $this->parseItem($key, $value); |
|
167 | 32 | } |
|
168 | 32 | } |
|
169 | |||
170 | 17 | public function __get($name) |
|
174 | |||
175 | 5 | public function __set($name, $value) |
|
176 | { |
||
177 | 5 | if (isset($this[$name]) && $this[$name] instanceof Instantiator) { |
|
178 | 1 | $this[$name]->setInstance($value); |
|
179 | 1 | } |
|
180 | 5 | $this[$name] = $value; |
|
181 | 5 | } |
|
182 | |||
183 | 19 | protected function keyHasStateInstance($key, &$k) |
|
187 | |||
188 | 32 | protected function keyHasInstantiator($key) |
|
192 | |||
193 | 32 | protected function parseItem($key, $value) |
|
194 | { |
||
195 | 32 | $key = trim($key); |
|
196 | 32 | if ($this->keyHasInstantiator($key)) { |
|
197 | 19 | if ($this->keyHasStateInstance($key, $k)) { |
|
198 | 1 | $this->offsetSet($key, $this[$k]); |
|
199 | 1 | } else { |
|
200 | 19 | $this->parseInstantiator($key, $value); |
|
201 | } |
||
202 | 19 | } else { |
|
203 | 16 | $this->parseStandardItem($key, $value); |
|
204 | } |
||
205 | 32 | } |
|
206 | |||
207 | 9 | protected function parseSubValues(&$value) |
|
208 | { |
||
209 | 9 | foreach ($value as &$subValue) { |
|
210 | 9 | $subValue = $this->parseValue($subValue); |
|
211 | 9 | } |
|
212 | |||
213 | 9 | return $value; |
|
214 | } |
||
215 | |||
216 | 16 | protected function parseStandardItem($key, &$value) |
|
217 | { |
||
218 | 16 | if (is_array($value)) { |
|
219 | 2 | $this->parseSubValues($value); |
|
220 | 2 | } else { |
|
221 | 15 | $value = $this->parseValue($value); |
|
222 | } |
||
223 | |||
224 | 16 | $this->offsetSet($key, $value); |
|
225 | 16 | } |
|
226 | |||
227 | 19 | protected function removeDuplicatedSpaces($string) |
|
231 | |||
232 | 19 | protected function parseInstantiator($key, $value) |
|
233 | { |
||
234 | 19 | $key = $this->removeDuplicatedSpaces($key); |
|
235 | 19 | list($keyName, $keyClass) = explode(' ', $key, 2); |
|
236 | 19 | if ('instanceof' === $keyName) { |
|
237 | 1 | $keyName = $keyClass; |
|
238 | 1 | } |
|
239 | 19 | $instantiator = new Instantiator($keyClass); |
|
240 | |||
241 | 19 | if (is_array($value)) { |
|
242 | 18 | foreach ($value as $property => $pValue) { |
|
243 | 16 | $instantiator->setParam($property, $this->parseValue($pValue)); |
|
244 | 18 | } |
|
245 | 18 | } else { |
|
246 | 1 | $instantiator->setParam('__construct', $this->parseValue($value)); |
|
247 | } |
||
248 | |||
249 | 19 | $this->offsetSet($keyName, $instantiator); |
|
250 | 19 | } |
|
251 | |||
252 | 30 | protected function parseValue($value) |
|
253 | { |
||
254 | 30 | if ($value instanceof Instantiator) { |
|
255 | return $value; |
||
256 | 30 | } elseif (is_array($value)) { |
|
257 | 6 | return $this->parseSubValues($value); |
|
258 | 30 | } elseif (empty($value)) { |
|
259 | 5 | return null; |
|
260 | } else { |
||
261 | 28 | return $this->parseSingleValue($value); |
|
262 | } |
||
263 | } |
||
264 | 28 | protected function hasCompleteBrackets($value) |
|
268 | |||
269 | 28 | protected function parseSingleValue($value) |
|
278 | |||
279 | 24 | protected function parseConstants($value) |
|
287 | |||
288 | 14 | protected function matchSequence(&$value) |
|
294 | |||
295 | 12 | protected function matchReference(&$value) |
|
301 | |||
302 | 14 | protected function parseBrackets($value) |
|
312 | |||
313 | 1 | protected function parseVariables($value) |
|
325 | |||
326 | 4 | protected function parseArgumentList($value) |
|
332 | |||
333 | 13 | protected function lazyLoad($name) |
|
342 | } |
||
343 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.