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 AttributeHandler 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 AttributeHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class AttributeHandler implements HandlerInterface |
||
18 | { |
||
19 | /** |
||
20 | * List of generic types |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $genericTypes = ['integer', 'float', 'string', 'boolean']; |
||
25 | |||
26 | /** |
||
27 | * Data-type handlers |
||
28 | * |
||
29 | * @var DataTypeHandlerInterface[] |
||
30 | */ |
||
31 | protected $registeredTypes = []; |
||
32 | |||
33 | /** |
||
34 | * Register data-type handler |
||
35 | * |
||
36 | * @param DataTypeHandlerInterface $handler |
||
37 | */ |
||
38 | 2 | public function registerDataTypeHandler(DataTypeHandlerInterface $handler) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 9 | public function toResource($object, ResourceObject $resource, MappingContext $context) |
|
58 | |||
59 | /** |
||
60 | * Process attribute to resource mapping |
||
61 | * |
||
62 | * @param mixed $object |
||
63 | * @param ResourceObject $resource |
||
64 | * @param Attribute $definition |
||
65 | */ |
||
66 | 9 | View Code Duplication | protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition) |
90 | |||
91 | /** |
||
92 | * Process value declared as many (iterable) |
||
93 | * |
||
94 | * @param mixed $value |
||
95 | * @param Attribute $definition |
||
96 | * @return array |
||
97 | */ |
||
98 | 3 | protected function processIterableValueToResource($value, Attribute $definition): array |
|
116 | |||
117 | |||
118 | /** |
||
119 | * Process data-type |
||
120 | * |
||
121 | * @param Attribute $definition |
||
122 | * @param mixed $value |
||
123 | * @return mixed |
||
124 | */ |
||
125 | 4 | View Code Duplication | protected function processTypeToResource(Attribute $definition, $value) |
141 | |||
142 | /** |
||
143 | * Process data-type |
||
144 | * |
||
145 | * @param Attribute $definition |
||
146 | * @param mixed $value |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 3 | View Code Duplication | protected function processResourceToType(Attribute $definition, $value) |
165 | |||
166 | /** |
||
167 | * Process generic data-types |
||
168 | * |
||
169 | * @param string $type |
||
170 | * @param mixed $value |
||
171 | * @return bool|float|int|string |
||
172 | */ |
||
173 | 4 | protected function processGenericType(string $type, $value) |
|
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 7 | public function fromResource($object, ResourceObject $resource, MappingContext $context) |
|
206 | |||
207 | /** |
||
208 | * Process resource to attribute mapping |
||
209 | * |
||
210 | * @param mixed $object |
||
211 | * @param ResourceObject $resource |
||
212 | * @param Attribute $definition |
||
213 | */ |
||
214 | 7 | View Code Duplication | protected function processResourceToAttribute($object, ResourceObject $resource, Attribute $definition) |
241 | |||
242 | /** |
||
243 | * Process iterable value |
||
244 | * |
||
245 | * @param mixed $value |
||
246 | * @param mixed $object |
||
247 | * @param Attribute $definition |
||
248 | */ |
||
249 | 2 | protected function processIterableValueFromResource($value, $object, Attribute $definition) |
|
271 | } |
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.