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 | final class RequestManager implements RequestManagerInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var AcceptNegotiatorInterface |
||
18 | */ |
||
19 | private $acceptNegotiator; |
||
20 | |||
21 | /** |
||
22 | * @var AcceptLanguageNegotiatorInterface |
||
23 | */ |
||
24 | private $acceptLanguageNegotiator; |
||
25 | |||
26 | /** |
||
27 | * @var ContentTypeNegotiatorInterface |
||
28 | */ |
||
29 | private $contentTypeNegotiator; |
||
30 | |||
31 | /** |
||
32 | * @var DeserializerInterface |
||
33 | */ |
||
34 | private $deserializer; |
||
35 | |||
36 | /** |
||
37 | * @param AcceptNegotiatorInterface $acceptNegotiator |
||
38 | * @param AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator |
||
39 | * @param ContentTypeNegotiatorInterface $contentTypeNegotiator |
||
40 | * @param DeserializerInterface $deserializer |
||
41 | */ |
||
42 | 3 | public function __construct( |
|
43 | AcceptNegotiatorInterface $acceptNegotiator, |
||
44 | AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator, |
||
45 | ContentTypeNegotiatorInterface $contentTypeNegotiator, |
||
46 | DeserializerInterface $deserializer |
||
47 | ) { |
||
48 | 3 | $this->acceptNegotiator = $acceptNegotiator; |
|
49 | 3 | $this->acceptLanguageNegotiator = $acceptLanguageNegotiator; |
|
50 | 3 | $this->contentTypeNegotiator = $contentTypeNegotiator; |
|
51 | 3 | $this->deserializer = $deserializer; |
|
52 | 3 | } |
|
53 | |||
54 | /** |
||
55 | * @param Request $request |
||
56 | * @param string|null $default |
||
57 | * |
||
58 | * @return string|null |
||
59 | */ |
||
60 | 3 | View Code Duplication | public function getAccept(Request $request, string $default = null) |
|
|||
61 | { |
||
62 | 3 | if (null === $value = $this->acceptNegotiator->negotiate($request)) { |
|
63 | 2 | return $default; |
|
64 | } |
||
65 | |||
66 | 1 | return $value->getValue(); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param Request $request |
||
71 | * @param string|null $default |
||
72 | * |
||
73 | * @return string|null |
||
74 | */ |
||
75 | View Code Duplication | public function getAcceptLanguage(Request $request, string $default = null) |
|
83 | |||
84 | /** |
||
85 | * @param Request $request |
||
86 | * @param string|null $default |
||
87 | * |
||
88 | * @return string|null |
||
89 | */ |
||
90 | View Code Duplication | public function getContentType(Request $request, string $default = null) |
|
98 | |||
99 | /** |
||
100 | * @param Request $request |
||
101 | * @param object|string $object |
||
102 | * @param string $contentType |
||
103 | * @param DenormalizerContextInterface $context |
||
104 | * |
||
105 | * @return object |
||
106 | */ |
||
107 | public function getDataFromRequestBody( |
||
115 | |||
116 | /** |
||
117 | * @param Request $request |
||
118 | * @param object|string $object |
||
119 | * @param DenormalizerContextInterface $context |
||
120 | * |
||
121 | * @return object |
||
122 | */ |
||
123 | public function getDataFromRequestQuery(Request $request, $object, DenormalizerContextInterface $context = null) |
||
127 | } |
||
128 |
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.