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 | 36 | 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 | 35 | 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 | 9 | public function rule($id) { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Set the name of the class for the current rule. |
||
| 69 | * |
||
| 70 | * @param string $value A valid class name. |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | 1 | public function setClass($value) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Set the factory that will be used to create the instance for the current rule. |
||
| 80 | * |
||
| 81 | * @param callable $value This callback will be called to create the instance for the rule. |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | 9 | public function setFactory(callable $value) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set whether or not the current rule is shared. |
||
| 91 | * |
||
| 92 | * @param bool $value Whether or not the current rule is shared. |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | 12 | public function setShared($value) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Set whether or not the current rule extends to subclasses. |
||
| 102 | * |
||
| 103 | * @param bool $value Pass **true** to have subclasses inherit this rule or **false** otherwise. |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | 1 | public function setInherit($value) { |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Set the constructor arguments for the current rule. |
||
| 113 | * |
||
| 114 | * @param array $args An array of constructor arguments. |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | 8 | public function setConstructorArgs(array $args) { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Set a specific shared instance into the container. |
||
| 124 | * |
||
| 125 | * When you set an instance into the container then it will always be returned by subsequent retrievals, even if a |
||
| 126 | * rule is configured that says that instances should not be shared. |
||
| 127 | * |
||
| 128 | * @param string $name The name of the container entry. |
||
| 129 | * @param mixed $instance This instance. |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | 5 | public function setInstance($name, $instance) { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Add a method call to a rule. |
||
| 139 | * |
||
| 140 | * @param string $method The name of the method to call. |
||
| 141 | * @param array $args The arguments to pass to the method. |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | 6 | public function addCall($method, array $args = []) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Finds an entry of the container by its identifier and returns it. |
||
| 152 | * |
||
| 153 | * @param string $id Identifier of the entry to look for. |
||
| 154 | * @param array $args Additional arguments to pass to the constructor. |
||
| 155 | * |
||
| 156 | * @throws NotFoundException No entry was found for this identifier. |
||
| 157 | * @throws ContainerException Error while retrieving the entry. |
||
| 158 | * |
||
| 159 | * @return mixed Entry. |
||
| 160 | */ |
||
| 161 | 35 | public function getArgs($id, array $args = []) { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Make a rule based on an ID. |
||
| 181 | * |
||
| 182 | * @param string $nid A normalized ID. |
||
| 183 | * @return array Returns an array representing a rule. |
||
| 184 | */ |
||
| 185 | 31 | private function makeRule($nid) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Make a function that creates objects from a rule. |
||
| 225 | * |
||
| 226 | * @param string $nid The normalized ID of the container item. |
||
| 227 | * @param array $rule The resolved rule for the ID. |
||
| 228 | * @return \Closure Returns a function that when called will create a new instance of the class. |
||
| 229 | * @throws NotFoundException No entry was found for this identifier. |
||
| 230 | */ |
||
| 231 | 25 | private function makeFactory($nid, array $rule) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Create a shared instance of a class from a rule. |
||
| 304 | * |
||
| 305 | * This method has the side effect of adding the new instance to the internal instances array of this object. |
||
| 306 | * |
||
| 307 | * @param string $nid The normalized ID of the container item. |
||
| 308 | * @param array $rule The resolved rule for the ID. |
||
| 309 | * @param array $args Additional arguments passed during creation. |
||
| 310 | * @return object Returns the the new instance. |
||
| 311 | * @throws NotFoundException Throws an exception if the class does not exist. |
||
| 312 | */ |
||
| 313 | 7 | private function createSharedInstance($nid, array $rule, array $args) { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Make an array of default arguments for a given function. |
||
| 379 | * |
||
| 380 | * @param \ReflectionFunctionAbstract $function The function to make the arguments for. |
||
| 381 | * @param array $ruleArgs An array of default arguments specifically for the function. |
||
| 382 | * @param array $rule The entire rule. |
||
| 383 | * @return array Returns an array in the form `name => defaultValue`. |
||
| 384 | */ |
||
| 385 | 24 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Replace an array of default args with called args. |
||
| 414 | * |
||
| 415 | * @param array $defaultArgs The default arguments from {@link Container::makeDefaultArgs()}. |
||
| 416 | * @param array $args The arguments passed into a creation. |
||
| 417 | * @param mixed $instance An object instance if the arguments are being resolved on an already constructed object. |
||
| 418 | * @return array Returns an array suitable to be applied to a function call. |
||
| 419 | */ |
||
| 420 | 24 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Create an instance of a container item. |
||
| 448 | * |
||
| 449 | * This method either creates a new instance or returns an already created shared instance. |
||
| 450 | * |
||
| 451 | * @param string $nid The normalized ID of the container item. |
||
| 452 | * @param array $args Additional arguments to pass to the constructor. |
||
| 453 | * @return object Returns an object instance. |
||
| 454 | */ |
||
| 455 | 31 | private function createInstance($nid, array $args) { |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Call a callback with argument injection. |
||
| 471 | * |
||
| 472 | * @param callable $callback The callback to call. |
||
| 473 | * @param array $args Additional arguments to pass to the callback. |
||
| 474 | * @return mixed Returns the result of the callback. |
||
| 475 | * @throws ContainerException Throws an exception if the callback cannot be understood. |
||
| 476 | */ |
||
| 477 | 1 | public function call(callable $callback, array $args = []) { |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Returns true if the container can return an entry for the given identifier. Returns false otherwise. |
||
| 498 | * |
||
| 499 | * @param string $id Identifier of the entry to look for. |
||
| 500 | * |
||
| 501 | * @return boolean |
||
| 502 | */ |
||
| 503 | public function has($id) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Finds an entry of the container by its identifier and returns it. |
||
| 511 | * |
||
| 512 | * @param string $id Identifier of the entry to look for. |
||
| 513 | * |
||
| 514 | * @throws NotFoundException No entry was found for this identifier. |
||
| 515 | * @throws ContainerException Error while retrieving the entry. |
||
| 516 | * |
||
| 517 | * @return mixed Entry. |
||
| 518 | */ |
||
| 519 | 30 | public function get($id) { |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Determine the reflection information for a callback. |
||
| 525 | * |
||
| 526 | * @param callable $callback The callback to reflect. |
||
| 527 | * @return \ReflectionFunctionAbstract Returns the reflection function for the callback. |
||
| 528 | */ |
||
| 529 | 9 | private function reflectCallback(callable $callback) { |
|
| 536 | } |
||
| 537 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.