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:
1 | <?php |
||
16 | class AttributeHandler implements HandlerInterface |
||
17 | { |
||
18 | /** |
||
19 | * List of generic types |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $genericTypes = ['integer', 'float', 'string', 'boolean']; |
||
24 | |||
25 | /** |
||
26 | * Data-type handlers |
||
27 | * |
||
28 | * @var DataTypeHandlerInterface[] |
||
29 | */ |
||
30 | protected $registeredTypes = []; |
||
31 | |||
32 | /** |
||
33 | * Register data-type handler |
||
34 | * |
||
35 | * @param DataTypeHandlerInterface $handler |
||
36 | */ |
||
37 | 2 | public function registerDataTypeHandler(DataTypeHandlerInterface $handler) |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 4 | public function toResource($object, ResourceObject $resource, MappingContext $context) |
|
57 | |||
58 | /** |
||
59 | * Process attribute to resource mapping |
||
60 | * |
||
61 | * @param mixed $object |
||
62 | * @param ResourceObject $resource |
||
63 | * @param Attribute $definition |
||
64 | */ |
||
65 | 4 | View Code Duplication | protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition) |
78 | |||
79 | /** |
||
80 | * Process data-type |
||
81 | * |
||
82 | * @param Attribute $definition |
||
83 | * @param mixed $value |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 3 | View Code Duplication | protected function processTypeToResource(Attribute $definition, $value) |
102 | |||
103 | /** |
||
104 | * Process data-type |
||
105 | * |
||
106 | * @param Attribute $definition |
||
107 | * @param mixed $value |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 2 | View Code Duplication | protected function processResourceToType(Attribute $definition, $value) |
126 | |||
127 | /** |
||
128 | * Process generic data-types |
||
129 | * |
||
130 | * @param string $type |
||
131 | * @param mixed $value |
||
132 | * @return bool|float|int|string |
||
133 | */ |
||
134 | 2 | protected function processGenericType(string $type, $value) |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 3 | public function fromResource($object, ResourceObject $resource, MappingContext $context) |
|
167 | |||
168 | /** |
||
169 | * Process resource to attribute mapping |
||
170 | * |
||
171 | * @param mixed $object |
||
172 | * @param ResourceObject $resource |
||
173 | * @param Attribute $definition |
||
174 | */ |
||
175 | 3 | View Code Duplication | protected function processResourceToAttribute($object, ResourceObject $resource, Attribute $definition) |
192 | } |
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.