Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like XmlResolver 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 XmlResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class XmlResolver |
||
36 | { |
||
37 | /** How parameter presents in xml code */ |
||
38 | const PARAMETERS_KEY = 'parameters'; |
||
39 | /** How dependencies presents in xml code */ |
||
40 | const DEPENDENCIES_KEY = 'dependencies'; |
||
41 | /** How instance presents in xml code */ |
||
42 | const INSTANCE_KEY = 'definition'; |
||
43 | |||
44 | /** |
||
45 | * Resolve xml code |
||
46 | * |
||
47 | * @param DefinitionBuilder $definitionBuilder |
||
48 | * @param $xml |
||
49 | * |
||
50 | * @throws ClassDefinitionAlreadyExistsException |
||
51 | * @throws \InvalidArgumentException |
||
52 | * @throws PropertyDefinitionAlreadyExistsException |
||
53 | * @throws ReferenceNotImplementsException |
||
54 | * @throws MethodDefinitionAlreadyExistsException |
||
55 | * @throws ParameterDefinitionAlreadyExistsException |
||
56 | * @throws \samsonframework\container\definition\parameter\exception\ParameterAlreadyExistsException |
||
57 | */ |
||
58 | 1 | public function resolve(DefinitionBuilder $definitionBuilder, $xml) |
|
120 | |||
121 | /** |
||
122 | * Resolve constructor |
||
123 | * |
||
124 | * @param ClassDefinition $classDefinition |
||
125 | * @param array $constructorArray |
||
126 | * @throws MethodDefinitionAlreadyExistsException |
||
127 | * @throws \InvalidArgumentException |
||
128 | * @throws ParameterDefinitionAlreadyExistsException |
||
129 | * @throws ReferenceNotImplementsException |
||
130 | */ |
||
131 | 1 | View Code Duplication | public function resolveConstructor(ClassDefinition $classDefinition, array $constructorArray) |
138 | |||
139 | /** |
||
140 | * Resolve property |
||
141 | * |
||
142 | * @param ClassDefinition $classDefinition |
||
143 | * @param array $propertyArray |
||
144 | * @param string $propertyName |
||
145 | * @throws \InvalidArgumentException |
||
146 | * @throws ReferenceNotImplementsException |
||
147 | * @throws PropertyDefinitionAlreadyExistsException |
||
148 | */ |
||
149 | 1 | public function resolveProperty(ClassDefinition $classDefinition, array $propertyArray, string $propertyName) |
|
154 | |||
155 | /** |
||
156 | * Resolve method |
||
157 | * |
||
158 | * @param ClassDefinition $classDefinition |
||
159 | * @param array $methodArray |
||
160 | * @param string $methodName |
||
161 | * @throws MethodDefinitionAlreadyExistsException |
||
162 | * @throws \InvalidArgumentException |
||
163 | * @throws ParameterDefinitionAlreadyExistsException |
||
164 | * @throws ReferenceNotImplementsException |
||
165 | */ |
||
166 | 1 | View Code Duplication | public function resolveMethod(ClassDefinition $classDefinition, array $methodArray, string $methodName) |
173 | |||
174 | /** |
||
175 | * Resolve method/constructor arguments |
||
176 | * |
||
177 | * @param MethodDefinition $methodDefinition |
||
178 | * @param array $arguments |
||
179 | * @throws ParameterDefinitionAlreadyExistsException |
||
180 | * @throws \InvalidArgumentException |
||
181 | * @throws ReferenceNotImplementsException |
||
182 | */ |
||
183 | 1 | public function resolveArguments(MethodDefinition $methodDefinition, array $arguments) |
|
191 | |||
192 | /** |
||
193 | * Resolve dependency |
||
194 | * |
||
195 | * @param $data |
||
196 | * @return ReferenceInterface |
||
197 | * @throws \InvalidArgumentException |
||
198 | * @throws ReferenceNotImplementsException |
||
199 | */ |
||
200 | 1 | public function resolveDependency($data): ReferenceInterface |
|
239 | |||
240 | /** |
||
241 | * Resolve collection type |
||
242 | * |
||
243 | * @param array $data |
||
244 | * @return CollectionReference |
||
245 | * @throws ReferenceNotImplementsException |
||
246 | * @throws \InvalidArgumentException |
||
247 | */ |
||
248 | 1 | public function resolveCollection(array $data): CollectionReference |
|
266 | |||
267 | /** |
||
268 | * Convert xml to array |
||
269 | * |
||
270 | * @param $xmlObject |
||
271 | * @param array $out |
||
272 | * @return array |
||
273 | */ |
||
274 | 1 | protected function xml2array($xmlObject, array $out = []): array |
|
281 | } |
||
282 |