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 $instances; |
||
| 18 | private $rules; |
||
| 19 | private $factories; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Construct a new instance of the {@link Container} class. |
||
| 23 | */ |
||
| 24 | 47 | public function __construct() { |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Normalize a container entry ID. |
||
| 33 | * |
||
| 34 | * @param string $id The ID to normalize. |
||
| 35 | * @return string Returns a normalized ID as a string. |
||
| 36 | */ |
||
| 37 | 45 | private function normalizeID($id) { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Set the current rule to the default rule. |
||
| 43 | * |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | 1 | public function defaultRule() { |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Set the current rule. |
||
| 53 | * |
||
| 54 | * @param string $id The ID of the rule. |
||
| 55 | * @return $this |
||
| 56 | */ |
||
| 57 | 18 | public function rule($id) { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Get the class name of the current rule. |
||
| 69 | * |
||
| 70 | * @return string Returns a class name. |
||
| 71 | */ |
||
| 72 | 2 | public function getClass() { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Set the name of the class for the current rule. |
||
| 78 | * |
||
| 79 | * @param string $value A valid class name. |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | 4 | public function setClass($value) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Get the factory callback for the current rule. |
||
| 89 | * |
||
| 90 | * @return callable|null Returns the rule's factory or **null** if it has none. |
||
| 91 | */ |
||
| 92 | 2 | public function getFactory() { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Set the factory that will be used to create the instance for the current rule. |
||
| 98 | * |
||
| 99 | * @param callable $value This callback will be called to create the instance for the rule. |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 10 | public function setFactory(callable $value) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Whether or not the current rule is shared. |
||
| 109 | * |
||
| 110 | * @return bool Returns **true** if the rule is shared or **false** otherwise. |
||
| 111 | */ |
||
| 112 | 2 | public function isShared() { |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Set whether or not the current rule is shared. |
||
| 118 | * |
||
| 119 | * @param bool $value Whether or not the current rule is shared. |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | 17 | public function setShared($value) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Whether or not the current rule will inherit to subclasses. |
||
| 129 | * |
||
| 130 | * @return bool Returns **true** if the current rule inherits or **false** otherwise. |
||
| 131 | */ |
||
| 132 | 2 | public function getInherit() { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Set whether or not the current rule extends to subclasses. |
||
| 138 | * |
||
| 139 | * @param bool $value Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | 3 | public function setInherit($value) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get the constructor arguments for the current rule. |
||
| 149 | * |
||
| 150 | * @return array Returns the constructor arguments for the current rule. |
||
| 151 | */ |
||
| 152 | 2 | public function getConstructorArgs() { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Set the constructor arguments for the current rule. |
||
| 158 | * |
||
| 159 | * @param array $args An array of constructor arguments. |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | 15 | public function setConstructorArgs(array $args) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set a specific shared instance into the container. |
||
| 169 | * |
||
| 170 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
| 171 | * rule is configured that says that instances should not be shared. |
||
| 172 | * |
||
| 173 | * @param string $name The name of the container entry. |
||
| 174 | * @param mixed $instance This instance. |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | 5 | public function setInstance($name, $instance) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Add a method call to a rule. |
||
| 184 | * |
||
| 185 | * @param string $method The name of the method to call. |
||
| 186 | * @param array $args The arguments to pass to the method. |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | 6 | public function addCall($method, array $args = []) { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Finds an entry of the container by its identifier and returns it. |
||
| 197 | * |
||
| 198 | * @param string $id Identifier of the entry to look for. |
||
| 199 | * @param array $args Additional arguments to pass to the constructor. |
||
| 200 | * |
||
| 201 | * @throws NotFoundException No entry was found for this identifier. |
||
| 202 | * @throws ContainerException Error while retrieving the entry. |
||
| 203 | * |
||
| 204 | * @return mixed Entry. |
||
| 205 | */ |
||
| 206 | 41 | public function getArgs($id, array $args = []) { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Make a rule based on an ID. |
||
| 226 | * |
||
| 227 | * @param string $nid A normalized ID. |
||
| 228 | * @return array Returns an array representing a rule. |
||
| 229 | */ |
||
| 230 | 37 | private function makeRule($nid) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Make a function that creates objects from a rule. |
||
| 279 | * |
||
| 280 | * @param string $nid The normalized ID of the container item. |
||
| 281 | * @param array $rule The resolved rule for the ID. |
||
| 282 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
| 283 | * @throws NotFoundException No entry was found for this identifier. |
||
| 284 | */ |
||
| 285 | 29 | private function makeFactory($nid, array $rule) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Create a shared instance of a class from a rule. |
||
| 358 | * |
||
| 359 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
| 360 | * |
||
| 361 | * @param string $nid The normalized ID of the container item. |
||
| 362 | * @param array $rule The resolved rule for the ID. |
||
| 363 | * @param array $args Additional arguments passed during creation. |
||
| 364 | * @return object Returns the the new instance. |
||
| 365 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
| 366 | */ |
||
| 367 | 9 | private function createSharedInstance($nid, array $rule, array $args) { |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Make an array of default arguments for a given function. |
||
| 433 | * |
||
| 434 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
| 435 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
| 436 | * @param array $rule The entire rule. |
||
| 437 | * @return array Returns an array in the form `name => defaultValue`. |
||
| 438 | */ |
||
| 439 | 30 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Replace an array of default args with called args. |
||
| 468 | * |
||
| 469 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
| 470 | * @param array $args The arguments passed into a creation. |
||
| 471 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
| 472 | * @return array Returns an array suitable to be applied to a function call. |
||
| 473 | */ |
||
| 474 | 30 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Create an instance of a container item. |
||
| 502 | * |
||
| 503 | * This method either creates a new instance or returns an already created shared instance. |
||
| 504 | * |
||
| 505 | * @param string $nid The normalized ID of the container item. |
||
| 506 | * @param array $args Additional arguments to pass to the constructor. |
||
| 507 | * @return object Returns an object instance. |
||
| 508 | */ |
||
| 509 | 37 | private function createInstance($nid, array $args) { |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Call a callback with argument injection. |
||
| 525 | * |
||
| 526 | * @param callable $callback The callback to call. |
||
| 527 | * @param array $args Additional arguments to pass to the callback. |
||
| 528 | * @return mixed Returns the result of the callback. |
||
| 529 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
| 530 | */ |
||
| 531 | 1 | public function call(callable $callback, array $args = []) { |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
| 552 | * |
||
| 553 | * @param string $id Identifier of the entry to look for. |
||
| 554 | * |
||
| 555 | * @return boolean |
||
| 556 | */ |
||
| 557 | public function has($id) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Determines whether a rule has been defined at a given ID. |
||
| 565 | * |
||
| 566 | * @param string $id Identifier of the entry to look for. |
||
| 567 | * @return bool Returns **true** if a rule has been defined or **false** otherwise. |
||
| 568 | */ |
||
| 569 | 4 | public function hasRule($id) { |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Finds an entry of the container by its identifier and returns it. |
||
| 576 | * |
||
| 577 | * @param string $id Identifier of the entry to look for. |
||
| 578 | * |
||
| 579 | * @throws NotFoundException No entry was found for this identifier. |
||
| 580 | * @throws ContainerException Error while retrieving the entry. |
||
| 581 | * |
||
| 582 | * @return mixed Entry. |
||
| 583 | */ |
||
| 584 | 36 | public function get($id) { |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Determine the reflection information for a callback. |
||
| 590 | * |
||
| 591 | * @param callable $callback The callback to reflect. |
||
| 592 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
| 593 | */ |
||
| 594 | 9 | private function reflectCallback(callable $callback) { |
|
| 601 | } |
||
| 602 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.