Complex classes like FooGallery 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class FooGallery extends stdClass { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * private constructor |
||
| 12 | * |
||
| 13 | * @param null $post |
||
| 14 | */ |
||
| 15 | private function __construct( $post = null ) { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Sets the default when a new gallery is instantiated |
||
| 25 | */ |
||
| 26 | private function set_defaults() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * private gallery load function |
||
| 38 | * @param $post |
||
| 39 | */ |
||
| 40 | private function load( $post ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * private meta data load function |
||
| 58 | * @param $post_id int |
||
| 59 | */ |
||
| 60 | private function load_meta( $post_id ) { |
||
| 72 | |||
| 73 | private function load_settings( $post_id ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * private function to load a gallery by an id |
||
| 96 | * @param $post_id |
||
| 97 | */ |
||
| 98 | private function load_by_id( $post_id ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * private function to load a gallery by the slug. |
||
| 107 | * Will be used when loading gallery shortcodes |
||
| 108 | * @param $slug |
||
| 109 | */ |
||
| 110 | private function load_by_slug( $slug ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Static function to build a dynamic gallery that does not exist in the database |
||
| 128 | * @param $template |
||
| 129 | * @param $attachment_ids |
||
| 130 | * |
||
| 131 | * @return FooGallery |
||
| 132 | */ |
||
| 133 | public static function dynamic( $template, $attachment_ids ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Static function to load a Gallery instance by passing in a post object |
||
| 150 | * @static |
||
| 151 | * |
||
| 152 | * @param $post |
||
| 153 | * |
||
| 154 | * @return FooGallery |
||
| 155 | */ |
||
| 156 | public static function get( $post ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Static function to load a Gallery instance by post id |
||
| 162 | * |
||
| 163 | * @param $post_id |
||
| 164 | * |
||
| 165 | * @return FooGallery | boolean |
||
| 166 | */ |
||
| 167 | public static function get_by_id( $post_id ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Static function to load a gallery instance by passing in a gallery slug |
||
| 178 | * |
||
| 179 | * @param string $slug |
||
| 180 | * |
||
| 181 | * @return FooGallery | boolean |
||
| 182 | */ |
||
| 183 | public static function get_by_slug( $slug ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get a setting using the current template and meta key |
||
| 194 | * @param $key |
||
| 195 | * @param $default |
||
| 196 | * |
||
| 197 | * @return mixed|null |
||
| 198 | */ |
||
| 199 | function get_setting( $key, $default ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get a meta value using a full key |
||
| 205 | * @param $key |
||
| 206 | * @param $default |
||
| 207 | * |
||
| 208 | * @return mixed|null |
||
| 209 | */ |
||
| 210 | function get_meta( $key, $default ) { |
||
| 223 | |||
| 224 | function is_checked( $key, $default = false ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the number of attachments in the current gallery |
||
| 234 | * @return int |
||
| 235 | */ |
||
| 236 | public function attachment_count() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Checks if the gallery has attachments |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function has_attachments() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Checks if the gallery exists |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function does_exist() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns true if the gallery is published |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function is_published() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns true if the gallery is newly created and not yet saved |
||
| 266 | */ |
||
| 267 | public function is_new() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get a comma separated list of attachment ids |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function attachment_id_csv() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Lazy load the attachments for the gallery |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function attachments() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 296 | * |
||
| 297 | * This forces the attachments to be fetched using the correct ordering. |
||
| 298 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
| 299 | * @param $query WP_Query |
||
| 300 | */ |
||
| 301 | public function force_gallery_ordering( $query ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Output the shortcode for the gallery |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function shortcode() { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 316 | * |
||
| 317 | * @return int|mixed|string |
||
| 318 | */ |
||
| 319 | public function find_featured_attachment_id() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
| 327 | * |
||
| 328 | * @return bool|FooGalleryAttachment |
||
| 329 | */ |
||
| 330 | public function featured_attachment() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 336 | * |
||
| 337 | * @param string $size |
||
| 338 | * @param bool $icon |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 350 | * |
||
| 351 | * Get an HTML img element representing the featured image for the gallery |
||
| 352 | * |
||
| 353 | * @param string $size Optional, default is 'thumbnail'. |
||
| 354 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
| 355 | * |
||
| 356 | * @return string HTML img element or empty string on failure. |
||
| 357 | */ |
||
| 358 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
| 363 | |||
| 364 | public function image_count() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns a safe name for the gallery, in case there has been no title set |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function safe_name() { |
||
| 395 | |||
| 396 | public function find_usages() { |
||
| 411 | |||
| 412 | public function gallery_template_details() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the name of the gallery template |
||
| 427 | * @return string|void |
||
| 428 | */ |
||
| 429 | public function gallery_template_name() { |
||
| 436 | |||
| 437 | public function gallery_template_has_field_of_type( $field_type ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Loads default settings from another gallery if it is set on the settings page |
||
| 454 | */ |
||
| 455 | public function load_default_settings_if_new() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the current gallery datasource object |
||
| 465 | * |
||
| 466 | * @returns IFooGalleryDatasource |
||
| 467 | */ |
||
| 468 | public function datasource() { |
||
| 477 | } |
||
| 478 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.