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 |
||
14 | class Body implements RestAnnotationInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $parameterName; |
||
20 | |||
21 | /** |
||
22 | * @var string|null |
||
23 | */ |
||
24 | private $denormalizationType; |
||
25 | |||
26 | /** |
||
27 | * @var string|null |
||
28 | */ |
||
29 | private $denormalizationGroup; |
||
30 | |||
31 | /** |
||
32 | * @var bool|null |
||
33 | */ |
||
34 | private $optional; |
||
35 | |||
36 | 97 | View Code Duplication | public function __construct(array $options) |
43 | |||
44 | /** |
||
45 | * @param string|null $denormalizationType |
||
46 | * @return $this |
||
47 | */ |
||
48 | 97 | private function setDenormalizationType($denormalizationType): self |
|
53 | |||
54 | /** |
||
55 | * @param string|null $denormalizationGroup |
||
56 | * @return $this |
||
57 | */ |
||
58 | 97 | public function setDenormalizationGroup($denormalizationGroup): self |
|
63 | |||
64 | /** |
||
65 | * @param string $parameterName |
||
66 | * @return $this |
||
67 | */ |
||
68 | 97 | private function setParameterName(string $parameterName): self |
|
73 | |||
74 | /** |
||
75 | * @param bool|null $optional |
||
76 | * @return $this |
||
77 | */ |
||
78 | 97 | private function setOptional($optional): self |
|
83 | |||
84 | 97 | public function isSeveralSupported(): bool |
|
88 | |||
89 | 97 | public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod) |
|
96 | |||
97 | 97 | View Code Duplication | private function resolveDenormalizationType(ReflectionMethodWrapper $reflectionMethod): string |
115 | |||
116 | 96 | View Code Duplication | private function resolveIfBodyIsOptional(ReflectionMethodWrapper $reflectionMethod): bool |
124 | } |
||
125 |
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.