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 ResponseManager implements ResponseManagerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var ResponseFactoryInterface |
||
19 | */ |
||
20 | private $responseFactory; |
||
21 | |||
22 | /** |
||
23 | * @var SerializerInterface |
||
24 | */ |
||
25 | private $serializer; |
||
26 | |||
27 | /** |
||
28 | * @param ResponseFactoryInterface $responseFactory |
||
29 | * @param SerializerInterface $serializer |
||
30 | */ |
||
31 | 6 | public function __construct( |
|
38 | |||
39 | /** |
||
40 | * @param object $object |
||
41 | * @param string $accept |
||
42 | * @param int $status |
||
43 | * @param NormalizerContextInterface|null $context |
||
44 | * |
||
45 | * @return Response |
||
46 | */ |
||
47 | 2 | public function create( |
|
60 | |||
61 | /** |
||
62 | * @param string $accept |
||
63 | * @param int $status |
||
64 | * |
||
65 | * @return Response |
||
66 | */ |
||
67 | 2 | public function createEmpty(string $accept, int $status = 204): Response |
|
71 | |||
72 | /** |
||
73 | * @param string $location |
||
74 | * @param int $status |
||
75 | * |
||
76 | * @return Response |
||
77 | */ |
||
78 | 2 | public function createRedirect(string $location, int $status = 307): Response |
|
82 | |||
83 | /** |
||
84 | * @param ErrorInterface $error |
||
85 | * @param string $accept |
||
86 | * @param int $status |
||
87 | * @param NormalizerContextInterface|null $context |
||
88 | * |
||
89 | * @return Response |
||
90 | */ |
||
91 | public function createByError( |
||
99 | |||
100 | /** |
||
101 | * @param Request $request |
||
102 | * @param string $accept |
||
103 | * @param string $authenticationType |
||
104 | * @param string $reason |
||
105 | * @param NormalizerContextInterface|null $context |
||
106 | * |
||
107 | * @return Response |
||
108 | */ |
||
109 | View Code Duplication | public function createNotAuthenticated( |
|
128 | |||
129 | /** |
||
130 | * @param string $type |
||
131 | * @param array $arguments |
||
132 | * @param string $accept |
||
133 | * @param NormalizerContextInterface|null $context |
||
134 | * |
||
135 | * @return Response |
||
136 | */ |
||
137 | View Code Duplication | public function createNotAuthorized( |
|
151 | |||
152 | /** |
||
153 | * @param string $type |
||
154 | * @param array $arguments |
||
155 | * @param string $accept |
||
156 | * @param NormalizerContextInterface|null $context |
||
157 | * |
||
158 | * @return Response |
||
159 | */ |
||
160 | View Code Duplication | public function createResourceNotFound( |
|
174 | |||
175 | /** |
||
176 | * @param Request $request |
||
177 | * |
||
178 | * @return Response |
||
179 | */ |
||
180 | public function createAcceptNotSupported(Request $request): Response |
||
188 | |||
189 | /** |
||
190 | * @param Request $request |
||
191 | * @param string $accept |
||
192 | * @param array $supportedContentTypes |
||
193 | * @param NormalizerContextInterface|null $context |
||
194 | * |
||
195 | * @return Response |
||
196 | */ |
||
197 | View Code Duplication | public function createContentTypeNotSupported( |
|
214 | } |
||
215 |
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.