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 |
||
5 | trait WooCommerceTrait |
||
6 | { |
||
7 | /** |
||
8 | * GET method. |
||
9 | * Retrieve data. |
||
10 | * |
||
11 | * @param string $endpoint API endpoint. |
||
12 | * @param array $options |
||
13 | * |
||
14 | * @return array |
||
15 | */ |
||
16 | View Code Duplication | public function all($endpoint = '', $options = []) |
|
26 | |||
27 | /** |
||
28 | * GET method. |
||
29 | * Retrieve Single data. |
||
30 | * |
||
31 | * @param string $endpoint API endpoint. |
||
32 | * @param array $options |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | View Code Duplication | public function find($endpoint = '', $options = []) |
|
46 | |||
47 | /** |
||
48 | * POST method. |
||
49 | * Insert data. |
||
50 | * |
||
51 | * @param string $endpoint API endpoint. |
||
52 | * @param array $data |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | View Code Duplication | public function create($endpoint, $data) |
|
66 | |||
67 | /** |
||
68 | * PUT method. |
||
69 | * Update data. |
||
70 | * |
||
71 | * @param string $endpoint API endpoint. |
||
72 | * @param array $data |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | View Code Duplication | public function update($endpoint, $data) |
|
86 | |||
87 | /** |
||
88 | * DELETE method. |
||
89 | * Remove data. |
||
90 | * |
||
91 | * @param string $endpoint API endpoint. |
||
92 | * @param array $options |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | View Code Duplication | public function delete($endpoint, $options = []) |
|
106 | |||
107 | /** |
||
108 | * Return the last request header. |
||
109 | * |
||
110 | * @return \Automattic\WooCommerce\HttpClient\Request |
||
111 | */ |
||
112 | public function getRequest() |
||
120 | |||
121 | /** |
||
122 | * Return the http response headers from last request. |
||
123 | * |
||
124 | * @return \Automattic\WooCommerce\HttpClient\Response |
||
125 | */ |
||
126 | public function getResponse() |
||
134 | |||
135 | /** |
||
136 | * Count the total results and return it. |
||
137 | * |
||
138 | * @return int |
||
139 | */ |
||
140 | public function countResults() |
||
144 | |||
145 | /** |
||
146 | * Count the total pages and return. |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function countPages() |
||
154 | |||
155 | /** |
||
156 | * Return the current page number. |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function current() |
||
164 | |||
165 | /** |
||
166 | * Return the previous page number. |
||
167 | * |
||
168 | * @return int|null |
||
169 | */ |
||
170 | public function previous() |
||
176 | |||
177 | /** |
||
178 | * Return the next page number. |
||
179 | * |
||
180 | * @return int|null |
||
181 | */ |
||
182 | public function next() |
||
188 | } |
||
189 |
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.