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 ) { |
||
69 | |||
70 | /** |
||
71 | * private function to load a gallery by an id |
||
72 | * @param $post_id |
||
73 | */ |
||
74 | private function load_by_id( $post_id ) { |
||
80 | |||
81 | /** |
||
82 | * private function to load a gallery by the slug. |
||
83 | * Will be used when loading gallery shortcodes |
||
84 | * @param $slug |
||
85 | */ |
||
86 | private function load_by_slug( $slug ) { |
||
101 | |||
102 | /** |
||
103 | * Static function to load a Gallery instance by passing in a post object |
||
104 | * @static |
||
105 | * |
||
106 | * @param $post |
||
107 | * |
||
108 | * @return FooGallery |
||
109 | */ |
||
110 | public static function get( $post ) { |
||
113 | |||
114 | /** |
||
115 | * Static function to load a Gallery instance by post id |
||
116 | * |
||
117 | * @param $post_id |
||
118 | * |
||
119 | * @return FooGallery | boolean |
||
120 | */ |
||
121 | public static function get_by_id( $post_id ) { |
||
129 | |||
130 | /** |
||
131 | * Static function to load a gallery instance by passing in a gallery slug |
||
132 | * |
||
133 | * @param string $slug |
||
134 | * |
||
135 | * @return FooGallery | boolean |
||
136 | */ |
||
137 | public static function get_by_slug( $slug ) { |
||
145 | |||
146 | function get_meta( $key, $default ) { |
||
159 | |||
160 | function is_checked( $key, $default = false ) { |
||
167 | |||
168 | /** |
||
169 | * Returns the number of attachments in the current gallery |
||
170 | * @return int |
||
171 | */ |
||
172 | public function attachment_count() { |
||
175 | |||
176 | /** |
||
177 | * Checks if the gallery has attachments |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function has_attachments() { |
||
183 | |||
184 | /** |
||
185 | * Checks if the gallery exists |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function does_exist() { |
||
191 | |||
192 | /** |
||
193 | * Returns true if the gallery is published |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function is_published() { |
||
199 | |||
200 | /** |
||
201 | * Returns true if the gallery is newly created and not yet saved |
||
202 | */ |
||
203 | public function is_new() { |
||
207 | |||
208 | /** |
||
209 | * Get a comma separated list of attachment ids |
||
210 | * @return string |
||
211 | */ |
||
212 | public function attachment_id_csv() { |
||
215 | |||
216 | /** |
||
217 | * Lazy load the attachments for the gallery |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | public function attachments() { |
||
229 | |||
230 | /** |
||
231 | * @deprecated 1.3.0 This is now moved into the datasource implementation |
||
232 | * |
||
233 | * This forces the attachments to be fetched using the correct ordering. |
||
234 | * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
||
235 | * @param $query WP_Query |
||
236 | */ |
||
237 | public function force_gallery_ordering( $query ) { |
||
240 | |||
241 | /** |
||
242 | * Output the shortcode for the gallery |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function shortcode() { |
||
249 | |||
250 | public function find_featured_attachment_id() { |
||
260 | |||
261 | /** |
||
262 | * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery |
||
263 | * |
||
264 | * @return bool|FooGalleryAttachment |
||
265 | */ |
||
266 | public function featured_attachment() { |
||
275 | |||
276 | public function featured_image_src( $size = 'thumbnail', $icon = false ) { |
||
283 | |||
284 | /** |
||
285 | * Get an HTML img element representing the featured image for the gallery |
||
286 | * |
||
287 | * @param string $size Optional, default is 'thumbnail'. |
||
288 | * @param bool $icon Optional, default is false. Whether it is an icon. |
||
289 | * |
||
290 | * @return string HTML img element or empty string on failure. |
||
291 | */ |
||
292 | public function featured_image_html( $size = 'thumbnail', $icon = false ) { |
||
299 | |||
300 | public function image_count() { |
||
320 | |||
321 | /** |
||
322 | * Returns a safe name for the gallery, in case there has been no title set |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function safe_name() { |
||
331 | |||
332 | public function find_usages() { |
||
347 | |||
348 | public function gallery_template_details() { |
||
360 | |||
361 | /** |
||
362 | * Returns the name of the gallery template |
||
363 | * @return string|void |
||
364 | */ |
||
365 | public function gallery_template_name() { |
||
372 | |||
373 | public function gallery_template_has_field_of_type( $field_type ) { |
||
387 | |||
388 | /** |
||
389 | * Loads default settings from another gallery if it is set on the settings page |
||
390 | */ |
||
391 | public function load_default_settings_if_new() { |
||
398 | |||
399 | /** |
||
400 | * Returns the current gallery datasource object |
||
401 | * |
||
402 | * @returns IFooGalleryDatasource |
||
403 | */ |
||
404 | public function datasource() { |
||
413 | } |
||
414 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.