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 ) { |
||
73 | |||
74 | /** |
||
75 | * private function to load a gallery by an id |
||
76 | * @param $post_id |
||
77 | */ |
||
78 | private function load_by_id( $post_id ) { |
||
84 | |||
85 | /** |
||
86 | * private function to load a gallery by the slug. |
||
87 | * Will be used when loading gallery shortcodes |
||
88 | * @param $slug |
||
89 | */ |
||
90 | private function load_by_slug( $slug ) { |
||
105 | |||
106 | /** |
||
107 | * Static function to build a dynamic gallery that does not exist in the database |
||
108 | * @param $template |
||
109 | * @param $attachment_ids |
||
110 | * |
||
111 | * @return FooGallery |
||
112 | */ |
||
113 | public static function dynamic( $template, $attachment_ids ) { |
||
127 | |||
128 | /** |
||
129 | * Static function to load a Gallery instance by passing in a post object |
||
130 | * @static |
||
131 | * |
||
132 | * @param $post |
||
133 | * |
||
134 | * @return FooGallery |
||
135 | */ |
||
136 | public static function get( $post ) { |
||
139 | |||
140 | /** |
||
141 | * Static function to load a Gallery instance by post id |
||
142 | * |
||
143 | * @param $post_id |
||
144 | * |
||
145 | * @return FooGallery | boolean |
||
146 | */ |
||
147 | public static function get_by_id( $post_id ) { |
||
155 | |||
156 | /** |
||
157 | * Static function to load a gallery instance by passing in a gallery slug |
||
158 | * |
||
159 | * @param string $slug |
||
160 | * |
||
161 | * @return FooGallery | boolean |
||
162 | */ |
||
163 | public static function get_by_slug( $slug ) { |
||
171 | |||
172 | /** |
||
173 | * Get a setting using the current template and meta key |
||
174 | * @param $key |
||
175 | * @param $default |
||
176 | * |
||
177 | * @return mixed|null |
||
178 | */ |
||
179 | function get_setting( $key, $default ) { |
||
182 | |||
183 | /** |
||
184 | * Get a meta value using a full key |
||
185 | * @param $key |
||
186 | * @param $default |
||
187 | * |
||
188 | * @return mixed|null |
||
189 | */ |
||
190 | function get_meta( $key, $default ) { |
||
203 | |||
204 | function is_checked( $key, $default = false ) { |
||
211 | |||
212 | /** |
||
213 | * Returns the number of attachments in the current gallery |
||
214 | * @return int |
||
215 | */ |
||
216 | public function attachment_count() { |
||
219 | |||
220 | /** |
||
221 | * Checks if the gallery has attachments |
||
222 | * @return bool |
||
223 | */ |
||
224 | public function has_attachments() { |
||
227 | |||
228 | /** |
||
229 | * Checks if the gallery exists |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function does_exist() { |
||
235 | |||
236 | /** |
||
237 | * Returns true if the gallery is published |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function is_published() { |
||
243 | |||
244 | /** |
||
245 | * Returns true if the gallery is newly created and not yet saved |
||
246 | */ |
||
247 | public function is_new() { |
||
251 | |||
252 | /** |
||
253 | * Get a comma separated list of attachment ids |
||
254 | * @return string |
||
255 | */ |
||
256 | public function attachment_id_csv() { |
||
259 | |||
260 | /** |
||
261 | * Lazy load the attachments for the gallery |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function attachments() { |
||
273 | |||
274 | /** |
||
275 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
276 | * |
||
277 | * This forces the attachments to be fetched using the correct ordering. |
||
278 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
279 | * @param $query WP_Query |
||
280 | */ |
||
281 | public function force_gallery_ordering( $query ) { |
||
284 | |||
285 | /** |
||
286 | * Output the shortcode for the gallery |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function shortcode() { |
||
293 | |||
294 | /** |
||
295 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
296 | * |
||
297 | * @return int|mixed|string |
||
298 | */ |
||
299 | public function find_featured_attachment_id() { |
||
304 | |||
305 | /** |
||
306 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
307 | * |
||
308 | * @return bool|FooGalleryAttachment |
||
309 | */ |
||
310 | public function featured_attachment() { |
||
313 | |||
314 | /** |
||
315 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
316 | * |
||
317 | * @param string $size |
||
318 | * @param bool $icon |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
327 | |||
328 | /** |
||
329 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
330 | * |
||
331 | * Get an HTML img element representing the featured image for the gallery |
||
332 | * |
||
333 | * @param string $size Optional, default is 'thumbnail'. |
||
334 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
335 | * |
||
336 | * @return string HTML img element or empty string on failure. |
||
337 | */ |
||
338 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
343 | |||
344 | public function image_count() { |
||
364 | |||
365 | /** |
||
366 | * Returns a safe name for the gallery, in case there has been no title set |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function safe_name() { |
||
375 | |||
376 | public function find_usages() { |
||
391 | |||
392 | public function gallery_template_details() { |
||
404 | |||
405 | /** |
||
406 | * Returns the name of the gallery template |
||
407 | * @return string|void |
||
408 | */ |
||
409 | public function gallery_template_name() { |
||
416 | |||
417 | public function gallery_template_has_field_of_type( $field_type ) { |
||
431 | |||
432 | /** |
||
433 | * Loads default settings from another gallery if it is set on the settings page |
||
434 | */ |
||
435 | public function load_default_settings_if_new() { |
||
442 | |||
443 | /** |
||
444 | * Returns the current gallery datasource object |
||
445 | * |
||
446 | * @returns IFooGalleryDatasource |
||
447 | */ |
||
448 | public function datasource() { |
||
457 | } |
||
458 |
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.