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_VideoPress 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_VideoPress, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_VideoPress { |
||
| 8 | /** @var string */ |
||
| 9 | public $module = 'videopress'; |
||
| 10 | |||
| 11 | /** @var int */ |
||
| 12 | public $version = 5; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Singleton |
||
| 16 | */ |
||
| 17 | public static function init() { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Jetpack_VideoPress constructor. |
||
| 29 | * |
||
| 30 | * Sets up the initializer and makes sure that videopress activates and deactivates properly. |
||
| 31 | */ |
||
| 32 | private function __construct() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Fires on init |
||
| 40 | */ |
||
| 41 | public function on_init() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Runs when the VideoPress module is deactivated. |
||
| 56 | */ |
||
| 57 | public function jetpack_module_deactivated() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * A can of coke |
||
| 63 | * |
||
| 64 | * Similar to current_user_can, but internal to VideoPress. Returns |
||
| 65 | * true if the given VideoPress capability is allowed by the given user. |
||
| 66 | */ |
||
| 67 | public function can( $cap, $user_id = false ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns true if the provided user is the Jetpack connection owner. |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function is_connection_owner( $user_id = false ) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Register VideoPress admin scripts. |
||
| 109 | */ |
||
| 110 | public function enqueue_admin_scripts() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * An override for the attachment url, which returns back the WPCOM videopress original url, |
||
| 154 | * if it is set to the the objects metadata. this allows us to show the original uploaded |
||
| 155 | * file on the WPCOM architecture, instead of the locally uplodaded file, |
||
| 156 | * which doeasn't exist. |
||
| 157 | * |
||
| 158 | * @param string $url |
||
| 159 | * @param int $post_id |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function update_attachment_url_for_videopress( $url, $post_id ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Modify the default plupload config to turn on videopress specific filters. |
||
| 178 | */ |
||
| 179 | public function videopress_pluploder_config( $config ) { |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Helper function to determine if the media uploader should be overridden. |
||
| 196 | * |
||
| 197 | * The rules are simple, only try to load the script when on the edit post or new post pages. |
||
| 198 | * |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | protected function should_override_media_uploader() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * A work-around / hack to make it possible to go to the media library with the add new box open. |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function print_in_footer_open_media_add_new() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Changes the add new menu location, so that VideoPress will be enabled |
||
| 254 | * when a user clicks that button. |
||
| 255 | */ |
||
| 256 | public function change_add_new_menu_location() { |
||
| 261 | } |
||
| 262 | |||
| 265 |