Complex classes like FooGallery_Pro_Video_Legacy 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 FooGallery_Pro_Video_Legacy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FooGallery_Pro_Video_Legacy { |
||
|
|
|||
| 11 | |||
| 12 | function __construct() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Determines if a migration is needed |
||
| 60 | * |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | function migration_required() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Migrate the gallery settings |
||
| 89 | * |
||
| 90 | * @param $foogallery |
||
| 91 | * |
||
| 92 | * @return FooGallery |
||
| 93 | */ |
||
| 94 | function migrate_settings( $foogallery ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Short-circuit the post meta updates for the legacy FooVideo while both plugins are activated |
||
| 102 | * |
||
| 103 | * @param $check |
||
| 104 | * @param $object_id |
||
| 105 | * @param $meta_key |
||
| 106 | * @param $meta_value |
||
| 107 | * @param $prev_value |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | function short_circuit_legacy_video_count( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Migrate video for the gallery that is saved |
||
| 120 | * |
||
| 121 | * @param $post_id |
||
| 122 | * @param $post |
||
| 123 | */ |
||
| 124 | function migrate_gallery($post_id, $post) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Remove the legacy template fields added by FooVideo |
||
| 144 | * |
||
| 145 | * @param $fields |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | function remove_legacy_template_fields( $fields ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Rename the Video Slider template to include the text 'Deprecated' |
||
| 168 | * @param $templates |
||
| 169 | * |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | function rename_videoslider_template( $templates ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Legacy way of knowing if an attachment is a video |
||
| 184 | * |
||
| 185 | * @param $is_video |
||
| 186 | * @param $foogallery_attachment |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | function foogallery_is_attachment_video_legacy( $is_video, $foogallery_attachment ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Applies the legacy filter for backwards compatibility |
||
| 198 | * @param $url |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | function foogallery_clean_video_url_legacy_filter( $url ) { |
||
| 205 | |||
| 206 | public function foogallery_build_class_attribute( $classes ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Display a message if the FooVideo extension is also installed |
||
| 226 | */ |
||
| 227 | function display_foovideo_notice() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Outputs the video migration view |
||
| 255 | */ |
||
| 256 | function render_video_migration_view() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Add a new menu item for running the migration |
||
| 262 | */ |
||
| 263 | function add_migration_menu() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Handle the Video Migration Step from an AJAX call |
||
| 269 | */ |
||
| 270 | function ajax_foogallery_video_migration() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Handle the Video Migration Reset from an AJAX call |
||
| 282 | */ |
||
| 283 | function ajax_foogallery_video_migration_reset() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Override the Discount Offer admin notice title |
||
| 295 | * @param $title |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | function override_discount_offer_notice_title( $title ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Override the Discount Offer admin notice message |
||
| 306 | * @param $message |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | function override_discount_offer_notice_message( $message ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Override the Discount Offer menu |
||
| 317 | * @param $menu |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | function override_discount_offer_menu( $menu ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Override the Discount Offer page message |
||
| 328 | * @param $message |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | function override_discount_offer_message( $message ) { |
||
| 336 | |||
| 337 | |||
| 338 | } |
||
| 339 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.