Complex classes like Extension 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 Extension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class Extension { |
||
15 | /** |
||
16 | * @var string Name of the plugin in gravityview.co |
||
17 | */ |
||
18 | protected $_title = NULL; |
||
19 | |||
20 | /** |
||
21 | * @var string Version number of the plugin |
||
22 | */ |
||
23 | protected $_version = NULL; |
||
24 | |||
25 | /** |
||
26 | * @var int The ID of the download on gravityview.co |
||
27 | * @since 1.1 |
||
28 | */ |
||
29 | protected $_item_id = NULL; |
||
30 | |||
31 | /** |
||
32 | * @var string Translation textdomain |
||
33 | */ |
||
34 | protected $_text_domain = 'gravityview'; |
||
35 | |||
36 | /** |
||
37 | * @var string Minimum version of GravityView the Extension requires |
||
38 | */ |
||
39 | protected $_min_gravityview_version = '2.0-dev'; |
||
40 | |||
41 | /** |
||
42 | * @var string Maximum version of GravityView the Extension requires, if any |
||
43 | */ |
||
44 | protected $_max_gravityview_version = null; |
||
45 | |||
46 | /** |
||
47 | * @var string Minimum version of GravityView the Extension requires |
||
48 | */ |
||
49 | protected $_min_php_version = '5.3'; |
||
50 | |||
51 | /** |
||
52 | * @var string The URL to fetch license info from. Do not change unless you know what you're doing. |
||
53 | */ |
||
54 | protected $_remote_update_url = 'https://gravityview.co'; |
||
55 | |||
56 | /** |
||
57 | * @var string Author of plugin, sent when fetching license info. |
||
58 | */ |
||
59 | protected $_author = 'Katz Web Services, Inc.'; |
||
60 | |||
61 | /** |
||
62 | * @var string The path to the extension. |
||
63 | */ |
||
64 | protected $_path = ''; |
||
65 | |||
66 | /** |
||
67 | * @var array Admin notices to display |
||
68 | */ |
||
69 | static protected $admin_notices = array(); |
||
70 | |||
71 | /** |
||
72 | * @var boolean[] An array of extension compatibility. |
||
73 | * @since 2.0 This is an array of classes instead. |
||
74 | */ |
||
75 | static public $is_compatible = array(); |
||
76 | |||
77 | /** |
||
78 | * Generic initialization. |
||
79 | */ |
||
80 | 1 | public function __construct() { |
|
104 | |||
105 | /** |
||
106 | * Extensions should override this hook to add their hooks. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function add_hooks() { } |
||
111 | |||
112 | /** |
||
113 | * Save extra view configuration. |
||
114 | * |
||
115 | * @param int $post_id Post ID |
||
116 | * @return void |
||
117 | */ |
||
118 | public function save_post( $post_id ) { } |
||
119 | |||
120 | /** |
||
121 | * Add tooltips for the extension. |
||
122 | * |
||
123 | * Add a tooltip with an array using the `title` and `value` keys. The `title` key is the H6 tag value of the tooltip; it's the headline. The `value` is the tooltip content, and can contain any HTML. |
||
124 | * |
||
125 | * The tooltip key must be `gv_{name_of_setting}`. If the name of the setting is "example_extension_setting", the code would be: |
||
126 | * |
||
127 | * <code> |
||
128 | * $tooltips['gv_example_extension_setting'] = array( |
||
129 | * 'title' => 'About Example Extension Setting', |
||
130 | * 'value' => 'When you do [x] with [y], [z] happens.' |
||
131 | * ); |
||
132 | * </code> |
||
133 | * |
||
134 | * @param array $tooltips Existing GV tooltips, with `title` and `value` keys |
||
135 | * @return array Modified tooltips |
||
136 | */ |
||
137 | 1 | public function tooltips( $tooltips = array() ) { |
|
140 | |||
141 | /** |
||
142 | * Add a tab to GravityView Edit View tabbed metabox. By overriding this method, you will add a tab to View settings |
||
143 | * |
||
144 | * @since 1.8 (Extension version 1.0.7) |
||
145 | * @see https://gist.github.com/zackkatz/6cc381bcf54849f2ed41 For example of adding a metabox |
||
146 | * |
||
147 | * @return array Array of metabox |
||
148 | */ |
||
149 | protected function tab_settings() { |
||
153 | |||
154 | /** |
||
155 | * If Extension overrides tab_settings() and passes its own tab, add it to the tabbed settings metabox |
||
156 | * |
||
157 | * @since 1.8 (Extension version 1.0.7) |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | 1 | public function add_metabox_tab() { |
|
192 | |||
193 | /** |
||
194 | * Is this extension even compatible? |
||
195 | * |
||
196 | * @return boolean|null Is or is not. Null if unknown yet. |
||
197 | */ |
||
198 | 1 | public static function is_compatible() { |
|
201 | |||
202 | /** |
||
203 | * Check whether the extension is supported: |
||
204 | * |
||
205 | * - Checks if GravityView and Gravity Forms exist |
||
206 | * - Checks GravityView and Gravity Forms version numbers |
||
207 | * - Checks PHP version numbers |
||
208 | * - Sets self::$is_compatible[__CLASS__] to boolean value |
||
209 | * |
||
210 | * @return boolean Is the extension supported? |
||
211 | */ |
||
212 | 1 | protected function is_extension_supported() { |
|
237 | |||
238 | /** |
||
239 | * Load translations for the extension |
||
240 | * |
||
241 | * 1. Check `wp-content/languages/gravityview/` folder and load using `load_textdomain()` |
||
242 | * 2. Check `wp-content/plugins/gravityview/languages/` folder for `gravityview-[locale].mo` file and load using `load_textdomain()` |
||
243 | * 3. Load default file using `load_plugin_textdomain()` from `wp-content/plugins/gravityview/languages/` |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | 1 | public function load_plugin_textdomain() { |
|
285 | |||
286 | /** |
||
287 | * Register the updater for the Extension using GravityView license information |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | 1 | public function settings() { |
|
316 | |||
317 | /** |
||
318 | * Get license information from GravityView |
||
319 | * |
||
320 | * @since 1.8 (Extension version 1.0.7) |
||
321 | * |
||
322 | * @return bool|array False: \GV\Addon_Settings class does not exist. Array: array of GV license data. |
||
323 | */ |
||
324 | 1 | protected function get_license() { |
|
330 | |||
331 | /** |
||
332 | * Add a notice to be displayed in the admin. |
||
333 | * |
||
334 | * @param array $notice Array with `class` and `message` keys. The message is not escaped. |
||
335 | * |
||
336 | * @return void |
||
337 | */ |
||
338 | 1 | public static function add_notice( $notice = array() ) { |
|
351 | |||
352 | /** |
||
353 | * Outputs the admin notices generated by the all plugins |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | 1 | public function admin_notice() { |
|
372 | } |
||
373 |
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.