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 |
||
| 22 | class Event extends Calculations_Base { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Calculates the total spaces on the event (not subtracting sales, but taking |
||
| 26 | * sales into account; so this is the optimum sales that CAN still be achieved) |
||
| 27 | * See EE_Event::total_available_spaces( true ); |
||
| 28 | * |
||
| 29 | * @param array $wpdb_row |
||
| 30 | * @param \WP_REST_Request $request |
||
| 31 | * @param Base $controller |
||
| 32 | * @return int |
||
| 33 | * @throws \EE_Error |
||
| 34 | */ |
||
| 35 | View Code Duplication | public static function optimum_sales_at_start( $wpdb_row, $request, $controller ){ |
|
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
||
| 58 | * sales that COULD have been achieved) |
||
| 59 | * See EE_Event::total_available_spaces( true ); |
||
| 60 | * |
||
| 61 | * @param array $wpdb_row |
||
| 62 | * @param \WP_REST_Request $request |
||
| 63 | * @param Base $controller |
||
| 64 | * @return int |
||
| 65 | * @throws \EE_Error |
||
| 66 | */ |
||
| 67 | View Code Duplication | public static function optimum_sales_now( $wpdb_row, $request, $controller ){ |
|
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Like optimum_sales_now, but minus total sales so far. |
||
| 90 | * See EE_Event::spaces_remaining_for_sale( true ); |
||
| 91 | * |
||
| 92 | * @param array $wpdb_row |
||
| 93 | * @param \WP_REST_Request $request |
||
| 94 | * @param Base $controller |
||
| 95 | * @return int |
||
| 96 | * @throws \EE_Error |
||
| 97 | */ |
||
| 98 | View Code Duplication | public static function spaces_remaining( $wpdb_row, $request, $controller ){ |
|
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Counts the number of approved registrations for this event (regardless |
||
| 121 | * of how many datetimes each registrations' ticket purchase is for) |
||
| 122 | * |
||
| 123 | * @param array $wpdb_row |
||
| 124 | * @param \WP_REST_Request $request |
||
| 125 | * @param Base $controller |
||
| 126 | * @return int |
||
| 127 | * @throws \EE_Error |
||
| 128 | */ |
||
| 129 | View Code Duplication | public static function spots_taken( $wpdb_row, $request, $controller ){ |
|
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Counts the number of pending-payment registrations for this event (regardless |
||
| 154 | * of how many datetimes each registrations' ticket purchase is for) |
||
| 155 | * |
||
| 156 | * @param array $wpdb_row |
||
| 157 | * @param \WP_REST_Request $request |
||
| 158 | * @param Base $controller |
||
| 159 | * @return int |
||
| 160 | * @throws \EE_Error |
||
| 161 | * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
||
| 162 | */ |
||
| 163 | View Code Duplication | public static function spots_taken_pending_payment( $wpdb_row, $request, $controller ){ |
|
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Counts all the registrations who have checked into one of this events' datetimes |
||
| 189 | * See EE_Event::total_available_spaces( false ); |
||
| 190 | * |
||
| 191 | * @param array $wpdb_row |
||
| 192 | * @param \WP_REST_Request $request |
||
| 193 | * @param Base $controller |
||
| 194 | * @return int|null if permission denied |
||
| 195 | * @throws \EE_Error |
||
| 196 | * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
||
| 197 | */ |
||
| 198 | View Code Duplication | public static function registrations_checked_in_count( $wpdb_row, $request, $controller ){ |
|
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Counts all the registrations who have checked out of one of this events' datetimes |
||
| 215 | * See EE_Event::total_available_spaces( false ); |
||
| 216 | * |
||
| 217 | * @param array $wpdb_row |
||
| 218 | * @param \WP_REST_Request $request |
||
| 219 | * @param Base $controller |
||
| 220 | * @return int |
||
| 221 | * @throws \EE_Error |
||
| 222 | * @throws \EventEspresso\core\libraries\rest_api\Rest_Exception |
||
| 223 | */ |
||
| 224 | View Code Duplication | public static function registrations_checked_out_count( $wpdb_row, $request, $controller ){ |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Gets the thumbnail image |
||
| 239 | * @param array $wpdb_row |
||
| 240 | * @param \WP_REST_Request $request |
||
| 241 | * @param Base $controller |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public static function image_thumbnail( $wpdb_row, $request, $controller ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets the medium image |
||
| 250 | * @param array $wpdb_row |
||
| 251 | * @param \WP_REST_Request $request |
||
| 252 | * @param Base $controller |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public static function image_medium( $wpdb_row, $request, $controller ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the medium-large image |
||
| 261 | * @param array $wpdb_row |
||
| 262 | * @param \WP_REST_Request $request |
||
| 263 | * @param Base $controller |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public static function image_medium_large( $wpdb_row, $request, $controller ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Gets the large image |
||
| 272 | * @param array $wpdb_row |
||
| 273 | * @param \WP_REST_Request $request |
||
| 274 | * @param Base $controller |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public static function image_large( $wpdb_row, $request, $controller ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Gets the post-thumbnail image |
||
| 283 | * @param array $wpdb_row |
||
| 284 | * @param \WP_REST_Request $request |
||
| 285 | * @param Base $controller |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public static function image_post_thumbnail( $wpdb_row, $request, $controller ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Gets the full size image |
||
| 294 | * @param array $wpdb_row |
||
| 295 | * @param \WP_REST_Request $request |
||
| 296 | * @param Base $controller |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public static function image_full( $wpdb_row, $request, $controller ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets image specs and formats them for the display in the API, |
||
| 305 | * according to the image size requested |
||
| 306 | * @param int $EVT_ID |
||
| 307 | * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
||
| 308 | * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
||
| 309 | */ |
||
| 310 | protected static function _calculate_image_data( $EVT_ID, $image_size ) { |
||
| 328 | } |
||
| 329 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: