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() { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Run legacy override hooks and filters |
||
| 28 | */ |
||
| 29 | function load_legacy_overrides() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Determines if a migration is needed |
||
| 69 | * |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | function migration_required() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Migrate the gallery settings |
||
| 98 | * |
||
| 99 | * @param $foogallery |
||
| 100 | * |
||
| 101 | * @return FooGallery |
||
| 102 | */ |
||
| 103 | function migrate_settings( $foogallery ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Short-circuit the post meta updates for the legacy FooVideo while both plugins are activated |
||
| 111 | * |
||
| 112 | * @param $check |
||
| 113 | * @param $object_id |
||
| 114 | * @param $meta_key |
||
| 115 | * @param $meta_value |
||
| 116 | * @param $prev_value |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | function short_circuit_legacy_video_count( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Migrate video for the gallery that is saved |
||
| 129 | * |
||
| 130 | * @param $post_id |
||
| 131 | * @param $post |
||
| 132 | */ |
||
| 133 | function migrate_gallery($post_id, $post) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Remove the legacy template fields added by FooVideo |
||
| 153 | * |
||
| 154 | * @param $fields |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | function remove_legacy_template_fields( $fields ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Rename the Video Slider template to include the text 'Deprecated' |
||
| 177 | * @param $templates |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | function rename_videoslider_template( $templates ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Legacy way of knowing if an attachment is a video |
||
| 193 | * |
||
| 194 | * @param $is_video |
||
| 195 | * @param $foogallery_attachment |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | function foogallery_is_attachment_video_legacy( $is_video, $foogallery_attachment ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Applies the legacy filter for backwards compatibility |
||
| 207 | * @param $url |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | function foogallery_clean_video_url_legacy_filter( $url ) { |
||
| 214 | |||
| 215 | public function foogallery_build_class_attribute( $classes ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Display a message if the FooVideo extension is also installed |
||
| 235 | */ |
||
| 236 | function display_foovideo_notice() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Outputs the video migration view |
||
| 264 | */ |
||
| 265 | function render_video_migration_view() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Add a new menu item for running the migration |
||
| 271 | */ |
||
| 272 | function add_migration_menu() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Handle the Video Migration Step from an AJAX call |
||
| 278 | */ |
||
| 279 | function ajax_foogallery_video_migration() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Handle the Video Migration Reset from an AJAX call |
||
| 291 | */ |
||
| 292 | function ajax_foogallery_video_migration_reset() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Override the Discount Offer admin notice title |
||
| 304 | * @param $title |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | function override_discount_offer_notice_title( $title ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Override the Discount Offer admin notice message |
||
| 315 | * @param $message |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | function override_discount_offer_notice_message( $message ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Override the Discount Offer menu |
||
| 326 | * @param $menu |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | function override_discount_offer_menu( $menu ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Override the Discount Offer page message |
||
| 337 | * @param $message |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | function override_discount_offer_message( $message ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Override the pricing page menu text |
||
| 348 | * @param $text |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | function override_pricing_menu_text( $text ) { |
||
| 356 | } |
||
| 357 | } |
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.