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 |
||
| 15 | class Container implements ContainerInterface { |
||
| 16 | private $currentRule; |
||
| 17 | private $currentRuleName; |
||
| 18 | private $instances; |
||
| 19 | private $rules; |
||
| 20 | private $factories; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Construct a new instance of the {@link Container} class. |
||
| 24 | */ |
||
| 25 | 67 | public function __construct() { |
|
| 26 | 67 | $this->rules = ['*' => ['inherit' => true, 'constructorArgs' => null]]; |
|
| 27 | 67 | $this->instances = []; |
|
| 28 | 67 | $this->factories = []; |
|
| 29 | |||
| 30 | $this->rule('*'); |
||
| 31 | 67 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Normalize a container entry ID. |
||
| 35 | * |
||
| 36 | * @param string $id The ID to normalize. |
||
| 37 | * @return string Returns a normalized ID as a string. |
||
| 38 | */ |
||
| 39 | private function normalizeID($id) { |
||
| 40 | return ltrim($id, '\\'); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Set the current rule to the default rule. |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | public function defaultRule() { |
||
| 49 | return $this->rule('*'); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set the current rule. |
||
| 54 | * |
||
| 55 | * @param string $id The ID of the rule. |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | 36 | public function rule($id) { |
|
| 59 | $id = $this->normalizeID($id); |
||
| 60 | |||
| 61 | 36 | if (!isset($this->rules[$id])) { |
|
| 62 | 26 | $this->rules[$id] = []; |
|
| 63 | } |
||
| 64 | 36 | $this->currentRuleName = $id; |
|
| 65 | 36 | $this->currentRule = &$this->rules[$id]; |
|
| 66 | |||
| 67 | 36 | return $this; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the class name of the current rule. |
||
| 72 | * |
||
| 73 | * @return string Returns a class name. |
||
| 74 | */ |
||
| 75 | 2 | public function getClass() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set the name of the class for the current rule. |
||
| 81 | * |
||
| 82 | * @param string $className A valid class name. |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 5 | public function setClass($className) { |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Get the rule that the current rule references. |
||
| 92 | * |
||
| 93 | * @return string Returns a reference name or an empty string if there is no reference. |
||
| 94 | */ |
||
| 95 | 3 | public function getAliasOf() { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Set the rule that the current rule is an alias of. |
||
| 101 | * |
||
| 102 | * @param string $alias The name of an entry in the container to point to. |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | 4 | public function setAliasOf($alias) { |
|
| 106 | $alias = $this->normalizeID($alias); |
||
| 107 | |||
| 108 | 4 | if ($alias === $this->currentRuleName) { |
|
| 109 | trigger_error("You cannot set alias '$alias' to itself.", E_USER_NOTICE); |
||
| 110 | } else { |
||
| 111 | 3 | $this->currentRule['aliasOf'] = $alias; |
|
| 112 | 1 | } |
|
| 113 | 4 | return $this; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Add an alias of the current rule. |
||
| 118 | * |
||
| 119 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
| 120 | * with the current rule. If the current rule is shared then the same shared instance will be returned. |
||
| 121 | * |
||
| 122 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
| 123 | * level error is raised and the alias is not added. |
||
| 124 | * |
||
| 125 | * @param string $alias The alias to set. |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | 6 | public function addAlias($alias) { |
|
| 129 | $alias = $this->normalizeID($alias); |
||
| 130 | |||
| 131 | 6 | if ($alias === $this->currentRuleName) { |
|
| 132 | trigger_error("Tried to set alias '$alias' to self.", E_USER_NOTICE); |
||
| 133 | } else { |
||
| 134 | 5 | $this->rules[$alias]['aliasOf'] = $this->currentRuleName; |
|
| 135 | 1 | } |
|
| 136 | 6 | return $this; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Remove an alias of the current rule. |
||
| 141 | * |
||
| 142 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
| 143 | * level error is raised, but the alias is still removed. |
||
| 144 | * |
||
| 145 | * @param string $alias The alias to remove. |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | 2 | public function removeAlias($alias) { |
|
| 149 | $alias = $this->normalizeID($alias); |
||
| 150 | |||
| 151 | 2 | if (!empty($this->rules[$alias]['aliasOf']) && $this->rules[$alias]['aliasOf'] !== $this->currentRuleName) { |
|
| 152 | trigger_error("Alias '$alias' does not point to the current rule.", E_USER_NOTICE); |
||
| 153 | } |
||
| 154 | |||
| 155 | 2 | unset($this->rules[$alias]['aliasOf']); |
|
| 156 | 2 | return $this; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get all of the aliases of the current rule. |
||
| 161 | * |
||
| 162 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
| 163 | * |
||
| 164 | * @return array Returns an array of strings representing aliases. |
||
| 165 | */ |
||
| 166 | 5 | public function getAliases() { |
|
| 167 | 5 | $result = []; |
|
| 168 | |||
| 169 | 5 | foreach ($this->rules as $name => $rule) { |
|
| 170 | 1 | if (!empty($rule['aliasOf']) && $rule['aliasOf'] === $this->currentRuleName) { |
|
| 171 | 3 | $result[] = $name; |
|
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | 5 | return $result; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the factory callback for the current rule. |
||
| 180 | * |
||
| 181 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
| 182 | */ |
||
| 183 | 2 | public function getFactory() { |
|
| 184 | 2 | return isset($this->currentRule['factory']) ? $this->currentRule['factory'] : null; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the factory that will be used to create the instance for the current rule. |
||
| 189 | * |
||
| 190 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | 10 | public function setFactory(callable $factory) { |
|
| 194 | 10 | $this->currentRule['factory'] = $factory; |
|
| 195 | 10 | return $this; |
|
| 196 | 10 | } |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Whether or not the current rule is shared. |
||
| 200 | * |
||
| 201 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
| 202 | */ |
||
| 203 | 2 | public function isShared() { |
|
| 204 | 2 | return !empty($this->currentRule['shared']); |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set whether or not the current rule is shared. |
||
| 209 | * |
||
| 210 | * @param bool $shared Whether or not the current rule is shared. |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | 22 | public function setShared($shared) { |
|
| 214 | 22 | $this->currentRule['shared'] = $shared; |
|
| 215 | 22 | return $this; |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Whether or not the current rule will inherit to subclasses. |
||
| 220 | * |
||
| 221 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
| 222 | */ |
||
| 223 | 2 | public function getInherit() { |
|
| 224 | 2 | return !empty($this->currentRule['inherit']); |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set whether or not the current rule extends to subclasses. |
||
| 229 | * |
||
| 230 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | 3 | public function setInherit($inherit) { |
|
| 234 | 3 | $this->currentRule['inherit'] = $inherit; |
|
| 235 | 3 | return $this; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the constructor arguments for the current rule. |
||
| 240 | * |
||
| 241 | * @return array Returns the constructor arguments for the current rule. |
||
| 242 | */ |
||
| 243 | 2 | public function getConstructorArgs() { |
|
| 244 | 2 | return empty($this->currentRule['constructorArgs']) ? [] : $this->currentRule['constructorArgs']; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set the constructor arguments for the current rule. |
||
| 249 | * |
||
| 250 | * @param array $args An array of constructor arguments. |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 14 | public function setConstructorArgs(array $args) { |
|
| 254 | 14 | $this->currentRule['constructorArgs'] = $args; |
|
| 255 | 14 | return $this; |
|
| 256 | 14 | } |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set a specific shared instance into the container. |
||
| 260 | * |
||
| 261 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
| 262 | * rule is configured that says that instances should not be shared. |
||
| 263 | * |
||
| 264 | * @param string $name The name of the container entry. |
||
| 265 | * @param mixed $instance This instance. |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | 4 | public function setInstance($name, $instance) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Add a method call to a rule. |
||
| 275 | * |
||
| 276 | * @param string $method The name of the method to call. |
||
| 277 | * @param array $args The arguments to pass to the method. |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | 3 | public function addCall($method, array $args = []) { |
|
| 281 | 3 | $this->currentRule['calls'][] = [$method, $args]; |
|
| 282 | |||
| 283 | 3 | return $this; |
|
| 284 | 3 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Finds an entry of the container by its identifier and returns it. |
||
| 288 | * |
||
| 289 | * @param string $id Identifier of the entry to look for. |
||
| 290 | * @param array $args Additional arguments to pass to the constructor. |
||
| 291 | * |
||
| 292 | * @throws NotFoundException No entry was found for this identifier. |
||
| 293 | * @throws ContainerException Error while retrieving the entry. |
||
| 294 | * |
||
| 295 | * @return mixed Entry. |
||
| 296 | */ |
||
| 297 | 41 | public function getArgs($id, array $args = []) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Make a rule based on an ID. |
||
| 322 | * |
||
| 323 | * @param string $nid A normalized ID. |
||
| 324 | * @return array Returns an array representing a rule. |
||
| 325 | */ |
||
| 326 | 43 | private function makeRule($nid) { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Make a function that creates objects from a rule. |
||
| 379 | * |
||
| 380 | * @param string $nid The normalized ID of the container item. |
||
| 381 | * @param array $rule The resolved rule for the ID. |
||
| 382 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
| 383 | * @throws NotFoundException No entry was found for this identifier. |
||
| 384 | */ |
||
| 385 | 30 | private function makeFactory($nid, array $rule) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Create a shared instance of a class from a rule. |
||
| 458 | * |
||
| 459 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
| 460 | * |
||
| 461 | * @param string $nid The normalized ID of the container item. |
||
| 462 | * @param array $rule The resolved rule for the ID. |
||
| 463 | * @param array $args Additional arguments passed during creation. |
||
| 464 | * @return object Returns the the new instance. |
||
|
|
|||
| 465 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
| 466 | */ |
||
| 467 | 15 | private function createSharedInstance($nid, array $rule, array $args) { |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Make an array of default arguments for a given function. |
||
| 532 | * |
||
| 533 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
| 534 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
| 535 | * @param array $rule The entire rule. |
||
| 536 | * @return array Returns an array in the form `name => defaultValue`. |
||
| 537 | */ |
||
| 538 | 29 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Replace an array of default args with called args. |
||
| 570 | * |
||
| 571 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
| 572 | * @param array $args The arguments passed into a creation. |
||
| 573 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
| 574 | * @return array Returns an array suitable to be applied to a function call. |
||
| 575 | */ |
||
| 576 | 27 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Create an instance of a container item. |
||
| 604 | * |
||
| 605 | * This method either creates a new instance or returns an already created shared instance. |
||
| 606 | * |
||
| 607 | * @param string $nid The normalized ID of the container item. |
||
| 608 | * @param array $args Additional arguments to pass to the constructor. |
||
| 609 | * @return object Returns an object instance. |
||
| 610 | */ |
||
| 611 | 43 | private function createInstance($nid, array $args) { |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Call a callback with argument injection. |
||
| 627 | * |
||
| 628 | * @param callable $callback The callback to call. |
||
| 629 | * @param array $args Additional arguments to pass to the callback. |
||
| 630 | * @return mixed Returns the result of the callback. |
||
| 631 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
| 632 | */ |
||
| 633 | 3 | public function call(callable $callback, array $args = []) { |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
| 653 | * |
||
| 654 | * @param string $id Identifier of the entry to look for. |
||
| 655 | * |
||
| 656 | * @return boolean |
||
| 657 | */ |
||
| 658 | 2 | public function has($id) { |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Determines whether a rule has been defined at a given ID. |
||
| 666 | * |
||
| 667 | * @param string $id Identifier of the entry to look for. |
||
| 668 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
| 669 | */ |
||
| 670 | 2 | public function hasRule($id) { |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Finds an entry of the container by its identifier and returns it. |
||
| 677 | * |
||
| 678 | * @param string $id Identifier of the entry to look for. |
||
| 679 | * |
||
| 680 | * @throws NotFoundException No entry was found for this identifier. |
||
| 681 | * @throws ContainerException Error while retrieving the entry. |
||
| 682 | * |
||
| 683 | * @return mixed Entry. |
||
| 684 | */ |
||
| 685 | 2 | public function get($id) { |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Determine the reflection information for a callback. |
||
| 691 | * |
||
| 692 | * @param callable $callback The callback to reflect. |
||
| 693 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
| 694 | */ |
||
| 695 | 9 | private function reflectCallback(callable $callback) { |
|
| 702 | } |
||
| 703 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.