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 |
||
37 | class XmlResolver |
||
38 | { |
||
39 | /** How parameter presents in xml code */ |
||
40 | const PARAMETERS_KEY = 'parameters'; |
||
41 | /** How dependencies presents in xml code */ |
||
42 | const DEPENDENCIES_KEY = 'dependencies'; |
||
43 | /** How instance presents in xml code */ |
||
44 | const INSTANCE_KEY = 'definition'; |
||
45 | |||
46 | /** |
||
47 | * Resolve file |
||
48 | * |
||
49 | * @param DefinitionBuilder $builder |
||
50 | * @param string $path |
||
51 | * |
||
52 | * @throws FileNotFoundException |
||
53 | * @throws ClassDefinitionAlreadyExistsException |
||
54 | * @throws \InvalidArgumentException |
||
55 | * @throws PropertyDefinitionAlreadyExistsException |
||
56 | * @throws ReferenceNotImplementsException |
||
57 | * @throws MethodDefinitionAlreadyExistsException |
||
58 | * @throws ParameterDefinitionAlreadyExistsException |
||
59 | * @throws ParameterAlreadyExistsException |
||
60 | */ |
||
61 | public function resolveFile(DefinitionBuilder $builder, string $path) |
||
69 | |||
70 | /** |
||
71 | * Resolve xml code |
||
72 | * |
||
73 | * @param DefinitionBuilder $definitionBuilder |
||
74 | * @param string $xml |
||
75 | * |
||
76 | * @throws ClassDefinitionAlreadyExistsException |
||
77 | * @throws \InvalidArgumentException |
||
78 | * @throws PropertyDefinitionAlreadyExistsException |
||
79 | * @throws ReferenceNotImplementsException |
||
80 | * @throws MethodDefinitionAlreadyExistsException |
||
81 | * @throws ParameterDefinitionAlreadyExistsException |
||
82 | * @throws ParameterAlreadyExistsException |
||
83 | */ |
||
84 | 1 | public function resolve(DefinitionBuilder $definitionBuilder, string $xml) |
|
146 | |||
147 | /** |
||
148 | * Resolve constructor |
||
149 | * |
||
150 | * @param ClassDefinition $classDefinition |
||
151 | * @param array $constructorArray |
||
152 | * @throws MethodDefinitionAlreadyExistsException |
||
153 | * @throws \InvalidArgumentException |
||
154 | * @throws ParameterDefinitionAlreadyExistsException |
||
155 | * @throws ReferenceNotImplementsException |
||
156 | */ |
||
157 | 1 | View Code Duplication | protected function resolveConstructor(ClassDefinition $classDefinition, array $constructorArray) |
164 | |||
165 | /** |
||
166 | * Resolve property |
||
167 | * |
||
168 | * @param ClassDefinition $classDefinition |
||
169 | * @param array $propertyArray |
||
170 | * @param string $propertyName |
||
171 | * @throws \InvalidArgumentException |
||
172 | * @throws ReferenceNotImplementsException |
||
173 | * @throws PropertyDefinitionAlreadyExistsException |
||
174 | */ |
||
175 | 1 | protected function resolveProperty(ClassDefinition $classDefinition, array $propertyArray, string $propertyName) |
|
180 | |||
181 | /** |
||
182 | * Resolve method |
||
183 | * |
||
184 | * @param ClassDefinition $classDefinition |
||
185 | * @param array $methodArray |
||
186 | * @param string $methodName |
||
187 | * @throws MethodDefinitionAlreadyExistsException |
||
188 | * @throws \InvalidArgumentException |
||
189 | * @throws ParameterDefinitionAlreadyExistsException |
||
190 | * @throws ReferenceNotImplementsException |
||
191 | */ |
||
192 | 1 | View Code Duplication | protected function resolveMethod(ClassDefinition $classDefinition, array $methodArray, string $methodName) |
199 | |||
200 | /** |
||
201 | * Resolve method/constructor arguments |
||
202 | * |
||
203 | * @param MethodDefinition $methodDefinition |
||
204 | * @param array $arguments |
||
205 | * @throws ParameterDefinitionAlreadyExistsException |
||
206 | * @throws \InvalidArgumentException |
||
207 | * @throws ReferenceNotImplementsException |
||
208 | */ |
||
209 | 1 | protected function resolveArguments(MethodDefinition $methodDefinition, array $arguments) |
|
217 | |||
218 | /** |
||
219 | * Resolve dependency |
||
220 | * |
||
221 | * @param $data |
||
222 | * @return ReferenceInterface |
||
223 | * @throws \InvalidArgumentException |
||
224 | * @throws ReferenceNotImplementsException |
||
225 | */ |
||
226 | 1 | protected function resolveDependency($data): ReferenceInterface |
|
265 | |||
266 | /** |
||
267 | * Resolve collection type |
||
268 | * |
||
269 | * @param array $data |
||
270 | * @return CollectionReference |
||
271 | * @throws \samsonframework\container\definition\builder\exception\ReferenceNotImplementsException |
||
272 | * @throws ReferenceNotImplementsException |
||
273 | * @throws \InvalidArgumentException |
||
274 | */ |
||
275 | 1 | protected function resolveCollection(array $data): CollectionReference |
|
293 | |||
294 | /** |
||
295 | * Convert xml to array |
||
296 | * |
||
297 | * @param $xmlObject |
||
298 | * @param array $out |
||
299 | * @return array |
||
300 | */ |
||
301 | 1 | protected function xml2array($xmlObject, array $out = []): array |
|
308 | } |
||
309 |