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 | 87 | 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 | 2 | 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 | 88 | 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 | 87 | 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 | 8 | 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. You can add |
||
| 148 | * multiple aliases by passing additional arguments to this method. |
||
| 149 | * |
||
| 150 | * If {@link Container::addAlias()} is called with an alias that is the same as the current rule then an **E_USER_NOTICE** |
||
| 151 | * level error is raised and the alias is not added. |
||
| 152 | * |
||
| 153 | * @param string ...$alias The alias to set. |
||
| 154 | * @return $this |
||
| 155 | * @since 1.4 Added the ability to pass multiple aliases. |
||
| 156 | */ |
||
|
|
|||
| 157 | 8 | public function addAlias(...$alias) { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Remove an alias of the current rule. |
||
| 172 | * |
||
| 173 | * If {@link Container::removeAlias()} is called with an alias that references a different rule then an **E_USER_NOTICE** |
||
| 174 | * level error is raised, but the alias is still removed. |
||
| 175 | * |
||
| 176 | * @param string $alias The alias to remove. |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | 2 | public function removeAlias($alias) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get all of the aliases of the current rule. |
||
| 192 | * |
||
| 193 | * This method is intended to aid in debugging and should not be used in production as it walks the entire rule array. |
||
| 194 | * |
||
| 195 | * @return array Returns an array of strings representing aliases. |
||
| 196 | */ |
||
| 197 | 6 | public function getAliases() { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the factory callback for the current rule. |
||
| 211 | * |
||
| 212 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
| 213 | */ |
||
| 214 | 2 | public function getFactory() { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set the factory that will be used to create the instance for the current rule. |
||
| 220 | * |
||
| 221 | * @param callable $factory This callback will be called to create the instance for the rule. |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | 10 | public function setFactory(callable $factory) { |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Whether or not the current rule is shared. |
||
| 231 | * |
||
| 232 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
| 233 | */ |
||
| 234 | 2 | public function isShared() { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Set whether or not the current rule is shared. |
||
| 240 | * |
||
| 241 | * @param bool $shared Whether or not the current rule is shared. |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | 38 | public function setShared($shared) { |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Whether or not the current rule will inherit to subclasses. |
||
| 251 | * |
||
| 252 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
| 253 | */ |
||
| 254 | 2 | public function getInherit() { |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Set whether or not the current rule extends to subclasses. |
||
| 260 | * |
||
| 261 | * @param bool $inherit Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | 3 | public function setInherit($inherit) { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get the constructor arguments for the current rule. |
||
| 271 | * |
||
| 272 | * @return array Returns the constructor arguments for the current rule. |
||
| 273 | */ |
||
| 274 | 2 | public function getConstructorArgs() { |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set the constructor arguments for the current rule. |
||
| 280 | * |
||
| 281 | * @param array $args An array of constructor arguments. |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | 25 | public function setConstructorArgs(array $args) { |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set a specific shared instance into the container. |
||
| 291 | * |
||
| 292 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
| 293 | * rule is configured that says that instances should not be shared. |
||
| 294 | * |
||
| 295 | * @param string $name The name of the container entry. |
||
| 296 | * @param mixed $instance This instance. |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | 9 | public function setInstance($name, $instance) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Add a method call to a rule. |
||
| 306 | * |
||
| 307 | * @param string $method The name of the method to call. |
||
| 308 | * @param array $args The arguments to pass to the method. |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | 8 | public function addCall($method, array $args = []) { |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Finds an entry of the container by its identifier and returns it. |
||
| 319 | * |
||
| 320 | * @param string $id Identifier of the entry to look for. |
||
| 321 | * @param array $args Additional arguments to pass to the constructor. |
||
| 322 | * |
||
| 323 | * @throws NotFoundException No entry was found for this identifier. |
||
| 324 | * @throws ContainerException Error while retrieving the entry. |
||
| 325 | * |
||
| 326 | * @return mixed Entry. |
||
| 327 | */ |
||
| 328 | 68 | public function getArgs($id, array $args = []) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Make a rule based on an ID. |
||
| 353 | * |
||
| 354 | * @param string $nid A normalized ID. |
||
| 355 | * @return array Returns an array representing a rule. |
||
| 356 | */ |
||
| 357 | 64 | private function makeRule($nid) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Make a function that creates objects from a rule. |
||
| 410 | * |
||
| 411 | * @param string $nid The normalized ID of the container item. |
||
| 412 | * @param array $rule The resolved rule for the ID. |
||
| 413 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
| 414 | * @throws NotFoundException No entry was found for this identifier. |
||
| 415 | */ |
||
| 416 | 42 | private function makeFactory($nid, array $rule) { |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Create a shared instance of a class from a rule. |
||
| 489 | * |
||
| 490 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
| 491 | * |
||
| 492 | * @param string $nid The normalized ID of the container item. |
||
| 493 | * @param array $rule The resolved rule for the ID. |
||
| 494 | * @param array $args Additional arguments passed during creation. |
||
| 495 | * @return object Returns the the new instance. |
||
| 496 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
| 497 | */ |
||
| 498 | 24 | private function createSharedInstance($nid, array $rule, array $args) { |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Make an array of default arguments for a given function. |
||
| 563 | * |
||
| 564 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
| 565 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
| 566 | * @param array $rule The entire rule. |
||
| 567 | * @return array Returns an array in the form `name => defaultValue`. |
||
| 568 | */ |
||
| 569 | 57 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Replace an array of default args with called args. |
||
| 601 | * |
||
| 602 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
| 603 | * @param array $args The arguments passed into a creation. |
||
| 604 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
| 605 | * @return array Returns an array suitable to be applied to a function call. |
||
| 606 | * @throws MissingArgumentException Throws an exception when a required parameter is missing. |
||
| 607 | */ |
||
| 608 | 57 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Create an instance of a container item. |
||
| 643 | * |
||
| 644 | * This method either creates a new instance or returns an already created shared instance. |
||
| 645 | * |
||
| 646 | * @param string $nid The normalized ID of the container item. |
||
| 647 | * @param array $args Additional arguments to pass to the constructor. |
||
| 648 | * @return object Returns an object instance. |
||
| 649 | */ |
||
| 650 | 64 | private function createInstance($nid, array $args) { |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Call a callback with argument injection. |
||
| 666 | * |
||
| 667 | * @param callable $callback The callback to call. |
||
| 668 | * @param array $args Additional arguments to pass to the callback. |
||
| 669 | * @return mixed Returns the result of the callback. |
||
| 670 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
| 671 | */ |
||
| 672 | 3 | public function call(callable $callback, array $args = []) { |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
| 692 | * |
||
| 693 | * @param string $id Identifier of the entry to look for. |
||
| 694 | * |
||
| 695 | * @return boolean |
||
| 696 | */ |
||
| 697 | 5 | public function has($id) { |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Determines whether a rule has been defined at a given ID. |
||
| 705 | * |
||
| 706 | * @param string $id Identifier of the entry to look for. |
||
| 707 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
| 708 | */ |
||
| 709 | 4 | public function hasRule($id) { |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Finds an entry of the container by its identifier and returns it. |
||
| 716 | * |
||
| 717 | * @param string $id Identifier of the entry to look for. |
||
| 718 | * |
||
| 719 | * @throws NotFoundException No entry was found for this identifier. |
||
| 720 | * @throws ContainerException Error while retrieving the entry. |
||
| 721 | * |
||
| 722 | * @return mixed Entry. |
||
| 723 | */ |
||
| 724 | 56 | public function get($id) { |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Determine the reflection information for a callback. |
||
| 730 | * |
||
| 731 | * @param callable $callback The callback to reflect. |
||
| 732 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
| 733 | */ |
||
| 734 | 9 | private function reflectCallback(callable $callback) { |
|
| 741 | } |
||
| 742 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.