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 setters for doing so. |
||
| 44 | * |
||
| 45 | * @since 4.1.0 |
||
| 46 | * @param array $init array fo initializing the sideloader if keys match the properties. |
||
| 47 | */ |
||
| 48 | public function __construct($init = array()) |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * sets the properties for class either to defaults or using incoming initialization array |
||
| 56 | * |
||
| 57 | * @since 4.1.0 |
||
| 58 | * @param array $init array on init (keys match properties others ignored) |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | private function _init($init) |
||
| 62 | { |
||
| 63 | $defaults = array( |
||
| 64 | '_upload_to' => $this->_get_wp_uploads_dir(), |
||
| 65 | '_download_from' => '', |
||
| 66 | '_permissions' => 0644, |
||
| 67 | '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
||
| 68 | ); |
||
| 69 | |||
| 70 | $props = array_merge($defaults, $init); |
||
| 71 | |||
| 72 | foreach ($props as $property => $val) { |
||
| 73 | $setter = 'set' . $property; |
||
| 74 | View Code Duplication | if (method_exists($this, $setter)) { |
|
| 75 | $this->$setter($val); |
||
| 76 | } else { |
||
| 77 | // No setter found. |
||
| 78 | EE_Error::add_error( |
||
| 79 | sprintf( |
||
| 80 | esc_html__( |
||
| 81 | 'EEH_Sideloader::%1$s not found. There is no setter for the %2$s property.', |
||
| 82 | 'event_espresso' |
||
| 83 | ), |
||
| 84 | $setter, |
||
| 85 | $property |
||
| 86 | ), |
||
| 87 | __FILE__, |
||
| 88 | __FUNCTION__, |
||
| 89 | __LINE__ |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // make sure we include the required wp file for needed functions |
||
| 95 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | // utilities |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * @since 4.1.0 |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | private function _get_wp_uploads_dir() |
||
| 109 | |||
| 110 | // setters |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * sets the _upload_to property to the directory to upload to. |
||
| 115 | * |
||
| 116 | * @since 4.1.0 |
||
| 117 | * @param $upload_to_folder |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function set_upload_to($upload_to_folder) |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * sets the _download_from property to the location we should download the file from. |
||
| 128 | * |
||
| 129 | * @since 4.10.5.p |
||
| 130 | * @param string $download_from The full path to the file we should sideload. |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function set_download_from($download_from) |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * sets the _permissions property used on the sideloaded file. |
||
| 141 | * |
||
| 142 | * @since 4.1.0 |
||
| 143 | * @param int $permissions |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | public function set_permissions($permissions) |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * sets the _new_file_name property used on the sideloaded file. |
||
| 154 | * |
||
| 155 | * @since 4.1.0 |
||
| 156 | * @param string $new_file_name |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | public function set_new_file_name($new_file_name) |
||
| 163 | |||
| 164 | // getters |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * @since 4.1.0 |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function get_upload_to() |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @since 4.10.5.p |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function get_download_from() |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @since 4.1.0 |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function get_permissions() |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @since 4.1.0 |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function get_new_file_name() |
||
| 205 | |||
| 206 | |||
| 207 | // upload methods |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Downloads the file using the WordPress HTTP API. |
||
| 212 | * |
||
| 213 | * @since 4.1.0 |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function sideload() |
||
| 300 | |||
| 301 | // deprecated |
||
| 302 | |||
| 303 | /** |
||
| 304 | * sets the _upload_from property to the location we should download the file from. |
||
| 305 | * |
||
| 306 | * @param string $upload_from The full path to the file we should sideload. |
||
| 307 | * @return void |
||
| 308 | * @deprecated since version 4.10.5.p |
||
| 309 | */ |
||
| 310 | public function set_upload_from($upload_from) |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * @since 4.1.0 |
||
| 326 | * @return string |
||
| 327 | * @deprecated since version 4.10.5.p |
||
| 328 | */ |
||
| 329 | public function get_upload_from() |
||
| 341 | } //end EEH_Sideloader class |
||
| 342 |