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 |
||
11 | trait SearchEndpointTrait |
||
12 | { |
||
13 | /** |
||
14 | * Creates an elastic search query from the provided array od parameters. |
||
15 | * |
||
16 | * @param array $params |
||
17 | * @param string $operator |
||
18 | * |
||
19 | * @return string |
||
20 | */ |
||
21 | private function createElasticSearchQuery(array $params, $operator = 'AND') |
||
34 | |||
35 | /** |
||
36 | * Adjust the response from the `search` endpoint for video data type, |
||
37 | * so that it is compatible with the response of `videos` endpoint. |
||
38 | * Namely, it converts the date to a timestamp, adjusts the format of channels array |
||
39 | * and re-maps some renamed properties (such as duration). |
||
40 | * It also renames root-level properties so that they can be correctly unserialized. |
||
41 | * |
||
42 | * @param string $response |
||
43 | * |
||
44 | * @return string |
||
45 | * |
||
46 | * @throws Exception |
||
47 | */ |
||
48 | private function normalizeSearchVideosResponse($response) |
||
80 | |||
81 | /** |
||
82 | * Adjust the response from the `search` endpoint for channel data type. |
||
83 | * Namely, it renames the root-level properties, so they can be correctly unserialized. |
||
84 | * |
||
85 | * @param string $response |
||
86 | * |
||
87 | * @return string |
||
88 | * |
||
89 | * @throws Exception |
||
90 | */ |
||
91 | private function normalizeSearchChannelsResponse($response) |
||
104 | |||
105 | /** |
||
106 | * @param string $response |
||
107 | * |
||
108 | * @return int |
||
109 | * |
||
110 | * @throws Exception |
||
111 | */ |
||
112 | private function getTotalCountFromSearchVideosResponse($response) |
||
121 | |||
122 | /** |
||
123 | * @param int $videoManagerId |
||
124 | * @param VideosRequestParameters|null $parameters |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | private function getRequestOptionsForSearchVideosEndpoint( |
||
167 | } |
||
168 |
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.