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 declare(strict_types = 1); |
||
15 | class SerializerTypeDefinitionMap |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $typeNames = []; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $definitions; |
||
26 | |||
27 | /** |
||
28 | * SerializerTypeDefinitionMap constructor. |
||
29 | * |
||
30 | * @param array $typeNames |
||
31 | * @param array $definitions |
||
32 | */ |
||
33 | public function __construct(array $definitions, $typeNames) |
||
38 | |||
39 | /** |
||
40 | * @param string $operationId |
||
41 | * |
||
42 | * @return \stdClass |
||
43 | */ |
||
44 | public function getDefinitionByOperationId(string $operationId): \stdClass |
||
52 | |||
53 | /** |
||
54 | * @param string $type |
||
55 | * |
||
56 | * @return \stdClass |
||
57 | */ |
||
58 | View Code Duplication | public function getDefinitionByType(string $type): \stdClass |
|
66 | |||
67 | /** |
||
68 | * @param string $fqdn |
||
69 | * |
||
70 | * @return \stdClass |
||
71 | */ |
||
72 | View Code Duplication | public function getDefinitionByFqcn(string $fqdn): \stdClass |
|
82 | |||
83 | /** |
||
84 | * @param string $type |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getFqcn(string $type): string |
||
104 | |||
105 | /** |
||
106 | * @param string $operationId |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | View Code Duplication | public function getTypeNameByOperationId(string $operationId): string |
|
118 | |||
119 | /** |
||
120 | * @param string $fqdn |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | View Code Duplication | private function getType(string $fqdn): string |
|
132 | } |
||
133 |
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.