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 ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * private function to load a gallery by an id |
||
| 93 | * @param $post_id |
||
| 94 | */ |
||
| 95 | private function load_by_id( $post_id ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * private function to load a gallery by the slug. |
||
| 104 | * Will be used when loading gallery shortcodes |
||
| 105 | * @param $slug |
||
| 106 | */ |
||
| 107 | private function load_by_slug( $slug ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Static function to build a dynamic gallery that does not exist in the database |
||
| 125 | * @param $template |
||
| 126 | * @param $attachment_ids |
||
| 127 | * |
||
| 128 | * @return FooGallery |
||
| 129 | */ |
||
| 130 | public static function dynamic( $template, $attachment_ids ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Static function to load a Gallery instance by passing in a post object |
||
| 147 | * @static |
||
| 148 | * |
||
| 149 | * @param $post |
||
| 150 | * |
||
| 151 | * @return FooGallery |
||
| 152 | */ |
||
| 153 | public static function get( $post ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Static function to load a Gallery instance by post id |
||
| 159 | * |
||
| 160 | * @param $post_id |
||
| 161 | * |
||
| 162 | * @return FooGallery | boolean |
||
| 163 | */ |
||
| 164 | public static function get_by_id( $post_id ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Static function to load a gallery instance by passing in a gallery slug |
||
| 175 | * |
||
| 176 | * @param string $slug |
||
| 177 | * |
||
| 178 | * @return FooGallery | boolean |
||
| 179 | */ |
||
| 180 | public static function get_by_slug( $slug ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get a setting using the current template and meta key |
||
| 191 | * @param $key |
||
| 192 | * @param $default |
||
| 193 | * |
||
| 194 | * @return mixed|null |
||
| 195 | */ |
||
| 196 | function get_setting( $key, $default ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get a meta value using a full key |
||
| 202 | * @param $key |
||
| 203 | * @param $default |
||
| 204 | * |
||
| 205 | * @return mixed|null |
||
| 206 | */ |
||
| 207 | function get_meta( $key, $default ) { |
||
| 220 | |||
| 221 | function is_checked( $key, $default = false ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the number of attachments in the current gallery |
||
| 231 | * @return int |
||
| 232 | */ |
||
| 233 | public function attachment_count() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Checks if the gallery has attachments |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function has_attachments() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Checks if the gallery exists |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function does_exist() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns true if the gallery is published |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function is_published() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns true if the gallery is newly created and not yet saved |
||
| 263 | */ |
||
| 264 | public function is_new() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get a comma separated list of attachment ids |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function attachment_id_csv() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Lazy load the attachments for the gallery |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function attachments() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 293 | * |
||
| 294 | * This forces the attachments to be fetched using the correct ordering. |
||
| 295 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
| 296 | * @param $query WP_Query |
||
| 297 | */ |
||
| 298 | public function force_gallery_ordering( $query ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Output the shortcode for the gallery |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function shortcode() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 313 | * |
||
| 314 | * @return int|mixed|string |
||
| 315 | */ |
||
| 316 | public function find_featured_attachment_id() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
| 324 | * |
||
| 325 | * @return bool|FooGalleryAttachment |
||
| 326 | */ |
||
| 327 | public function featured_attachment() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 333 | * |
||
| 334 | * @param string $size |
||
| 335 | * @param bool $icon |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 347 | * |
||
| 348 | * Get an HTML img element representing the featured image for the gallery |
||
| 349 | * |
||
| 350 | * @param string $size Optional, default is 'thumbnail'. |
||
| 351 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
| 352 | * |
||
| 353 | * @return string HTML img element or empty string on failure. |
||
| 354 | */ |
||
| 355 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
| 360 | |||
| 361 | public function image_count() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns a safe name for the gallery, in case there has been no title set |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function safe_name() { |
||
| 392 | |||
| 393 | public function find_usages() { |
||
| 408 | |||
| 409 | public function gallery_template_details() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Returns the name of the gallery template |
||
| 424 | * @return string|void |
||
| 425 | */ |
||
| 426 | public function gallery_template_name() { |
||
| 433 | |||
| 434 | public function gallery_template_has_field_of_type( $field_type ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Loads default settings from another gallery if it is set on the settings page |
||
| 451 | */ |
||
| 452 | public function load_default_settings_if_new() { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Returns the current gallery datasource object |
||
| 462 | * |
||
| 463 | * @returns IFooGalleryDatasource |
||
| 464 | */ |
||
| 465 | public function datasource() { |
||
| 474 | } |
||
| 475 |
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.