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 | 73 | public function __construct() { |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Deep clone rules. |
||
| 35 | */ |
||
| 36 | 1 | public function __clone() { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Deep clone an array. |
||
| 43 | * |
||
| 44 | * @param array $array The array to clone. |
||
| 45 | * @return array Returns the cloned array. |
||
| 46 | * @see http://stackoverflow.com/a/17729234 |
||
| 47 | */ |
||
| 48 | 1 | private function arrayClone(array $array) { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Normalize a container entry ID. |
||
| 62 | * |
||
| 63 | * @param string $id The ID to normalize. |
||
| 64 | * @return string Returns a normalized ID as a string. |
||
| 65 | */ |
||
| 66 | 74 | private function normalizeID($id) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Set the current rule to the default rule. |
||
| 72 | * |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | 1 | public function defaultRule() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set the current rule. |
||
| 81 | * |
||
| 82 | * @param string $id The ID of the rule. |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 73 | public function rule($id) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get the class name of the current rule. |
||
| 99 | * |
||
| 100 | * @return string Returns a class name. |
||
| 101 | */ |
||
| 102 | 2 | public function getClass() { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Set the name of the class for the current rule. |
||
| 108 | * |
||
| 109 | * @param string $className A valid class name. |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | 6 | public function setClass($className) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get the rule that the current rule references. |
||
| 119 | * |
||
| 120 | * @return string Returns a reference name or an empty string if there is no reference. |
||
| 121 | */ |
||
| 122 | 3 | public function getAliasOf() { |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Set the rule that the current rule is an alias of. |
||
| 128 | * |
||
| 129 | * @param string $alias The name of an entry in the container to point to. |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | 4 | public function setAliasOf($alias) { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Add an alias of the current rule. |
||
| 145 | * |
||
| 146 | * Setting an alias to the current rule means that getting an item with the alias' name will be like getting the item |
||
| 147 | * with the current rule. If the current rule is shared then the same shared instance will be returned. |
||
| 148 | * |
||
| 149 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
| 150 | * level error is raised and the alias is not added. |
||
| 151 | * |
||
| 152 | * @param string $alias The alias to set. |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | 7 | public function addAlias($alias) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Remove an alias of the current rule. |
||
| 168 | * |
||
| 169 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
| 170 | * level error is raised, but the alias is still removed. |
||
| 171 | * |
||
| 172 | * @param string $alias The alias to remove. |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | 2 | public function removeAlias($alias) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get all of the aliases of the current rule. |
||
| 188 | * |
||
| 189 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
| 190 | * |
||
| 191 | * @return array Returns an array of strings representing aliases. |
||
| 192 | */ |
||
| 193 | 6 | public function getAliases() { |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get the factory callback for the current rule. |
||
| 207 | * |
||
| 208 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
| 209 | */ |
||
| 210 | 2 | public function getFactory() { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the factory that will be used to create the instance for the current rule. |
||
| 216 | * |
||
| 217 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | 10 | public function setFactory(callable $factory) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Whether or not the current rule is shared. |
||
| 227 | * |
||
| 228 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
| 229 | */ |
||
| 230 | 2 | public function isShared() { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Set whether or not the current rule is shared. |
||
| 236 | * |
||
| 237 | * @param bool $shared Whether or not the current rule is shared. |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | 26 | public function setShared($shared) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Whether or not the current rule will inherit to subclasses. |
||
| 247 | * |
||
| 248 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
| 249 | */ |
||
| 250 | 2 | public function getInherit() { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Set whether or not the current rule extends to subclasses. |
||
| 256 | * |
||
| 257 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | 3 | public function setInherit($inherit) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the constructor arguments for the current rule. |
||
| 267 | * |
||
| 268 | * @return array Returns the constructor arguments for the current rule. |
||
| 269 | */ |
||
| 270 | 2 | public function getConstructorArgs() { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Set the constructor arguments for the current rule. |
||
| 276 | * |
||
| 277 | * @param array $args An array of constructor arguments. |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | 18 | public function setConstructorArgs(array $args) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Set a specific shared instance into the container. |
||
| 287 | * |
||
| 288 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
| 289 | * rule is configured that says that instances should not be shared. |
||
| 290 | * |
||
| 291 | * @param string $name The name of the container entry. |
||
| 292 | * @param mixed $instance This instance. |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | 7 | public function setInstance($name, $instance) { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Add a method call to a rule. |
||
| 302 | * |
||
| 303 | * @param string $method The name of the method to call. |
||
| 304 | * @param array $args The arguments to pass to the method. |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | 6 | public function addCall($method, array $args = []) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Finds an entry of the container by its identifier and returns it. |
||
| 315 | * |
||
| 316 | * @param string $id Identifier of the entry to look for. |
||
| 317 | * @param array $args Additional arguments to pass to the constructor. |
||
| 318 | * |
||
| 319 | * @throws NotFoundException No entry was found for this identifier. |
||
| 320 | * @throws ContainerException Error while retrieving the entry. |
||
| 321 | * |
||
| 322 | * @return mixed Entry. |
||
| 323 | */ |
||
| 324 | 54 | public function getArgs($id, array $args = []) { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Make a rule based on an ID. |
||
| 349 | * |
||
| 350 | * @param string $nid A normalized ID. |
||
| 351 | * @return array Returns an array representing a rule. |
||
| 352 | */ |
||
| 353 | 50 | private function makeRule($nid) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Make a function that creates objects from a rule. |
||
| 406 | * |
||
| 407 | * @param string $nid The normalized ID of the container item. |
||
| 408 | * @param array $rule The resolved rule for the ID. |
||
| 409 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
| 410 | * @throws NotFoundException No entry was found for this identifier. |
||
| 411 | */ |
||
| 412 | 34 | private function makeFactory($nid, array $rule) { |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Create a shared instance of a class from a rule. |
||
| 485 | * |
||
| 486 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
| 487 | * |
||
| 488 | * @param string $nid The normalized ID of the container item. |
||
| 489 | * @param array $rule The resolved rule for the ID. |
||
| 490 | * @param array $args Additional arguments passed during creation. |
||
| 491 | * @return object Returns the the new instance. |
||
|
|
|||
| 492 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
| 493 | */ |
||
| 494 | 17 | private function createSharedInstance($nid, array $rule, array $args) { |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Make an array of default arguments for a given function. |
||
| 559 | * |
||
| 560 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
| 561 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
| 562 | * @param array $rule The entire rule. |
||
| 563 | * @return array Returns an array in the form `name => defaultValue`. |
||
| 564 | */ |
||
| 565 | 43 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Replace an array of default args with called args. |
||
| 597 | * |
||
| 598 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
| 599 | * @param array $args The arguments passed into a creation. |
||
| 600 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
| 601 | * @return array Returns an array suitable to be applied to a function call. |
||
| 602 | */ |
||
| 603 | 43 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Create an instance of a container item. |
||
| 631 | * |
||
| 632 | * This method either creates a new instance or returns an already created shared instance. |
||
| 633 | * |
||
| 634 | * @param string $nid The normalized ID of the container item. |
||
| 635 | * @param array $args Additional arguments to pass to the constructor. |
||
| 636 | * @return object Returns an object instance. |
||
| 637 | */ |
||
| 638 | 50 | private function createInstance($nid, array $args) { |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Call a callback with argument injection. |
||
| 654 | * |
||
| 655 | * @param callable $callback The callback to call. |
||
| 656 | * @param array $args Additional arguments to pass to the callback. |
||
| 657 | * @return mixed Returns the result of the callback. |
||
| 658 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
| 659 | */ |
||
| 660 | 3 | public function call(callable $callback, array $args = []) { |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
| 680 | * |
||
| 681 | * @param string $id Identifier of the entry to look for. |
||
| 682 | * |
||
| 683 | * @return boolean |
||
| 684 | */ |
||
| 685 | 5 | public function has($id) { |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Determines whether a rule has been defined at a given ID. |
||
| 693 | * |
||
| 694 | * @param string $id Identifier of the entry to look for. |
||
| 695 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
| 696 | */ |
||
| 697 | 4 | public function hasRule($id) { |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Finds an entry of the container by its identifier and returns it. |
||
| 704 | * |
||
| 705 | * @param string $id Identifier of the entry to look for. |
||
| 706 | * |
||
| 707 | * @throws NotFoundException No entry was found for this identifier. |
||
| 708 | * @throws ContainerException Error while retrieving the entry. |
||
| 709 | * |
||
| 710 | * @return mixed Entry. |
||
| 711 | */ |
||
| 712 | 49 | public function get($id) { |
|
| 715 | |||
| 716 | /** |
||
| 717 | * Determine the reflection information for a callback. |
||
| 718 | * |
||
| 719 | * @param callable $callback The callback to reflect. |
||
| 720 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
| 721 | */ |
||
| 722 | 9 | private function reflectCallback(callable $callback) { |
|
| 729 | } |
||
| 730 |
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.