Complex classes like FS_Admin_Notice_Manager 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 FS_Admin_Notice_Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class FS_Admin_Notice_Manager { |
||
14 | /** |
||
15 | * @since 1.2.2 |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $_module_unique_affix; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_id; |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $_title; |
||
28 | /** |
||
29 | * @var array[string]array |
||
30 | */ |
||
31 | private $_notices = array(); |
||
32 | /** |
||
33 | * @var FS_Key_Value_Storage |
||
34 | */ |
||
35 | private $_sticky_storage; |
||
36 | /** |
||
37 | * @var FS_Logger |
||
38 | */ |
||
39 | protected $_logger; |
||
40 | /** |
||
41 | * @since 2.0.0 |
||
42 | * @var int The ID of the blog that is associated with the current site level admin notices. |
||
43 | */ |
||
44 | private $_blog_id = 0; |
||
45 | /** |
||
46 | * @since 2.0.0 |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $_is_network_notices; |
||
50 | |||
51 | /** |
||
52 | * @var FS_Admin_Notice_Manager[] |
||
53 | */ |
||
54 | private static $_instances = array(); |
||
55 | |||
56 | /** |
||
57 | * @param string $id |
||
58 | * @param string $title |
||
59 | * @param string $module_unique_affix |
||
60 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on |
||
61 | * network and blog admin pages. |
||
62 | * @param bool $network_level_or_blog_id Since 2.0.0 |
||
63 | * |
||
64 | * @return \FS_Admin_Notice_Manager |
||
65 | */ |
||
66 | static function instance( |
||
103 | |||
104 | /** |
||
105 | * @param string $id |
||
106 | * @param string $title |
||
107 | * @param string $module_unique_affix |
||
108 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network and |
||
109 | * blog admin pages. |
||
110 | * @param bool|int $network_level_or_blog_id |
||
111 | */ |
||
112 | protected function __construct( |
||
169 | |||
170 | /** |
||
171 | * Remove sticky message by ID. |
||
172 | * |
||
173 | * @author Vova Feldman (@svovaf) |
||
174 | * @since 1.0.7 |
||
175 | * |
||
176 | */ |
||
177 | function dismiss_notice_ajax_callback() { |
||
181 | |||
182 | /** |
||
183 | * Rendered sticky message dismiss JavaScript. |
||
184 | * |
||
185 | * @author Vova Feldman (@svovaf) |
||
186 | * @since 1.0.7 |
||
187 | */ |
||
188 | static function _add_sticky_dismiss_javascript() { |
||
192 | |||
193 | private static $_added_sticky_javascript = false; |
||
194 | |||
195 | /** |
||
196 | * Hook to the admin_footer to add sticky message dismiss JavaScript handler. |
||
197 | * |
||
198 | * @author Vova Feldman (@svovaf) |
||
199 | * @since 1.0.7 |
||
200 | */ |
||
201 | private static function has_sticky_messages() { |
||
206 | |||
207 | /** |
||
208 | * Handle admin_notices by printing the admin messages stacked in the queue. |
||
209 | * |
||
210 | * @author Vova Feldman (@svovaf) |
||
211 | * @since 1.0.4 |
||
212 | * |
||
213 | */ |
||
214 | function _admin_notices_hook() { |
||
282 | |||
283 | /** |
||
284 | * Enqueue common stylesheet to style admin notice. |
||
285 | * |
||
286 | * @author Vova Feldman (@svovaf) |
||
287 | * @since 1.0.7 |
||
288 | */ |
||
289 | function _enqueue_styles() { |
||
292 | |||
293 | /** |
||
294 | * Check if the current page is the Gutenberg block editor. |
||
295 | * |
||
296 | * @author Vova Feldman (@svovaf) |
||
297 | * @since 2.2.3 |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | function is_gutenberg_page() { |
||
320 | |||
321 | /** |
||
322 | * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. |
||
323 | * |
||
324 | * @author Vova Feldman (@svovaf) |
||
325 | * @since 1.0.4 |
||
326 | * |
||
327 | * @param string $message |
||
328 | * @param string $title |
||
329 | * @param string $type |
||
330 | * @param bool $is_sticky |
||
331 | * @param string $id Message ID |
||
332 | * @param bool $store_if_sticky |
||
333 | * @param number|null $wp_user_id |
||
334 | * @param string|null $plugin_title |
||
335 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network |
||
336 | * and blog admin pages. |
||
337 | * |
||
338 | * @uses add_action() |
||
339 | */ |
||
340 | function add( |
||
385 | |||
386 | /** |
||
387 | * @author Vova Feldman (@svovaf) |
||
388 | * @since 1.0.7 |
||
389 | * |
||
390 | * @param string|string[] $ids |
||
391 | */ |
||
392 | function remove_sticky( $ids ) { |
||
406 | |||
407 | /** |
||
408 | * Check if sticky message exists by id. |
||
409 | * |
||
410 | * @author Vova Feldman (@svovaf) |
||
411 | * @since 1.0.9 |
||
412 | * |
||
413 | * @param $id |
||
414 | * |
||
415 | * @return bool |
||
416 | */ |
||
417 | function has_sticky( $id ) { |
||
420 | |||
421 | /** |
||
422 | * Adds sticky admin notification. |
||
423 | * |
||
424 | * @author Vova Feldman (@svovaf) |
||
425 | * @since 1.0.7 |
||
426 | * |
||
427 | * @param string $message |
||
428 | * @param string $id Message ID |
||
429 | * @param string $title |
||
430 | * @param string $type |
||
431 | * @param number|null $wp_user_id |
||
432 | * @param string|null $plugin_title |
||
433 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network |
||
434 | * and blog admin pages. |
||
435 | */ |
||
436 | function add_sticky( $message, $id, $title = '', $type = 'success', $wp_user_id = null, $plugin_title = null, $is_network_and_blog_admins = false ) { |
||
444 | |||
445 | /** |
||
446 | * Clear all sticky messages. |
||
447 | * |
||
448 | * @author Vova Feldman (@svovaf) |
||
449 | * @since 1.0.8 |
||
450 | */ |
||
451 | function clear_all_sticky() { |
||
454 | |||
455 | #-------------------------------------------------------------------------------- |
||
456 | #region Helper Method |
||
457 | #-------------------------------------------------------------------------------- |
||
458 | |||
459 | /** |
||
460 | * @author Vova Feldman (@svovaf) |
||
461 | * @since 2.0.0 |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | private function get_notices_type() { |
||
470 | |||
471 | #endregion |
||
472 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.