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 |
||
| 7 | class DiMaria |
||
| 8 | { |
||
| 9 | protected $preferences = []; |
||
| 10 | protected $aliases = []; |
||
| 11 | protected $cache = []; |
||
| 12 | protected $injections = []; |
||
| 13 | protected $params = []; |
||
| 14 | protected $shared = []; |
||
| 15 | protected $sharedInstance = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Set multiple di rules at once. |
||
| 19 | * @param array $rules a multi-dimensional array of rules to set |
||
| 20 | * @return self |
||
| 21 | */ |
||
| 22 | 3 | public function setRules(array $rules): self |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Set a preferred implementation of a class/interface |
||
| 54 | * @param string $alias the name of the alias |
||
| 55 | * @param string $className the name of the class/interface |
||
| 56 | * @return self |
||
| 57 | */ |
||
| 58 | 4 | public function setPreference(string $alias, string $className): self |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Alias a class/interface/alias to a string. |
||
| 66 | * @param string $alias the name of the alias |
||
| 67 | * @param string $className the name of the class |
||
| 68 | * @param array $params a key/value array of parameter names and values |
||
| 69 | * @return self |
||
| 70 | */ |
||
| 71 | 23 | public function setAlias(string $alias, string $className, array $params = []): self |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Set rule to call a method after constructing a class |
||
| 82 | * @param string $className the name of the class |
||
| 83 | * @param string $method the name of the method |
||
| 84 | * @param array $params a key/value array of parameter names and values |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | 19 | public function setInjection(string $className, string $method, array $params = []): self |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Set parameters of a class |
||
| 95 | * @param string $className the name of the class |
||
| 96 | * @param array $params a key/value array of parameter names and values |
||
| 97 | * @return self |
||
| 98 | */ |
||
| 99 | 21 | public function setParams(string $className, array $params): self |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Mark a class/alias as shared |
||
| 107 | * @param string $className the name of class/alias |
||
| 108 | * @return self |
||
| 109 | */ |
||
| 110 | 4 | public function setShared(string $className): self |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get an instance of a class |
||
| 118 | * @param string $className the name of class/alias to create |
||
| 119 | * @param array $params a key/value array of parameter names and values |
||
| 120 | * @return mixed an instance of the class requested |
||
| 121 | */ |
||
| 122 | 58 | public function get(string $className, array $params = []) |
|
| 142 | |||
| 143 | 58 | protected function getCallback(string $className, string $originalClassName): callable |
|
| 162 | |||
| 163 | 58 | protected function generateCallback(string $className): callable |
|
| 177 | |||
| 178 | 56 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
| 194 | |||
| 195 | 56 | protected function getParameters(array $methodInfo, array $params): array |
|
| 214 | |||
| 215 | 39 | protected function determineParameter($param, bool $isVariadic): array |
|
| 229 | } |
||
| 230 |