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() { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * private gallery load function |
||
| 37 | * @param $post |
||
| 38 | */ |
||
| 39 | private function load( $post ) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * private meta data load function |
||
| 57 | * @param $post_id int |
||
| 58 | */ |
||
| 59 | private function load_meta( $post_id ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * private function to load a gallery by an id |
||
| 75 | * @param $post_id |
||
| 76 | */ |
||
| 77 | private function load_by_id( $post_id ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * private function to load a gallery by the slug. |
||
| 86 | * Will be used when loading gallery shortcodes |
||
| 87 | * @param $slug |
||
| 88 | */ |
||
| 89 | private function load_by_slug( $slug ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Static function to load a Gallery instance by passing in a post object |
||
| 107 | * @static |
||
| 108 | * |
||
| 109 | * @param $post |
||
| 110 | * |
||
| 111 | * @return FooGallery |
||
| 112 | */ |
||
| 113 | public static function get( $post ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Static function to load a Gallery instance by post id |
||
| 119 | * |
||
| 120 | * @param $post_id |
||
| 121 | * |
||
| 122 | * @return FooGallery | boolean |
||
| 123 | */ |
||
| 124 | public static function get_by_id( $post_id ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Static function to load a gallery instance by passing in a gallery slug |
||
| 135 | * |
||
| 136 | * @param string $slug |
||
| 137 | * |
||
| 138 | * @return FooGallery | boolean |
||
| 139 | */ |
||
| 140 | public static function get_by_slug( $slug ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get a setting using the current template and meta key |
||
| 151 | * @param $key |
||
| 152 | * @param $default |
||
| 153 | * |
||
| 154 | * @return mixed|null |
||
| 155 | */ |
||
| 156 | function get_setting( $key, $default ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get a meta value using a full key |
||
| 162 | * @param $key |
||
| 163 | * @param $default |
||
| 164 | * |
||
| 165 | * @return mixed|null |
||
| 166 | */ |
||
| 167 | function get_meta( $key, $default ) { |
||
| 180 | |||
| 181 | function is_checked( $key, $default = false ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the number of attachments in the current gallery |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function attachment_count() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Checks if the gallery has attachments |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function has_attachments() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Checks if the gallery exists |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function does_exist() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns true if the gallery is published |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function is_published() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns true if the gallery is newly created and not yet saved |
||
| 223 | */ |
||
| 224 | public function is_new() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get a comma separated list of attachment ids |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function attachment_id_csv() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Lazy load the attachments for the gallery |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function attachments() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 253 | * |
||
| 254 | * This forces the attachments to be fetched using the correct ordering. |
||
| 255 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
| 256 | * @param $query WP_Query |
||
| 257 | */ |
||
| 258 | public function force_gallery_ordering( $query ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Output the shortcode for the gallery |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function shortcode() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 273 | * |
||
| 274 | * @return int|mixed|string |
||
| 275 | */ |
||
| 276 | public function find_featured_attachment_id() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
| 284 | * |
||
| 285 | * @return bool|FooGalleryAttachment |
||
| 286 | */ |
||
| 287 | public function featured_attachment() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 293 | * |
||
| 294 | * @param string $size |
||
| 295 | * @param bool $icon |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
| 307 | * |
||
| 308 | * Get an HTML img element representing the featured image for the gallery |
||
| 309 | * |
||
| 310 | * @param string $size Optional, default is 'thumbnail'. |
||
| 311 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
| 312 | * |
||
| 313 | * @return string HTML img element or empty string on failure. |
||
| 314 | */ |
||
| 315 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
| 320 | |||
| 321 | public function image_count() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns a safe name for the gallery, in case there has been no title set |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function safe_name() { |
||
| 352 | |||
| 353 | public function find_usages() { |
||
| 368 | |||
| 369 | public function gallery_template_details() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the name of the gallery template |
||
| 384 | * @return string|void |
||
| 385 | */ |
||
| 386 | public function gallery_template_name() { |
||
| 393 | |||
| 394 | public function gallery_template_has_field_of_type( $field_type ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Loads default settings from another gallery if it is set on the settings page |
||
| 411 | */ |
||
| 412 | public function load_default_settings_if_new() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns the current gallery datasource object |
||
| 422 | * |
||
| 423 | * @returns IFooGalleryDatasource |
||
| 424 | */ |
||
| 425 | public function datasource() { |
||
| 434 | } |
||
| 435 |
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.