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 | final class RequestManager implements RequestManagerInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var ContentNegotiator |
||
20 | */ |
||
21 | private $contentNegotiator; |
||
22 | |||
23 | /** |
||
24 | * @var DeserializerInterface |
||
25 | */ |
||
26 | private $deserializer; |
||
27 | |||
28 | /** |
||
29 | * @var LanguageNegotiator |
||
30 | */ |
||
31 | private $languageNegotiator; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $languages; |
||
37 | |||
38 | /** |
||
39 | * @var TransformerInterface |
||
40 | */ |
||
41 | private $transformer; |
||
42 | |||
43 | /** |
||
44 | * @param ContentNegotiator $contentNegotiator |
||
45 | * @param DeserializerInterface $deserializer |
||
46 | * @param LanguageNegotiator $languageNegotiator |
||
47 | * @param array $languages |
||
48 | * @param TransformerInterface $transformer |
||
49 | */ |
||
50 | 13 | public function __construct( |
|
63 | |||
64 | /** |
||
65 | * @param Request $request |
||
66 | * |
||
67 | * @return string|null |
||
68 | */ |
||
69 | 3 | public function getAccept(Request $request) |
|
73 | |||
74 | /** |
||
75 | * @param Request $request |
||
76 | * |
||
77 | * @return string|null |
||
78 | */ |
||
79 | 3 | View Code Duplication | public function getAcceptLanguage(Request $request) |
97 | |||
98 | /** |
||
99 | * @param Request $request |
||
100 | * |
||
101 | * @return string|null |
||
102 | */ |
||
103 | 6 | public function getContentType(Request $request) |
|
107 | |||
108 | /** |
||
109 | * @param Request $request |
||
110 | * @param string $headerName |
||
111 | * |
||
112 | * @return null|string |
||
113 | */ |
||
114 | 9 | View Code Duplication | private function negotiateContentType(Request $request, string $headerName) |
132 | |||
133 | /** |
||
134 | * @param Request $request |
||
135 | * @param object|string $object |
||
136 | * |
||
137 | * @return object|null |
||
138 | */ |
||
139 | 3 | public function getDataFromRequestBody(Request $request, $object) |
|
155 | |||
156 | /** |
||
157 | * @param Request $request |
||
158 | * @param object|string $object |
||
159 | * |
||
160 | * @return object|null |
||
161 | */ |
||
162 | 1 | public function getDataFromRequestQuery(Request $request, $object) |
|
168 | |||
169 | /** |
||
170 | * @param object|string $object |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 2 | private function getSerializerMethod($object): string |
|
178 | } |
||
179 |
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.