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 $aliases = []; |
||
10 | protected $cache = []; |
||
11 | protected $injections = []; |
||
12 | protected $params = []; |
||
13 | protected $shared = []; |
||
14 | protected $sharedInstance = []; |
||
15 | |||
16 | /** |
||
17 | * Set multiple di rules at once. Rules are applied in the following format: |
||
18 | * [ 'aliases' => ['aliasName' => ['instance', ['optional key/value array of params']]], |
||
19 | * 'params' => ['instance' => ['key/value array of params']], |
||
20 | * 'shared' => ['instance' => true], |
||
21 | * 'injections' => ['instance' => [['method', ['key/value array of params']]] |
||
22 | * ] |
||
23 | * |
||
24 | * @param array $rules a multi-dimensional array of rules to set |
||
25 | * @return self |
||
26 | */ |
||
27 | public function setRules(array $rules): self |
||
55 | |||
56 | /** |
||
57 | * Alias a class/interface/alias to another. |
||
58 | * @param string $alias the name of the alias |
||
59 | * @param string $className the name of the class |
||
60 | * @param array $params a key/value array of parameter names and values |
||
61 | * @return self |
||
62 | */ |
||
63 | public function setAlias(string $alias, string $className, array $params = []): self |
||
71 | |||
72 | /** |
||
73 | * Call a method after constructing a class |
||
74 | * @param string $className the name of the class |
||
75 | * @param string $method the name of the method |
||
76 | * @param array $params a key/value array of parameter names and values |
||
77 | * @return self |
||
78 | */ |
||
79 | public function setInjection(string $className, string $method, array $params = []): self |
||
90 | |||
91 | /** |
||
92 | * Set parameters of a class |
||
93 | * @param string $className the name of the class |
||
94 | * @param array $params a key/value array of parameter names and values |
||
95 | * @return self |
||
96 | */ |
||
97 | public function setParams(string $className, array $params): self |
||
102 | |||
103 | /** |
||
104 | * Mark a class/alias as shared |
||
105 | * @param string $className the name of class/alias |
||
106 | * @return self |
||
107 | */ |
||
108 | public function setShared(string $className): self |
||
113 | |||
114 | /** |
||
115 | * Get an instance of a class |
||
116 | * @param string $className the name of class/alias to create |
||
117 | * @param array $params a key/value array of parameter names and values |
||
118 | * @return mixed an instance of the class requested |
||
119 | */ |
||
120 | public function get(string $className, array $params = []) |
||
137 | |||
138 | protected function getCallback(string $className, string $originalClassName): callable |
||
157 | |||
158 | protected function generateCallback(string $className): callable |
||
173 | |||
174 | protected function getMethodInfo(\ReflectionMethod $method): array |
||
190 | |||
191 | protected function getParameters(array $methodInfo, array $params): array |
||
210 | |||
211 | protected function determineParameter($param, bool $isVariadic): array |
||
225 | } |
||
226 |