Complex classes like DiMaria 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 DiMaria, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class DiMaria implements ContainerInterface |
||
| 12 | { |
||
| 13 | protected $aliases = []; |
||
| 14 | protected $cache = []; |
||
| 15 | protected $injections = []; |
||
| 16 | protected $params = []; |
||
| 17 | protected $preferences = []; |
||
| 18 | protected $shared = []; |
||
| 19 | protected $sharedInstance = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | 3 | * Alias a class/interface/alias to a string |
|
| 23 | * @param string $alias the name of the alias |
||
| 24 | 3 | * @param string $class the name of the class |
|
| 25 | 3 | * @param array $params a key/value array of parameter names and values |
|
| 26 | 3 | * @return self |
|
| 27 | 3 | */ |
|
| 28 | 3 | public function setAlias(string $alias, string $class, array $params = []): self |
|
| 29 | { |
||
| 30 | 3 | $this->aliases[$alias] = [ |
|
| 31 | 1 | 'class' => $class, |
|
| 32 | 'params' => $params |
||
| 33 | 3 | ]; |
|
| 34 | 1 | return $this; |
|
| 35 | } |
||
| 36 | 3 | ||
| 37 | 1 | /** |
|
| 38 | * Set rule to call a method after constructing a class |
||
| 39 | 3 | * @param string $class the name of the class |
|
| 40 | 1 | * @param string $method the name of the method |
|
| 41 | 1 | * @param array $params a key/value array of parameter names and values |
|
| 42 | * @return self |
||
| 43 | */ |
||
| 44 | 3 | public function setInjection(string $class, string $method, array $params = []): self |
|
| 45 | 1 | { |
|
| 46 | 1 | $this->injections[$class][$method][] = $params; |
|
| 47 | return $this; |
||
| 48 | } |
||
| 49 | 3 | ||
| 50 | /** |
||
| 51 | * Set parameters of a class |
||
| 52 | * @param string $class the name of the class |
||
| 53 | * @param array $params a key/value array of parameter names and values |
||
| 54 | * @return self |
||
| 55 | */ |
||
| 56 | public function setParams(string $class, array $params): self |
||
| 57 | { |
||
| 58 | 4 | $this->params[$class] = $params + ($this->params[$class] ?? []); |
|
| 59 | return $this; |
||
| 60 | 4 | } |
|
| 61 | 4 | ||
| 62 | /** |
||
| 63 | * Set a preferred implementation of a class/interface |
||
| 64 | * @param string $alias the name of the alias |
||
| 65 | * @param string $class the name of the class/interface |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | public function setPreference(string $alias, string $class): self |
||
| 69 | { |
||
| 70 | $this->preferences[$alias] = $class; |
||
| 71 | 23 | return $this; |
|
| 72 | } |
||
| 73 | 23 | ||
| 74 | 23 | /** |
|
| 75 | 23 | * Mark a class/alias as shared |
|
| 76 | * @param string $class the name of class/alias |
||
| 77 | 23 | * @return self |
|
| 78 | */ |
||
| 79 | public function setShared(string $class): self |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set a value. Retrievable with the get method |
||
| 87 | 19 | * @param string $key |
|
| 88 | * @param mixed $value |
||
| 89 | 19 | * @return self |
|
| 90 | 19 | */ |
|
| 91 | public function set(string $key, $value): self |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
||
| 99 | 21 | * @param string $id identifier of the entry to look for |
|
|
|
|||
| 100 | * @return boolean |
||
| 101 | 21 | */ |
|
| 102 | 21 | public function has($class): bool |
|
| 109 | |||
| 110 | 4 | /** |
|
| 111 | * Get an instance of a class |
||
| 112 | 4 | * @param string $class the name of class/alias to create |
|
| 113 | 4 | * @param array $params a key/value array of parameter names and values |
|
| 114 | * @throws NotFoundException when no entry was found |
||
| 115 | * @throws ContainerException if class could not be constructed |
||
| 116 | * @return mixed an instance of the class requested |
||
| 117 | */ |
||
| 118 | public function get($class, array $params = []) |
||
| 129 | |||
| 130 | 58 | /** |
|
| 131 | 58 | * Get a new instance of a class |
|
| 132 | 22 | * @param string $class the name of class/alias to create |
|
| 133 | 22 | * @param array $params a key/value array of parameter names and values |
|
| 134 | * @throws NotFoundException if no entry was found |
||
| 135 | 58 | * @throws ContainerException if class could not be constructed |
|
| 136 | 58 | * @return mixed an instance of the class requested |
|
| 137 | 56 | */ |
|
| 138 | 4 | public function create(string $class, array $params = []) |
|
| 145 | 58 | ||
| 146 | 58 | protected function getClassName(string $class): string |
|
| 156 | |||
| 157 | protected function getObject(string $class, array $params) |
||
| 173 | 40 | ||
| 174 | 40 | protected function getCallback(string $class, string $originalClass): callable |
|
| 192 | 56 | ||
| 193 | protected function generateCallback(string $class): callable |
||
| 208 | |||
| 209 | 51 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
| 225 | 8 | ||
| 226 | public function getParameters(array $methodInfo, array $params): array |
||
| 245 | |||
| 246 | protected function determineParameter($param, bool $isVariadic): array |
||
| 260 | } |
||
| 261 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.