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 namespace Neomerx\JsonApi\Http; |
||
32 | abstract class Responses implements ResponsesInterface |
||
33 | { |
||
34 | /** Header name that contains format of input data from client */ |
||
35 | const HEADER_CONTENT_TYPE = HeaderInterface::HEADER_CONTENT_TYPE; |
||
36 | |||
37 | /** Header name that location of newly created resource */ |
||
38 | const HEADER_LOCATION = HeaderInterface::HEADER_LOCATION; |
||
39 | |||
40 | /** |
||
41 | * Create HTTP response. |
||
42 | * |
||
43 | * @param string|null $content |
||
44 | * @param int $statusCode |
||
45 | * @param array $headers |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | abstract protected function createResponse($content, $statusCode, array $headers); |
||
50 | |||
51 | /** |
||
52 | * @return EncoderInterface |
||
53 | */ |
||
54 | abstract protected function getEncoder(); |
||
55 | |||
56 | /** |
||
57 | * @return string|null |
||
58 | */ |
||
59 | abstract protected function getUrlPrefix(); |
||
60 | |||
61 | /** |
||
62 | * @return EncodingParametersInterface|null |
||
63 | */ |
||
64 | abstract protected function getEncodingParameters(); |
||
65 | |||
66 | /** |
||
67 | * @return ContainerInterface |
||
68 | */ |
||
69 | abstract protected function getSchemaContainer(); |
||
70 | |||
71 | /** |
||
72 | * @return SupportedExtensionsInterface|null |
||
73 | */ |
||
74 | abstract protected function getSupportedExtensions(); |
||
75 | |||
76 | /** |
||
77 | * @return MediaTypeInterface |
||
78 | */ |
||
79 | abstract protected function getMediaType(); |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | 2 | View Code Duplication | public function getContentResponse( |
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 2 | View Code Duplication | public function getCreatedResponse($resource, $links = null, $meta = null, array $headers = []) |
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 5 | public function getCodeResponse($statusCode, array $headers = []) |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 2 | public function getMetaResponse($meta, $statusCode = self::HTTP_OK, array $headers = []) |
|
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | 2 | View Code Duplication | public function getIdentifiersResponse( |
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | 4 | public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST, array $headers = []) |
|
165 | |||
166 | /** |
||
167 | * @param mixed $resource |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | protected function getResourceLocationUrl($resource) |
|
179 | |||
180 | /** |
||
181 | * @param string|null $content |
||
182 | * @param int $statusCode |
||
183 | * @param array $headers |
||
184 | * |
||
185 | * @return mixed |
||
186 | */ |
||
187 | 17 | protected function createJsonApiResponse($content, $statusCode, array $headers = []) |
|
211 | } |
||
212 |
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.