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 |
||
14 | class EEH_Sideloader extends EEH_Base |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @since 4.1.0 |
||
19 | * @var string |
||
20 | */ |
||
21 | private $_upload_to; |
||
22 | |||
23 | /** |
||
24 | * @since 4.10.5.p |
||
25 | * @var string |
||
26 | */ |
||
27 | private $_download_from; |
||
28 | |||
29 | /** |
||
30 | * @since 4.1.0 |
||
31 | * @var string |
||
32 | */ |
||
33 | private $_permissions; |
||
34 | |||
35 | /** |
||
36 | * @since 4.1.0 |
||
37 | * @var string |
||
38 | */ |
||
39 | private $_new_file_name; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * constructor allows the user to set the properties on the sideloader on construct. However, there are also |
||
44 | * setters for doing so. |
||
45 | * |
||
46 | * @param array $init array fo initializing the sideloader if keys match the properties. |
||
47 | * @since 4.1.0 |
||
48 | */ |
||
49 | public function __construct($init = []) |
||
53 | |||
54 | |||
55 | /** |
||
56 | * sets the properties for class either to defaults or using incoming initialization array |
||
57 | * |
||
58 | * @param array $init array on init (keys match properties others ignored) |
||
59 | * @return void |
||
60 | * @since 4.1.0 |
||
61 | */ |
||
62 | private function _init($init) |
||
98 | |||
99 | |||
100 | // utilities |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | * @since 4.1.0 |
||
106 | */ |
||
107 | private function _get_wp_uploads_dir() |
||
112 | |||
113 | // setters |
||
114 | |||
115 | |||
116 | /** |
||
117 | * sets the _upload_to property to the directory to upload to. |
||
118 | * |
||
119 | * @param $upload_to_folder |
||
120 | * @return void |
||
121 | * @since 4.1.0 |
||
122 | */ |
||
123 | public function set_upload_to($upload_to_folder) |
||
127 | |||
128 | |||
129 | /** |
||
130 | * sets the _download_from property to the location we should download the file from. |
||
131 | * |
||
132 | * @param string $download_from The full path to the file we should sideload. |
||
133 | * @return void |
||
134 | * @since 4.10.5.p |
||
135 | */ |
||
136 | public function set_download_from($download_from) |
||
140 | |||
141 | |||
142 | /** |
||
143 | * sets the _permissions property used on the sideloaded file. |
||
144 | * |
||
145 | * @param int $permissions |
||
146 | * @return void |
||
147 | * @since 4.1.0 |
||
148 | */ |
||
149 | public function set_permissions($permissions) |
||
153 | |||
154 | |||
155 | /** |
||
156 | * sets the _new_file_name property used on the sideloaded file. |
||
157 | * |
||
158 | * @param string $new_file_name |
||
159 | * @return void |
||
160 | * @since 4.1.0 |
||
161 | */ |
||
162 | public function set_new_file_name($new_file_name) |
||
166 | |||
167 | // getters |
||
168 | |||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | * @since 4.1.0 |
||
173 | */ |
||
174 | public function get_upload_to() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | * @since 4.10.5.p |
||
183 | */ |
||
184 | public function get_download_from() |
||
188 | |||
189 | |||
190 | /** |
||
191 | * @return int |
||
192 | * @since 4.1.0 |
||
193 | */ |
||
194 | public function get_permissions() |
||
198 | |||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | * @since 4.1.0 |
||
203 | */ |
||
204 | public function get_new_file_name() |
||
208 | |||
209 | |||
210 | // upload methods |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Downloads the file using the WordPress HTTP API. |
||
215 | * |
||
216 | * @return bool |
||
217 | * @since 4.1.0 |
||
218 | */ |
||
219 | public function sideload() |
||
297 | |||
298 | |||
299 | /** |
||
300 | * returns TRUE if there IS an error, FALSE if there is NO ERROR |
||
301 | * |
||
302 | * @param array|WP_Error $response |
||
303 | * @return bool |
||
304 | * @throws RuntimeException |
||
305 | */ |
||
306 | private function isResponseError($response) |
||
328 | |||
329 | |||
330 | /** |
||
331 | * returns TRUE if there IS an error, FALSE if there is NO ERROR |
||
332 | * |
||
333 | * @param array $response |
||
334 | * @return bool |
||
335 | * @throws RuntimeException |
||
336 | */ |
||
337 | private function isDownloadError(array $response) |
||
362 | |||
363 | // deprecated |
||
364 | |||
365 | /** |
||
366 | * sets the _upload_from property to the location we should download the file from. |
||
367 | * |
||
368 | * @param string $upload_from The full path to the file we should sideload. |
||
369 | * @return void |
||
370 | * @deprecated since version 4.10.5.p |
||
371 | */ |
||
372 | public function set_upload_from($upload_from) |
||
384 | |||
385 | |||
386 | /** |
||
387 | * @return string |
||
388 | * @since 4.1.0 |
||
389 | * @deprecated since version 4.10.5.p |
||
390 | */ |
||
391 | public function get_upload_from() |
||
403 | } //end EEH_Sideloader class |
||
404 |