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 $factory = []; |
||
15 | protected $injections = []; |
||
16 | protected $params = []; |
||
17 | protected $preferences = []; |
||
18 | protected $shared = []; |
||
19 | protected $sharedInstance = []; |
||
20 | |||
21 | 81 | 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 | 24 | 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 | 22 | 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 | * Set an instance to always return a shared or new instance, regardless of method used |
||
81 | * @param string $class the name of class/alias |
||
82 | * @param bool $isShared true will always return a shared instance. false will always return a new instance |
||
83 | * @return self |
||
84 | */ |
||
85 | 6 | public function setShared(string $class, bool $isShared = true): self |
|
90 | |||
91 | /** |
||
92 | * Set a value. Retrievable with the get method |
||
93 | * @param string $key a name to retrieve the value by |
||
94 | * @param mixed $value the content to store against the key |
||
95 | * @return self |
||
96 | */ |
||
97 | 4 | public function set(string $key, $value): self |
|
102 | |||
103 | /** |
||
104 | * Set a factory. Retrievable with the create method. The get method will fetch but also cache the response |
||
105 | * @param string $key a name to retrieve the content by |
||
106 | * @param callable $factory a callable to be invoked when fetched |
||
107 | * @return self |
||
108 | */ |
||
109 | 8 | public function setFactory(string $key, callable $factory): self |
|
114 | |||
115 | /** |
||
116 | * Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
||
117 | * @param string $class identifier of the entry to look for |
||
118 | * @return boolean |
||
119 | */ |
||
120 | 75 | public function has($class): bool |
|
124 | |||
125 | /** |
||
126 | * Get an instance of a class |
||
127 | * @param string $class the name of class/alias to create |
||
128 | * @param array $params a key/value array of parameter names and values |
||
129 | * @throws NotFoundException when no entry was found |
||
130 | * @throws ContainerException if class could not be constructed |
||
131 | * @return mixed an instance of the class requested |
||
132 | */ |
||
133 | 72 | public function get($class, array $params = []) |
|
134 | { |
||
135 | 72 | if (isset($this->shared[$class]) && !$this->shared[$class]) { |
|
136 | 1 | return $this->create($class, $params); |
|
137 | } |
||
138 | 71 | if (isset($this->sharedInstance[$class])) { |
|
139 | 7 | return $this->sharedInstance[$class]; |
|
140 | } |
||
141 | 67 | $class = $this->getClassName($class); |
|
142 | |||
143 | 65 | $object = $this->getObject($class, $params); |
|
144 | 61 | $this->sharedInstance[$class] = $object; |
|
145 | 61 | return $object; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get a new instance of a class |
||
150 | * @param string $class the name of class/alias to create |
||
151 | * @param array $params a key/value array of parameter names and values |
||
152 | * @throws NotFoundException if no entry was found |
||
153 | * @throws ContainerException if class could not be constructed |
||
154 | * @return mixed an instance of the class requested |
||
155 | */ |
||
156 | 33 | public function create(string $class, array $params = []) |
|
157 | { |
||
158 | 33 | if (isset($this->shared[$class]) && $this->shared[$class]) { |
|
159 | 4 | return $this->get($class, $params); |
|
160 | } |
||
161 | 29 | return $this->getObject($this->getClassName($class), $params); |
|
162 | } |
||
163 | |||
164 | 72 | protected function getClassName(string $class): string |
|
174 | |||
175 | 70 | protected function getObject(string $class, array $params) |
|
191 | |||
192 | 67 | protected function getCallback(string $class, string $originalClass): callable |
|
210 | |||
211 | 67 | protected function generateCallback(string $class): callable |
|
226 | |||
227 | 59 | protected function getMethodInfo(\ReflectionMethod $method): array |
|
243 | |||
244 | 59 | protected function getParameters(array $methodInfo, array $params): array |
|
263 | |||
264 | 42 | protected function determineParameter($param, bool $isVariadic): array |
|
265 | { |
||
280 | } |
||
281 |