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() { |
||
38 | |||
39 | /** |
||
40 | * private gallery load function |
||
41 | * @param $post |
||
42 | */ |
||
43 | private function load( $post ) { |
||
58 | |||
59 | /** |
||
60 | * private meta data load function |
||
61 | * @param $post_id int |
||
62 | */ |
||
63 | private function load_meta( $post_id ) { |
||
75 | |||
76 | private function load_settings( $post_id ) { |
||
96 | |||
97 | /** |
||
98 | * private function to load a gallery by an id |
||
99 | * @param $post_id |
||
100 | */ |
||
101 | private function load_by_id( $post_id ) { |
||
107 | |||
108 | /** |
||
109 | * private function to load a gallery by the slug. |
||
110 | * Will be used when loading gallery shortcodes |
||
111 | * @param $slug |
||
112 | */ |
||
113 | private function load_by_slug( $slug ) { |
||
128 | |||
129 | /** |
||
130 | * Static function to build a dynamic gallery that does not exist in the database |
||
131 | * @param $template |
||
132 | * @param $attachment_ids |
||
133 | * |
||
134 | * @return FooGallery |
||
135 | */ |
||
136 | public static function dynamic( $template, $attachment_ids ) { |
||
150 | |||
151 | /** |
||
152 | * Static function to load a Gallery instance by passing in a post object |
||
153 | * @static |
||
154 | * |
||
155 | * @param $post |
||
156 | * |
||
157 | * @return FooGallery |
||
158 | */ |
||
159 | public static function get( $post ) { |
||
162 | |||
163 | /** |
||
164 | * Static function to load a Gallery instance by post id |
||
165 | * |
||
166 | * @param $post_id |
||
167 | * |
||
168 | * @return FooGallery | boolean |
||
169 | */ |
||
170 | public static function get_by_id( $post_id ) { |
||
178 | |||
179 | /** |
||
180 | * Static function to load a gallery instance by passing in a gallery slug |
||
181 | * |
||
182 | * @param string $slug |
||
183 | * |
||
184 | * @return FooGallery | boolean |
||
185 | */ |
||
186 | public static function get_by_slug( $slug ) { |
||
194 | |||
195 | /** |
||
196 | * Get a setting using the current template and meta key |
||
197 | * @param $key |
||
198 | * @param $default |
||
199 | * |
||
200 | * @return mixed|null |
||
201 | */ |
||
202 | function get_setting( $key, $default ) { |
||
205 | |||
206 | /** |
||
207 | * Get a meta value using a full key |
||
208 | * @param $key |
||
209 | * @param $default |
||
210 | * |
||
211 | * @return mixed|null |
||
212 | */ |
||
213 | function get_meta( $key, $default ) { |
||
226 | |||
227 | function is_checked( $key, $default = false ) { |
||
234 | |||
235 | /** |
||
236 | * Returns the number of attachments in the current gallery |
||
237 | * @return int |
||
238 | */ |
||
239 | public function attachment_count() { |
||
242 | |||
243 | /** |
||
244 | * Checks if the gallery has attachments |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function has_attachments() { |
||
250 | |||
251 | /** |
||
252 | * Checks if the gallery exists |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function does_exist() { |
||
258 | |||
259 | /** |
||
260 | * Returns true if the gallery is published |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function is_published() { |
||
266 | |||
267 | /** |
||
268 | * Returns true if the gallery is newly created and not yet saved |
||
269 | */ |
||
270 | public function is_new() { |
||
274 | |||
275 | /** |
||
276 | * Get a comma separated list of attachment ids |
||
277 | * @return string |
||
278 | */ |
||
279 | public function attachment_id_csv() { |
||
282 | |||
283 | /** |
||
284 | * Lazy load the attachments for the gallery |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | public function attachments() { |
||
296 | |||
297 | /** |
||
298 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
299 | * |
||
300 | * This forces the attachments to be fetched using the correct ordering. |
||
301 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
302 | * @param $query WP_Query |
||
303 | */ |
||
304 | public function force_gallery_ordering( $query ) { |
||
307 | |||
308 | /** |
||
309 | * Output the shortcode for the gallery |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function shortcode() { |
||
316 | |||
317 | /** |
||
318 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
319 | * |
||
320 | * @return int|mixed|string |
||
321 | */ |
||
322 | public function find_featured_attachment_id() { |
||
327 | |||
328 | /** |
||
329 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
330 | * |
||
331 | * @return bool|FooGalleryAttachment |
||
332 | */ |
||
333 | public function featured_attachment() { |
||
336 | |||
337 | /** |
||
338 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
339 | * |
||
340 | * @param string $size |
||
341 | * @param bool $icon |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
350 | |||
351 | /** |
||
352 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
353 | * |
||
354 | * Get an HTML img element representing the featured image for the gallery |
||
355 | * |
||
356 | * @param string $size Optional, default is 'thumbnail'. |
||
357 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
358 | * |
||
359 | * @return string HTML img element or empty string on failure. |
||
360 | */ |
||
361 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
366 | |||
367 | public function image_count() { |
||
387 | |||
388 | /** |
||
389 | * Returns a safe name for the gallery, in case there has been no title set |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | public function safe_name() { |
||
398 | |||
399 | public function find_usages() { |
||
414 | |||
415 | public function gallery_template_details() { |
||
427 | |||
428 | /** |
||
429 | * Returns the name of the gallery template |
||
430 | * @return string|void |
||
431 | */ |
||
432 | public function gallery_template_name() { |
||
439 | |||
440 | public function gallery_template_has_field_of_type( $field_type ) { |
||
454 | |||
455 | /** |
||
456 | * Loads default settings from another gallery if it is set on the settings page |
||
457 | */ |
||
458 | public function load_default_settings_if_new() { |
||
465 | |||
466 | /** |
||
467 | * Returns the current gallery datasource object |
||
468 | * |
||
469 | * @returns IFooGalleryDatasource |
||
470 | */ |
||
471 | public function datasource() { |
||
480 | } |
||
481 |
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.