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 | 70 | public function __construct() |
|
25 | |||
26 | /** |
||
27 | * Alias a class/interface/alias to a string |
||
28 | * @param string $alias the name of the alias |
||
29 | * @param string $class the name of the class |
||
30 | * @param array $params a key/value array of parameter names and values |
||
31 | * @return self |
||
32 | */ |
||
33 | 23 | public function setAlias(string $alias, string $class, array $params = []): self |
|
41 | |||
42 | /** |
||
43 | * Set rule to call a method after constructing a class |
||
44 | * @param string $class the name of the class |
||
45 | * @param string $method the name of the method |
||
46 | * @param array $params a key/value array of parameter names and values |
||
47 | * @return self |
||
48 | */ |
||
49 | 18 | public function setInjection(string $class, string $method, array $params = []): self |
|
54 | |||
55 | /** |
||
56 | * Set parameters of a class |
||
57 | * @param string $class the name of the class |
||
58 | * @param array $params a key/value array of parameter names and values |
||
59 | * @return self |
||
60 | */ |
||
61 | 20 | public function setParams(string $class, array $params): self |
|
66 | |||
67 | /** |
||
68 | * Set a preferred implementation of a class/interface |
||
69 | * @param string $alias the name of the alias |
||
70 | * @param string $class the name of the class/interface |
||
71 | * @return self |
||
72 | */ |
||
73 | 3 | public function setPreference(string $alias, string $class): self |
|
78 | |||
79 | /** |
||
80 | * Mark a class/alias as shared |
||
81 | * @param string $class the name of class/alias |
||
82 | * @return self |
||
83 | */ |
||
84 | 4 | public function setShared(string $class): self |
|
89 | |||
90 | /** |
||
91 | * Set a value. Retrievable with the get method |
||
92 | * @param string $key |
||
93 | * @param mixed $value |
||
94 | * @return self |
||
95 | */ |
||
96 | 4 | public function set(string $key, $value): self |
|
101 | |||
102 | /** |
||
103 | * Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
||
104 | * @param string $class identifier of the entry to look for |
||
105 | * @return boolean |
||
106 | */ |
||
107 | 64 | public function has($class): bool |
|
111 | |||
112 | /** |
||
113 | * Get an instance of a class |
||
114 | * @param string $class the name of class/alias to create |
||
115 | * @param array $params a key/value array of parameter names and values |
||
116 | * @throws NotFoundException when no entry was found |
||
117 | * @throws ContainerException if class could not be constructed |
||
118 | * @return mixed an instance of the class requested |
||
119 | */ |
||
120 | 63 | public function get($class, array $params = []) |
|
131 | |||
132 | /** |
||
133 | * Get a new instance of a class |
||
134 | * @param string $class the name of class/alias to create |
||
135 | * @param array $params a key/value array of parameter names and values |
||
136 | * @throws NotFoundException if no entry was found |
||
137 | * @throws ContainerException if class could not be constructed |
||
138 | * @return mixed an instance of the class requested |
||
139 | */ |
||
140 | 27 | public function create(string $class, array $params = []) |
|
147 | |||
148 | 61 | protected function getClassName(string $class): string |
|
158 | |||
159 | 59 | protected function getObject(string $class, array $params) |
|
175 | |||
176 | 59 | protected function getCallback(string $class, string $originalClass): callable |
|
194 | |||
195 | 59 | protected function generateCallback(string $class): callable |
|
210 | |||
211 | 56 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
227 | |||
228 | 56 | public function getParameters(array $methodInfo, array $params): array |
|
247 | |||
248 | 39 | protected function determineParameter($param, bool $isVariadic): array |
|
262 | } |
||
263 |