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 | 71 | public function __construct() { |
|
| 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 | 72 | private function normalizeID($id) { |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Set the current rule to the default rule. |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | 1 | public function defaultRule() { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Set the current rule. |
||
| 54 | * |
||
| 55 | * @param string $id The ID of the rule. |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | 71 | public function rule($id) { |
|
| 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 | 6 | 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) { |
|
| 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 | 7 | public function addAlias($alias) { |
|
| 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) { |
|
| 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 | 6 | public function getAliases() { |
|
| 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() { |
|
| 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) { |
|
| 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() { |
|
| 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 | 24 | public function setShared($shared) { |
|
| 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() { |
|
| 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) { |
|
| 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() { |
|
| 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 | 16 | public function setConstructorArgs(array $args) { |
|
| 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 | 7 | 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 | 6 | public function addCall($method, array $args = []) { |
|
| 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 | 52 | 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 | 48 | 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 | 34 | 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 | 41 | private function makeDefaultArgs(\ReflectionFunctionAbstract $function, array $ruleArgs, array $rule = []) { |
|
| 539 | 41 | $ruleArgs = array_change_key_case($ruleArgs); |
|
| 540 | 41 | $result = []; |
|
| 541 | |||
| 542 | 41 | $pos = 0; |
|
| 543 | 41 | foreach ($function->getParameters() as $i => $param) { |
|
| 544 | 41 | $name = strtolower($param->name); |
|
| 545 | |||
| 546 | 41 | if (array_key_exists($name, $ruleArgs)) { |
|
| 547 | 1 | $value = $ruleArgs[$name]; |
|
| 548 | 41 | } elseif ($param->getClass() |
|
| 549 | 40 | && !(isset($ruleArgs[$pos]) && is_object($ruleArgs[$pos]) && get_class($ruleArgs[$pos]) === $param->getClass()->getName()) |
|
| 550 | 40 | && ($param->getClass()->isInstantiable() || isset($this->rules[$param->getClass()->getName()]) || array_key_exists($param->getClass()->getName(), $this->instances)) |
|
| 551 | 40 | ) { |
|
| 552 | 9 | $value = new DefaultReference($this->normalizeID($param->getClass()->getName())); |
|
| 553 | 40 | } elseif (array_key_exists($pos, $ruleArgs)) { |
|
| 554 | 17 | $value = $ruleArgs[$pos]; |
|
| 555 | 17 | $pos++; |
|
| 556 | 39 | } elseif ($param->isDefaultValueAvailable()) { |
|
| 557 | 24 | $value = $param->getDefaultValue(); |
|
| 558 | 24 | } else { |
|
| 559 | 1 | $value = null; |
|
| 560 | } |
||
| 561 | |||
| 562 | 41 | $result[$name] = $value; |
|
| 563 | 41 | } |
|
| 564 | |||
| 565 | 41 | return $result; |
|
| 566 | } |
||
| 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 | 41 | private function resolveArgs(array $defaultArgs, array $args, $instance = null) { |
|
| 577 | 41 | $args = array_change_key_case($args); |
|
| 578 | |||
| 579 | 41 | $pos = 0; |
|
| 580 | 41 | foreach ($defaultArgs as $name => &$arg) { |
|
| 581 | 41 | if (array_key_exists($name, $args)) { |
|
| 582 | // This is a named arg and should be used. |
||
| 583 | 2 | $value = $args[$name]; |
|
| 584 | 41 | } elseif (isset($args[$pos]) && (!($arg instanceof DefaultReference) || is_a($args[$pos], $arg->getName()))) { |
|
| 585 | // There is an arg at this position and it's the same type as the default arg or the default arg is typeless. |
||
| 586 | 4 | $value = $args[$pos]; |
|
| 587 | 4 | $pos++; |
|
| 588 | 4 | } else { |
|
| 589 | // There is no passed arg, so use the default arg. |
||
| 590 | 38 | $value = $arg; |
|
| 591 | } |
||
| 592 | |||
| 593 | 41 | if ($value instanceof ReferenceInterface) { |
|
| 594 | 9 | $value = $value->resolve($this, $instance); |
|
| 595 | 9 | } |
|
| 596 | 41 | $arg = $value; |
|
| 597 | 41 | } |
|
| 598 | |||
| 599 | 41 | return $defaultArgs; |
|
| 600 | } |
||
| 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 | 48 | private function createInstance($nid, array $args) { |
|
| 612 | 48 | $rule = $this->makeRule($nid); |
|
| 613 | |||
| 614 | // Cache the instance or its factory for future use. |
||
| 615 | 48 | if (empty($rule['shared'])) { |
|
| 616 | 34 | $factory = $this->makeFactory($nid, $rule); |
|
| 617 | 33 | $instance = $factory($args); |
|
| 618 | 33 | $this->factories[$nid] = $factory; |
|
| 619 | 33 | } else { |
|
| 620 | 15 | $instance = $this->createSharedInstance($nid, $rule, $args); |
|
| 621 | } |
||
| 622 | 46 | return $instance; |
|
| 623 | } |
||
| 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 = []) { |
|
| 634 | 3 | $instance = null; |
|
| 635 | |||
| 636 | 3 | if (is_array($callback)) { |
|
| 637 | 1 | $function = new \ReflectionMethod($callback[0], $callback[1]); |
|
| 638 | |||
| 639 | 1 | if (is_object($callback[0])) { |
|
| 640 | 1 | $instance = $callback[0]; |
|
| 641 | 1 | } |
|
| 642 | 1 | } else { |
|
| 643 | 2 | $function = new \ReflectionFunction($callback); |
|
| 644 | } |
||
| 645 | |||
| 646 | 3 | $args = $this->resolveArgs($this->makeDefaultArgs($function, $args), [], $instance); |
|
| 647 | |||
| 648 | 3 | return call_user_func_array($callback, $args); |
|
| 649 | } |
||
| 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 | 5 | public function has($id) { |
|
| 659 | 5 | $id = $this->normalizeID($id); |
|
| 660 | |||
| 661 | 5 | return isset($this->instances[$id]) || !empty($this->rules[$id]) || class_exists($id); |
|
| 662 | } |
||
| 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 | 4 | public function hasRule($id) { |
|
| 671 | 4 | $id = $this->normalizeID($id); |
|
| 672 | 4 | return !empty($this->rules[$id]); |
|
| 673 | } |
||
| 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 | 47 | 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.