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 |
||
| 27 | class Event extends Calculations_Base |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Calculates the total spaces on the event (not subtracting sales, but taking |
||
| 32 | * sales into account; so this is the optimum sales that CAN still be achieved) |
||
| 33 | * See EE_Event::total_available_spaces( true ); |
||
| 34 | * |
||
| 35 | * @param array $wpdb_row |
||
| 36 | * @param \WP_REST_Request $request |
||
| 37 | * @param Base $controller |
||
| 38 | * @return int |
||
| 39 | * @throws EE_Error |
||
| 40 | */ |
||
| 41 | View Code Duplication | public static function optimumSalesAtStart($wpdb_row, $request, $controller) |
|
| 42 | { |
||
| 43 | if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) { |
||
| 44 | $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
||
| 45 | } else { |
||
| 46 | $event_obj = null; |
||
| 47 | } |
||
| 48 | if ($event_obj instanceof EE_Event) { |
||
| 49 | return $event_obj->total_available_spaces(); |
||
| 50 | } else { |
||
| 51 | throw new EE_Error( |
||
| 52 | sprintf( |
||
| 53 | __( |
||
| 54 | // @codingStandardsIgnoreStart |
||
| 55 | 'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
||
| 56 | // @codingStandardsIgnoreEnd |
||
| 57 | 'event_espresso' |
||
| 58 | ), |
||
| 59 | $wpdb_row['Event_CPT.ID'], |
||
| 60 | print_r($wpdb_row, true) |
||
| 61 | ) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
||
| 70 | * sales that COULD have been achieved) |
||
| 71 | * See EE_Event::total_available_spaces( true ); |
||
| 72 | * |
||
| 73 | * @param array $wpdb_row |
||
| 74 | * @param \WP_REST_Request $request |
||
| 75 | * @param Base $controller |
||
| 76 | * @return int |
||
| 77 | * @throws EE_Error |
||
| 78 | */ |
||
| 79 | View Code Duplication | public static function optimumSalesNow($wpdb_row, $request, $controller) |
|
| 80 | { |
||
| 81 | if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) { |
||
| 82 | $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
||
| 83 | } else { |
||
| 84 | $event_obj = null; |
||
| 85 | } |
||
| 86 | if ($event_obj instanceof EE_Event) { |
||
| 87 | return $event_obj->total_available_spaces(true); |
||
| 88 | } else { |
||
| 89 | throw new EE_Error( |
||
| 90 | sprintf( |
||
| 91 | __( |
||
| 92 | // @codingStandardsIgnoreStart |
||
| 93 | 'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
||
| 94 | // @codingStandardsIgnoreEnd |
||
| 95 | 'event_espresso' |
||
| 96 | ), |
||
| 97 | $wpdb_row['Event_CPT.ID'], |
||
| 98 | print_r($wpdb_row, true) |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Like optimum_sales_now, but minus total sales so far. |
||
| 108 | * See EE_Event::spaces_remaining_for_sale( true ); |
||
| 109 | * |
||
| 110 | * @param array $wpdb_row |
||
| 111 | * @param \WP_REST_Request $request |
||
| 112 | * @param Base $controller |
||
| 113 | * @return int |
||
| 114 | * @throws EE_Error |
||
| 115 | */ |
||
| 116 | View Code Duplication | public static function spacesRemaining($wpdb_row, $request, $controller) |
|
| 117 | { |
||
| 118 | if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) { |
||
| 119 | $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
||
| 120 | } else { |
||
| 121 | $event_obj = null; |
||
| 122 | } |
||
| 123 | if ($event_obj instanceof EE_Event) { |
||
| 124 | return $event_obj->spaces_remaining_for_sale(); |
||
| 125 | } else { |
||
| 126 | throw new EE_Error( |
||
| 127 | sprintf( |
||
| 128 | __( |
||
| 129 | // @codingStandardsIgnoreStart |
||
| 130 | 'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
||
| 131 | // @codingStandardsIgnoreEnd |
||
| 132 | 'event_espresso' |
||
| 133 | ), |
||
| 134 | $wpdb_row['Event_CPT.ID'], |
||
| 135 | print_r($wpdb_row, true) |
||
| 136 | ) |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Counts the number of approved registrations for this event (regardless |
||
| 145 | * of how many datetimes each registrations' ticket purchase is for) |
||
| 146 | * |
||
| 147 | * @param array $wpdb_row |
||
| 148 | * @param \WP_REST_Request $request |
||
| 149 | * @param Base $controller |
||
| 150 | * @return int |
||
| 151 | * @throws EE_Error |
||
| 152 | */ |
||
| 153 | public static function spotsTaken($wpdb_row, $request, $controller) |
||
| 154 | { |
||
| 155 | if (! (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID']))) { |
||
| 156 | throw new EE_Error( |
||
| 157 | sprintf( |
||
| 158 | __( |
||
| 159 | // @codingStandardsIgnoreStart |
||
| 160 | 'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
||
| 161 | // @codingStandardsIgnoreEnd |
||
| 162 | 'event_espresso' |
||
| 163 | ), |
||
| 164 | print_r($wpdb_row, true) |
||
| 165 | ) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | return EEM_Registration::instance()->count( |
||
| 169 | array( |
||
| 170 | array( |
||
| 171 | 'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
||
| 172 | 'STS_ID' => EEM_Registration::status_id_approved, |
||
| 173 | ), |
||
| 174 | ), |
||
| 175 | 'REG_ID', |
||
| 176 | true |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Counts the number of pending-payment registrations for this event (regardless |
||
| 184 | * of how many datetimes each registrations' ticket purchase is for) |
||
| 185 | * |
||
| 186 | * @param array $wpdb_row |
||
| 187 | * @param \WP_REST_Request $request |
||
| 188 | * @param Base $controller |
||
| 189 | * @return int |
||
| 190 | * @throws EE_Error |
||
| 191 | * @throws RestException |
||
| 192 | */ |
||
| 193 | View Code Duplication | public static function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
|
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Counts all the registrations who have checked into one of this events' datetimes |
||
| 225 | * See EE_Event::total_available_spaces( false ); |
||
| 226 | * |
||
| 227 | * @param array $wpdb_row |
||
| 228 | * @param \WP_REST_Request $request |
||
| 229 | * @param Base $controller |
||
| 230 | * @return int|null if permission denied |
||
| 231 | * @throws EE_Error |
||
| 232 | * @throws RestException |
||
| 233 | */ |
||
| 234 | View Code Duplication | public static function registrationsCheckedInCount($wpdb_row, $request, $controller) |
|
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Counts all the registrations who have checked out of one of this events' datetimes |
||
| 257 | * See EE_Event::total_available_spaces( false ); |
||
| 258 | * |
||
| 259 | * @param array $wpdb_row |
||
| 260 | * @param \WP_REST_Request $request |
||
| 261 | * @param Base $controller |
||
| 262 | * @return int |
||
| 263 | * @throws EE_Error |
||
| 264 | * @throws RestException |
||
| 265 | */ |
||
| 266 | View Code Duplication | public static function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
|
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Gets the thumbnail image |
||
| 289 | * |
||
| 290 | * @param array $wpdb_row |
||
| 291 | * @param \WP_REST_Request $request |
||
| 292 | * @param Base $controller |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public static function imageThumbnail($wpdb_row, $request, $controller) |
||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Gets the medium image |
||
| 304 | * |
||
| 305 | * @param array $wpdb_row |
||
| 306 | * @param \WP_REST_Request $request |
||
| 307 | * @param Base $controller |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public static function imageMedium($wpdb_row, $request, $controller) |
||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the medium-large image |
||
| 319 | * |
||
| 320 | * @param array $wpdb_row |
||
| 321 | * @param \WP_REST_Request $request |
||
| 322 | * @param Base $controller |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | public static function imageMediumLarge($wpdb_row, $request, $controller) |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Gets the large image |
||
| 334 | * |
||
| 335 | * @param array $wpdb_row |
||
| 336 | * @param \WP_REST_Request $request |
||
| 337 | * @param Base $controller |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | public static function imageLarge($wpdb_row, $request, $controller) |
||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Gets the post-thumbnail image |
||
| 349 | * |
||
| 350 | * @param array $wpdb_row |
||
| 351 | * @param \WP_REST_Request $request |
||
| 352 | * @param Base $controller |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public static function imagePostThumbnail($wpdb_row, $request, $controller) |
||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Gets the full size image |
||
| 364 | * |
||
| 365 | * @param array $wpdb_row |
||
| 366 | * @param \WP_REST_Request $request |
||
| 367 | * @param Base $controller |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public static function imageFull($wpdb_row, $request, $controller) |
||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Gets image specs and formats them for the display in the API, |
||
| 379 | * according to the image size requested |
||
| 380 | * |
||
| 381 | * @param int $EVT_ID |
||
| 382 | * @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
||
| 383 | * @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
||
| 384 | */ |
||
| 385 | protected static function calculateImageData($EVT_ID, $image_size) |
||
| 404 | } |
||
| 405 |
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: