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:
Complex classes like Jetpack_Media often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Media, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Jetpack_Media { |
||
| 9 | public static $WP_ORIGINAL_MEDIA = '_wp_original_post_media'; |
||
| 10 | public static $WP_REVISION_HISTORY = '_wp_revision_history'; |
||
| 11 | public static $REVISION_HISTORY_MAXIMUM_AMOUNT = 0; |
||
| 12 | public static $WP_ATTACHMENT_IMAGE_ALT = '_wp_attachment_image_alt'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Generate a filename in function of the original filename of the media. |
||
| 16 | * The returned name has the `{basename}-{hash}-{random-number}.{ext}` shape. |
||
| 17 | * The hash is built according to the filename trying to avoid name collisions |
||
| 18 | * with other media files. |
||
| 19 | * |
||
| 20 | * @param number $media_id - media post ID. |
||
| 21 | * @param string $new_filename - the new filename. |
||
| 22 | * @return string A random filename. |
||
| 23 | */ |
||
| 24 | public static function generate_new_filename( $media_id, $new_filename ) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * File urls use the post (image item) date to generate a folder path. |
||
| 65 | * Post dates can change, so we use the original date used in the `guid` |
||
| 66 | * url so edits can remain in the same folder. In the following function |
||
| 67 | * we capture a string in the format of `YYYY/MM` from the guid. |
||
| 68 | * |
||
| 69 | * For example with a guid of |
||
| 70 | * "http://test.files.wordpress.com/2016/10/test.png" the resulting string |
||
| 71 | * would be: "2016/10" |
||
| 72 | * |
||
| 73 | * @param number $media_id |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | View Code Duplication | private static function get_time_string_from_guid( $media_id ) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Return an array of allowed mime_type items used to upload a media file. |
||
| 91 | * |
||
| 92 | * @return array mime_type array |
||
| 93 | */ |
||
| 94 | View Code Duplication | static function get_allowed_mime_types( $default_mime_types ) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Checks that the mime type of the file |
||
| 110 | * is among those in a filterable list of mime types. |
||
| 111 | * |
||
| 112 | * @param string $file Path to file to get its mime type. |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | protected static function is_file_supported_for_sideloading( $file ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Try to remove the temporal file from the given file array. |
||
| 121 | * |
||
| 122 | * @param array $file_array Array with data about the temporal file |
||
| 123 | * @return bool `true` if the file has been removed. `false` either the file doesn't exist or it couldn't be removed. |
||
| 124 | */ |
||
| 125 | private static function remove_tmp_file( $file_array ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Save the given temporal file considering file type, |
||
| 134 | * correct location according to the original file path, etc. |
||
| 135 | * The file type control is done through of `jetpack_supported_media_sideload_types` filter, |
||
| 136 | * which allows define to the users their own file types list. |
||
| 137 | * |
||
| 138 | * @param array $file_array file to save |
||
| 139 | * @param number $media_id |
||
| 140 | * @return array|WP_Error an array with information about the new file saved or a WP_Error is something went wrong. |
||
| 141 | */ |
||
| 142 | public static function save_temporary_file( $file_array, $media_id ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return an object with an snapshot of a revision item. |
||
| 188 | * |
||
| 189 | * @param object $media_item - media post object |
||
| 190 | * @return object a revision item |
||
| 191 | */ |
||
| 192 | View Code Duplication | public static function get_snapshot( $media_item ) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Add a new item into revision_history array. |
||
| 210 | * |
||
| 211 | * @param object $media_item - media post object |
||
| 212 | * @param file $file - file recently added |
||
| 213 | * @param bool $has_original_media - condition is the original media has been already added |
||
| 214 | * @return bool `true` if the item has been added. Otherwise `false`. |
||
| 215 | */ |
||
| 216 | public static function register_revision( $media_item, $file, $has_original_media ) { |
||
| 223 | /** |
||
| 224 | * Return the `revision_history` of the given media. |
||
| 225 | * |
||
| 226 | * @param number $media_id - media post ID |
||
| 227 | * @return array `revision_history` array |
||
| 228 | */ |
||
| 229 | public static function get_revision_history( $media_id ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return the original media data |
||
| 235 | */ |
||
| 236 | public static function get_original_media( $media_id ) { |
||
| 241 | |||
| 242 | public static function delete_file( $pathname ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Try to delete a file according to the dirname of |
||
| 253 | * the media attached file and the filename. |
||
| 254 | * |
||
| 255 | * @param number $media_id - media post ID |
||
| 256 | * @param string $filename - basename of the file ( name-of-file.ext ) |
||
| 257 | * @return bool `true` is the file has been removed, `false` if not. |
||
| 258 | */ |
||
| 259 | private static function delete_media_history_file( $media_id, $filename ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Remove specific items from the `revision history` array |
||
| 281 | * depending on the given criteria: array( |
||
| 282 | * 'from' => (int) <from>, |
||
| 283 | * 'to' => (int) <to>, |
||
| 284 | * ) |
||
| 285 | * |
||
| 286 | * Also, it removes the file defined in each item. |
||
| 287 | * |
||
| 288 | * @param number $media_id - media post ID |
||
| 289 | * @param object $criteria - criteria to remove the items |
||
| 290 | * @param array [$revision_history] - revision history array |
||
| 291 | * @return array `revision_history` array updated. |
||
| 292 | */ |
||
| 293 | public static function remove_items_from_revision_history( $media_id, $criteria, $revision_history ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Limit the number of items of the `revision_history` array. |
||
| 323 | * When the stack is overflowing the oldest item is remove from there (FIFO). |
||
| 324 | * |
||
| 325 | * @param number $media_id - media post ID |
||
| 326 | * @param number [$limit] - maximun amount of items. 20 as default. |
||
| 327 | * @return array items removed from `revision_history` |
||
| 328 | */ |
||
| 329 | public static function limit_revision_history( $media_id, $limit = null) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Remove the original file and clean the post metadata. |
||
| 353 | * |
||
| 354 | * @param number $media_id - media post ID |
||
| 355 | */ |
||
| 356 | public static function clean_original_media( $media_id ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Clean `revision_history` of the given $media_id. it means: |
||
| 369 | * - remove all media files tied to the `revision_history` items. |
||
| 370 | * - clean `revision_history` meta data. |
||
| 371 | * - remove and clean the `original_media` |
||
| 372 | * |
||
| 373 | * @param number $media_id - media post ID |
||
| 374 | * @return array results of removing these files |
||
| 375 | */ |
||
| 376 | public static function clean_revision_history( $media_id ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Edit media item process: |
||
| 398 | * |
||
| 399 | * - update attachment file |
||
| 400 | * - preserve original media file |
||
| 401 | * - trace revision history |
||
| 402 | * |
||
| 403 | * @param number $media_id - media post ID. |
||
| 404 | * @param array $file_array - temporal file. |
||
| 405 | * @return {Post|WP_Error} Updated media item or a WP_Error is something went wrong. |
||
| 406 | */ |
||
| 407 | public static function edit_media_file( $media_id, $file_array ) { |
||
| 459 | } |
||
| 460 | |||
| 467 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.