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 |
||
17 | class DataTypeManager |
||
18 | { |
||
19 | /** |
||
20 | * List of generic types |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $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 | 6 | public function registerDataTypeHandler(DataTypeHandlerInterface $handler) |
|
44 | |||
45 | /** |
||
46 | * Process data-type |
||
47 | * |
||
48 | * @param Attribute $definition |
||
49 | * @param mixed $value |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 16 | View Code Duplication | public function toResource(Attribute $definition, $value) |
70 | |||
71 | /** |
||
72 | * Process data-type |
||
73 | * |
||
74 | * @param Attribute $definition |
||
75 | * @param mixed $value |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 16 | View Code Duplication | public function fromResource(Attribute $definition, $value) |
96 | |||
97 | /** |
||
98 | * Process not-typed value |
||
99 | * Both directions: from object to resource and from resource to object |
||
100 | * |
||
101 | * @param Attribute $definition |
||
102 | * @param mixed $value |
||
103 | * @return mixed |
||
104 | * @throws NotIterableAttribute |
||
105 | */ |
||
106 | 3 | View Code Duplication | protected function processNotTypedToResource(Attribute $definition, $value) |
122 | |||
123 | /** |
||
124 | * Process not-typed value |
||
125 | * Both directions: from object to resource and from resource to object |
||
126 | * |
||
127 | * @param Attribute $definition |
||
128 | * @param mixed $value |
||
129 | * @return mixed |
||
130 | * @throws NotIterableAttribute |
||
131 | */ |
||
132 | 3 | View Code Duplication | protected function processNotTypedFromResource(Attribute $definition, $value) |
148 | |||
149 | /** |
||
150 | * Process value by registered data-type handler. |
||
151 | * From object to resource. |
||
152 | * |
||
153 | * @param Attribute $definition |
||
154 | * @param mixed $value |
||
155 | * @return mixed |
||
156 | * @throws NotIterableAttribute |
||
157 | */ |
||
158 | 3 | View Code Duplication | protected function processHandlerToResource(Attribute $definition, $value) |
180 | |||
181 | /** |
||
182 | * Process value by registered data-type handler. |
||
183 | * From resource to object. |
||
184 | * |
||
185 | * @param Attribute $definition |
||
186 | * @param mixed $value |
||
187 | * @return mixed |
||
188 | * @throws NotIterableAttribute |
||
189 | */ |
||
190 | 3 | View Code Duplication | protected function processHandlerFromResource(Attribute $definition, $value) |
212 | |||
213 | /** |
||
214 | * Process value by generic data-type |
||
215 | * Both directions: from object to resource and from resource to object |
||
216 | * |
||
217 | * @param Attribute $definition |
||
218 | * @param mixed $value |
||
219 | * @param bool $toResource |
||
220 | * @return mixed |
||
221 | */ |
||
222 | 18 | protected function processGeneric(Attribute $definition, $value, bool $toResource = true) |
|
242 | |||
243 | /** |
||
244 | * Process generic data-types |
||
245 | * |
||
246 | * @param string $type |
||
247 | * @param mixed $value |
||
248 | * @return bool|float|int|string |
||
249 | */ |
||
250 | 16 | protected function processGenericType(string $type, $value) |
|
266 | } |
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.