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 | 63 | public function __construct($configurator = null) |
|
| 19 | |||
| 20 | 2 | public function __isset($name) |
|
| 24 | |||
| 25 | 3 | public function has($name) |
|
| 31 | |||
| 32 | 5 | public function __invoke($spec) |
|
| 33 | { |
||
| 34 | 5 | if (is_callable($spec)) { |
|
| 35 | 3 | if (is_array($spec)) { |
|
| 36 | 1 | list($class, $method) = $spec; |
|
| 37 | 1 | $class = new ReflectionClass($class); |
|
| 38 | 1 | $object = $class->newInstance(); |
|
| 39 | 1 | $mirror = $class->getMethod($method); |
|
| 40 | 1 | } else { |
|
| 41 | 2 | $object = false; |
|
| 42 | 2 | $mirror = new ReflectionFunction($spec); |
|
| 43 | } |
||
| 44 | 3 | $container = $this; |
|
| 45 | 3 | $arguments = array_map( |
|
| 46 | function ($param) use ($container) { |
||
| 47 | 3 | if ($paramClass = $param->getClass()) { |
|
| 48 | 3 | $paramClassName = $paramClass->getName(); |
|
| 49 | |||
| 50 | 3 | return $container->getItem($paramClassName); |
|
| 51 | } |
||
| 52 | 3 | }, |
|
| 53 | 3 | $mirror->getParameters() |
|
| 54 | 3 | ); |
|
| 55 | 3 | if ($object) { |
|
| 56 | 1 | return $mirror->invokeArgs($object, $arguments); |
|
| 57 | } |
||
| 58 | |||
| 59 | 2 | return $mirror->invokeArgs($arguments); |
|
| 60 | } |
||
| 61 | 4 | if ((bool) array_filter(func_get_args(), 'is_object')) { |
|
| 62 | 2 | foreach (func_get_args() as $dependency) { |
|
| 63 | 2 | $this[get_class($dependency)] = $dependency; |
|
| 64 | 2 | } |
|
| 65 | 2 | } |
|
| 66 | |||
| 67 | 4 | foreach ($spec as $name => $item) { |
|
| 68 | 4 | parent::offsetSet($name, $item); |
|
| 69 | 4 | } |
|
| 70 | |||
| 71 | 4 | $this->configure(); |
|
| 72 | |||
| 73 | 4 | return $this; |
|
| 74 | } |
||
| 75 | |||
| 76 | 1 | public function __call($name, $dict) |
|
| 82 | |||
| 83 | 60 | protected function configure() |
|
| 106 | |||
| 107 | 60 | public function getItem($name, $raw = false) |
|
| 121 | |||
| 122 | 4 | public function get($name) |
|
| 126 | |||
| 127 | 43 | public function loadString($configurator) |
|
| 137 | |||
| 138 | 1 | public function loadFile($configurator) |
|
| 147 | |||
| 148 | 52 | protected function state() |
|
| 154 | |||
| 155 | 52 | public function loadArray(array $configurator) |
|
| 156 | { |
||
| 157 | 52 | foreach ($this->state() + $configurator as $key => $value) { |
|
| 158 | 52 | if ($value instanceof \Closure) { |
|
| 159 | 1 | continue; |
|
| 160 | } |
||
| 161 | 52 | $this->parseItem($key, $value); |
|
| 162 | 51 | } |
|
| 163 | 47 | } |
|
| 164 | |||
| 165 | 45 | public function __get($name) |
|
| 169 | |||
| 170 | 7 | public function __set($name, $value) |
|
| 171 | { |
||
| 172 | 7 | if (isset($this[$name]) && $this[$name] instanceof Instantiator) { |
|
| 173 | $this[$name]->setInstance($value); |
||
| 174 | } |
||
| 175 | 7 | $this[$name] = $value; |
|
| 176 | 7 | } |
|
| 177 | |||
| 178 | 22 | protected function keyHasStateInstance($key, &$k) |
|
| 182 | |||
| 183 | 52 | protected function keyHasInstantiator($key) |
|
| 187 | |||
| 188 | 52 | protected function parseItem($key, $value) |
|
| 189 | { |
||
| 190 | 52 | $key = trim($key); |
|
| 191 | 52 | if ($this->keyHasInstantiator($key)) { |
|
| 192 | 22 | if ($this->keyHasStateInstance($key, $k)) { |
|
| 193 | 2 | $this->offsetSet($key, $this[$k]); |
|
| 194 | 2 | } else { |
|
| 195 | 22 | $this->parseInstantiator($key, $value); |
|
| 196 | } |
||
| 197 | 22 | } else { |
|
| 198 | 34 | $this->parseStandardItem($key, $value); |
|
| 199 | } |
||
| 200 | 51 | } |
|
| 201 | |||
| 202 | 15 | protected function parseSubValues(&$value) |
|
| 203 | { |
||
| 204 | 15 | foreach ($value as &$subValue) { |
|
| 205 | 15 | $subValue = $this->parseValue($subValue); |
|
| 206 | 15 | } |
|
| 207 | |||
| 208 | 15 | return $value; |
|
| 209 | } |
||
| 210 | |||
| 211 | 34 | protected function parseStandardItem($key, &$value) |
|
| 212 | { |
||
| 213 | 34 | if (is_array($value)) { |
|
| 214 | 3 | $this->parseSubValues($value); |
|
| 215 | 3 | } else { |
|
| 216 | 32 | $value = $this->parseValue($value); |
|
| 217 | } |
||
| 218 | |||
| 219 | 33 | $this->offsetSet($key, $value); |
|
| 220 | 33 | } |
|
| 221 | |||
| 222 | 22 | protected function removeDuplicatedSpaces($string) |
|
| 226 | |||
| 227 | 22 | protected function parseInstantiator($key, $value) |
|
| 228 | { |
||
| 229 | 22 | $key = $this->removeDuplicatedSpaces($key); |
|
| 230 | 22 | list($keyName, $keyClass) = explode(' ', $key, 2); |
|
| 231 | 22 | if ('instanceof' === $keyName) { |
|
| 232 | 2 | $keyName = $keyClass; |
|
| 233 | 2 | } |
|
| 234 | 22 | $instantiator = new Instantiator($keyClass); |
|
| 235 | |||
| 236 | 22 | if (is_array($value)) { |
|
| 237 | 21 | foreach ($value as $property => $pValue) { |
|
| 238 | 17 | $instantiator->setParam($property, $this->parseValue($pValue)); |
|
| 239 | 21 | } |
|
| 240 | 21 | } else { |
|
| 241 | 1 | $instantiator->setParam('__construct', $this->parseValue($value)); |
|
| 242 | } |
||
| 243 | |||
| 244 | 22 | $this->offsetSet($keyName, $instantiator); |
|
| 245 | 22 | } |
|
| 246 | |||
| 247 | 48 | protected function parseValue($value) |
|
| 248 | { |
||
| 249 | 48 | if ($value instanceof Instantiator) { |
|
| 250 | return $value; |
||
| 251 | 48 | } elseif (is_array($value)) { |
|
| 252 | 7 | return $this->parseSubValues($value); |
|
| 253 | 48 | } elseif (empty($value)) { |
|
| 254 | 8 | return null; |
|
| 255 | } else { |
||
| 256 | 46 | return $this->parseSingleValue($value); |
|
| 257 | } |
||
| 258 | } |
||
| 259 | 46 | protected function hasCompleteBrackets($value) |
|
| 263 | |||
| 264 | 46 | protected function parseSingleValue($value) |
|
| 277 | |||
| 278 | 38 | protected function parseConstants($value) |
|
| 286 | |||
| 287 | 27 | protected function matchSequence(&$value) |
|
| 293 | |||
| 294 | 21 | protected function matchReference(&$value) |
|
| 300 | |||
| 301 | 27 | protected function parseBrackets($value) |
|
| 311 | |||
| 312 | 1 | protected function parseVariables($value) |
|
| 324 | |||
| 325 | 9 | protected function parseArgumentList($value) |
|
| 331 | |||
| 332 | 21 | protected function lazyLoad($name) |
|
| 341 | } |
||
| 342 |
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 theSoncalls the wrong method in the parent class.