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 $preferences = []; |
||
| 14 | protected $aliases = []; |
||
| 15 | protected $cache = []; |
||
| 16 | protected $injections = []; |
||
| 17 | protected $params = []; |
||
| 18 | protected $shared = []; |
||
| 19 | protected $sharedInstance = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | 3 | * Set a preferred implementation of a class/interface |
|
| 23 | * @param string $alias the name of the alias |
||
| 24 | 3 | * @param string $class the name of the class/interface |
|
| 25 | 3 | * @return self |
|
| 26 | 3 | */ |
|
| 27 | 3 | public function setPreference(string $alias, string $class): self |
|
| 32 | |||
| 33 | 3 | /** |
|
| 34 | 1 | * Alias a class/interface/alias to a string. |
|
| 35 | * @param string $alias the name of the alias |
||
| 36 | 3 | * @param string $class the name of the class |
|
| 37 | 1 | * @param array $params a key/value array of parameter names and values |
|
| 38 | * @return self |
||
| 39 | 3 | */ |
|
| 40 | 1 | public function setAlias(string $alias, string $class, array $params = []): self |
|
| 48 | |||
| 49 | 3 | /** |
|
| 50 | * Set rule to call a method after constructing a class |
||
| 51 | * @param string $class the name of the class |
||
| 52 | * @param string $method the name of the method |
||
| 53 | * @param array $params a key/value array of parameter names and values |
||
| 54 | * @return self |
||
| 55 | */ |
||
| 56 | public function setInjection(string $class, string $method, array $params = []): self |
||
| 61 | 4 | ||
| 62 | /** |
||
| 63 | * Set parameters of a class |
||
| 64 | * @param string $class the name of the class |
||
| 65 | * @param array $params a key/value array of parameter names and values |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | public function setParams(string $class, array $params): self |
||
| 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 | public function has($class): bool |
||
| 89 | 19 | ||
| 90 | 19 | /** |
|
| 91 | * Get an instance of a class |
||
| 92 | * @param string $class the name of class/alias to create |
||
| 93 | * @param array $params a key/value array of parameter names and values |
||
| 94 | * @return mixed an instance of the class requested |
||
| 95 | */ |
||
| 96 | public function get($class, array $params = []) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get a new instance of a class |
||
| 110 | 4 | * @param string $class the name of class/alias to create |
|
| 111 | * @param array $params a key/value array of parameter names and values |
||
| 112 | 4 | * @return mixed an instance of the class requested |
|
| 113 | 4 | */ |
|
| 114 | public function newInstance(string $class, array $params = []) |
||
| 136 | 58 | ||
| 137 | 56 | protected function getCallback(string $class, string $originalClass): callable |
|
| 155 | 18 | ||
| 156 | protected function generateCallback(string $class): callable |
||
| 171 | 40 | ||
| 172 | 40 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
| 188 | 53 | ||
| 189 | 53 | public function getParameters(array $methodInfo, array $params): array |
|
| 208 | |||
| 209 | 51 | protected function determineParameter($param, bool $isVariadic): array |
|
| 223 | } |
||
| 224 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.