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 Converter 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 Converter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Converter |
||
22 | { |
||
23 | /** |
||
24 | * @var MetadataCollector |
||
25 | */ |
||
26 | private $metadataCollector; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param MetadataCollector $metadataCollector |
||
32 | */ |
||
33 | public function __construct($metadataCollector) |
||
34 | { |
||
35 | $this->metadataCollector = $metadataCollector; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Converts raw array to document. |
||
40 | * |
||
41 | * @param array $rawData |
||
42 | * @param Manager $manager |
||
43 | * |
||
44 | * @return object |
||
45 | * |
||
46 | * @throws \LogicException |
||
47 | */ |
||
48 | public function convertToDocument($rawData, Manager $manager) |
||
74 | |||
75 | /** |
||
76 | * Assigns all properties to object. |
||
77 | * |
||
78 | * @param array $array |
||
79 | * @param \ReflectionClass $object |
||
80 | * @param array $aliases |
||
81 | * |
||
82 | * @return object |
||
83 | */ |
||
84 | public function assignArrayToObject(array $array, $object, array $aliases) |
||
132 | |||
133 | /** |
||
134 | * Converts object to an array. |
||
135 | * |
||
136 | * @param mixed $object |
||
137 | * @param array $aliases |
||
138 | * @param array $fields |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function convertToArray($object, $aliases = [], $fields = []) |
||
212 | |||
213 | /** |
||
214 | * Check if class matches the expected one. |
||
215 | * |
||
216 | * @param object $object |
||
217 | * @param array $expectedClasses |
||
218 | * |
||
219 | * @throws \InvalidArgumentException |
||
220 | */ |
||
221 | private function checkVariableType($object, array $expectedClasses) |
||
222 | { |
||
223 | if (!is_object($object)) { |
||
224 | $msg = 'Expected variable of type object, got ' . gettype($object) . ". (field isn't multiple)"; |
||
225 | throw new \InvalidArgumentException($msg); |
||
226 | } |
||
227 | |||
228 | $class = get_class($object); |
||
229 | if (!in_array($class, $expectedClasses)) { |
||
230 | throw new \InvalidArgumentException("Expected object of type {$expectedClasses[0]}, got {$class}."); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Check if value is instance of Collection. |
||
236 | * |
||
237 | * @param string $property |
||
238 | * @param mixed $value |
||
239 | * |
||
240 | * @throws \InvalidArgumentException |
||
241 | */ |
||
242 | private function isCollection($property, $value) |
||
252 | |||
253 | /** |
||
254 | * Returns aliases for certain document. |
||
255 | * |
||
256 | * @param object $document |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | private function getAlias($document) |
||
267 | } |
||
268 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.