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 |
||
20 | class ControllerQuery |
||
21 | { |
||
22 | use FilterHandlers; |
||
23 | use FilterCheck; |
||
24 | |||
25 | /** |
||
26 | * List of enabled filters generated by ControllerRequest. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $enabledFilters = []; |
||
31 | |||
32 | /** |
||
33 | * List of setted filters by user. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $settedFilters |
||
38 | = [ |
||
39 | 'field_list' => '', |
||
40 | 'limit' => 100, |
||
41 | 'offset' => 0, |
||
42 | 'filter' => '', |
||
43 | 'sort' => '', |
||
44 | 'api_key' => '', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Part of url to proper resource. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $url = ""; |
||
53 | |||
54 | /** |
||
55 | * Connection instance. |
||
56 | * |
||
57 | * @var \ComicVine\Api\Connection\Connection |
||
58 | */ |
||
59 | private $connection; |
||
60 | |||
61 | /** |
||
62 | * Validation object. |
||
63 | * |
||
64 | * @var Validation |
||
65 | */ |
||
66 | private $validation; |
||
67 | |||
68 | /** |
||
69 | * ControllerQuery constructor. |
||
70 | * |
||
71 | * @param array $filters List of enabled filters |
||
72 | * @param string $url Part of URL |
||
73 | */ |
||
74 | public function __construct($filters, $url) |
||
82 | |||
83 | /** |
||
84 | * Set fields to get in response. |
||
85 | * |
||
86 | * @param array $arr |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setFieldList($arr = []) |
||
98 | |||
99 | /** |
||
100 | * Set filters for request. |
||
101 | * |
||
102 | * @param array $arr Filters defined by user. |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | View Code Duplication | public function setFilters($arr = []) |
|
116 | |||
117 | /** |
||
118 | * Set sort for request. |
||
119 | * |
||
120 | * @param array $arr Sort defined by user |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | View Code Duplication | public function setSorts($arr = []) |
|
134 | |||
135 | /** |
||
136 | * Set limit for request. |
||
137 | * |
||
138 | * @param int $limit Limit for elements. Limit can only be between 0 and 100. |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | View Code Duplication | public function setLimit($limit = 100) |
|
152 | |||
153 | /** |
||
154 | * Set offset for request. |
||
155 | * |
||
156 | * @param int $offset Offset for elements. |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | View Code Duplication | public function setOffset($offset = 0) |
|
170 | |||
171 | /** |
||
172 | * Set format for response. |
||
173 | * |
||
174 | * @param \ComicVine\Api\Response\Type\ResponseFormat $format |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function setFormat(ResponseFormat $format) |
||
184 | |||
185 | /** |
||
186 | * Get reponse from Connection instance. |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public function getResponse() |
||
196 | |||
197 | /** |
||
198 | * Build full url from protected attributes. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public function build() |
||
212 | |||
213 | /** |
||
214 | * Iterate array to add elements to class variable. |
||
215 | * |
||
216 | * @param array $array Array of elements to iterate |
||
217 | * @param string $type Type of elements: sort|filter |
||
218 | * |
||
219 | * @return false |
||
220 | */ |
||
221 | protected function iterateFilterOrSort($array, $type) |
||
229 | |||
230 | /** |
||
231 | * Add new filter or sort to settedFilters array. |
||
232 | * |
||
233 | * @param string $type Type: sort|filter |
||
234 | * @param string $param Param for request |
||
235 | * @param string $value Value for request |
||
236 | * @param bool|false $last Is element last? |
||
237 | */ |
||
238 | protected function addFilterOrSort($type, $param, $value, $last = false) |
||
248 | |||
249 | /** |
||
250 | * Iterate array to set allowed fields in response. |
||
251 | * |
||
252 | * @param array $array List of allowed fields in request. |
||
253 | */ |
||
254 | protected function iterateFieldList($array) |
||
265 | |||
266 | } |
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.