Complex classes like GravityView_Compatibility 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 GravityView_Compatibility, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class GravityView_Compatibility { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var GravityView_Compatibility |
||
| 22 | */ |
||
| 23 | static public $instance = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool Is Gravity Forms version valid and is Gravity Forms loaded? |
||
| 27 | */ |
||
| 28 | static public $valid_gravity_forms = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool Is the WordPress installation compatible? |
||
| 32 | */ |
||
| 33 | static public $valid_wordpress = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool Is the server's PHP version compatible? |
||
| 37 | */ |
||
| 38 | static public $valid_php = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array Holder for notices to be displayed in frontend shortcodes if not valid GF |
||
| 42 | */ |
||
| 43 | static private $notices = array(); |
||
| 44 | |||
| 45 | function __construct() { |
||
| 57 | |||
| 58 | function add_hooks() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Add the compatibility notices to the other admin notices |
||
| 67 | * @param array $notices |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | function insert_admin_notices( $notices = array() ) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return GravityView_Compatibility |
||
| 77 | */ |
||
| 78 | public static function getInstance() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Is everything compatible with this version of GravityView? |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public static function is_valid() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Is the version of WordPress compatible? |
||
| 95 | * @since 1.12 |
||
| 96 | */ |
||
| 97 | private static function is_valid_wordpress() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @since 1.12 |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | private static function is_valid_gravity_forms() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @since 1.12 |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | private static function is_valid_php() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @since 1.12 |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | private function add_fallback_shortcode() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get admin notices |
||
| 136 | * @since 1.12 |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public static function get_notices() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @since 1.9.2 in gravityview.php |
||
| 145 | * @since 1.12 |
||
| 146 | * |
||
| 147 | * @param array $atts |
||
| 148 | * @param null $content |
||
| 149 | * @param string $shortcode |
||
| 150 | * |
||
| 151 | * @return null|string NULL returned if user can't activate plugins. Notice shown with a warning that GF isn't supported. |
||
| 152 | */ |
||
| 153 | public function _shortcode_gf_notice( $atts = array(), $content = null, $shortcode = 'gravityview' ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Is the version of PHP compatible? |
||
| 173 | * |
||
| 174 | * @since 1.12 |
||
| 175 | * @since 1.19.2 Shows a notice if it's compatible with future PHP version requirements |
||
| 176 | * |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public static function check_php() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Is WordPress compatible? |
||
| 212 | * |
||
| 213 | * @since 1.12 |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | public static function check_wordpress() { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Check if Gravity Forms plugin is active and show notice if not. |
||
| 237 | * |
||
| 238 | * @since 1.12 |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * @return boolean True: checks have been passed; GV is fine to run; False: checks have failed, don't continue loading |
||
| 242 | */ |
||
| 243 | public static function check_gravityforms() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check for potential conflicts and let users know about common issues. |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | private static function check_gf_directory() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if specified plugin is active, inactive or not installed |
||
| 337 | * |
||
| 338 | * @access public |
||
| 339 | * @static |
||
| 340 | * @param string $location (default: '') |
||
| 341 | * @return boolean|string True: plugin is active; False: plugin file doesn't exist at path; 'inactive' it's inactive |
||
| 342 | */ |
||
| 343 | public static function get_plugin_status( $location = '' ) { |
||
| 366 | |||
| 367 | } |
||
| 368 | |||
| 370 |
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.