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 |
||
15 | final class RequestManager implements RequestManagerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var AcceptNegotiatorInterface |
||
19 | */ |
||
20 | private $acceptNegotiator; |
||
21 | |||
22 | /** |
||
23 | * @var AcceptLanguageNegotiatorInterface |
||
24 | */ |
||
25 | private $acceptLanguageNegotiator; |
||
26 | |||
27 | /** |
||
28 | * @var ContentTypeNegotiatorInterface |
||
29 | */ |
||
30 | private $contentTypeNegotiator; |
||
31 | |||
32 | /** |
||
33 | * @var DeserializerInterface |
||
34 | */ |
||
35 | private $deserializer; |
||
36 | |||
37 | /** |
||
38 | * @var TransformerInterface |
||
39 | */ |
||
40 | private $transformer; |
||
41 | |||
42 | /** |
||
43 | * @param AcceptNegotiatorInterface $acceptNegotiator |
||
44 | * @param AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator |
||
45 | * @param ContentTypeNegotiatorInterface $contentTypeNegotiator |
||
46 | * @param DeserializerInterface $deserializer |
||
47 | * @param TransformerInterface $transformer |
||
48 | */ |
||
49 | 19 | public function __construct( |
|
62 | |||
63 | /** |
||
64 | * @param Request $request |
||
65 | * @param string|null $default |
||
66 | * |
||
67 | * @return string|null |
||
68 | */ |
||
69 | 5 | View Code Duplication | public function getAccept(Request $request, string $default = null) |
77 | |||
78 | /** |
||
79 | * @param Request $request |
||
80 | * @param string|null $default |
||
81 | * |
||
82 | * @return string|null |
||
83 | */ |
||
84 | 5 | View Code Duplication | public function getAcceptLanguage(Request $request, string $default = null) |
92 | |||
93 | /** |
||
94 | * @param Request $request |
||
95 | * @param string|null $default |
||
96 | * |
||
97 | * @return string|null |
||
98 | */ |
||
99 | 8 | View Code Duplication | public function getContentType(Request $request, string $default = null) |
107 | |||
108 | /** |
||
109 | * @param Request $request |
||
110 | * @param object|string $object |
||
111 | * @param string|null $defaultContentType |
||
112 | * |
||
113 | * @return object|null |
||
114 | */ |
||
115 | 3 | public function getDataFromRequestBody(Request $request, $object, string $defaultContentType = null) |
|
131 | |||
132 | /** |
||
133 | * @param Request $request |
||
134 | * @param object|string $object |
||
135 | * |
||
136 | * @return object|null |
||
137 | */ |
||
138 | 1 | public function getDataFromRequestQuery(Request $request, $object) |
|
144 | |||
145 | /** |
||
146 | * @param object|string $object |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 2 | private function getSerializerMethod($object): string |
|
154 | } |
||
155 |
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.