Complex classes like Widget 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 Widget, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class Widget { |
||
15 | /** |
||
16 | * Widget admin label |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $widget_label = ''; |
||
20 | |||
21 | /** |
||
22 | * Widget description, shown on the "+ Add Widget" picker |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $widget_description = ''; |
||
26 | |||
27 | /** |
||
28 | * Widget details, shown in the widget lightbox |
||
29 | * @since 1.8 |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $widget_subtitle = ''; |
||
33 | |||
34 | /** |
||
35 | * Widget admin ID |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $widget_id = ''; |
||
39 | |||
40 | /** |
||
41 | * Default configuration for header and footer |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $defaults = array(); |
||
45 | |||
46 | /** |
||
47 | * Widget admin advanced settings |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $settings = array(); |
||
51 | |||
52 | /** |
||
53 | * Allow class to automatically add widget_text filter for you in shortcode |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $shortcode_name; |
||
57 | |||
58 | /** |
||
59 | * Hold the widget options. |
||
60 | * @var array() |
||
61 | */ |
||
62 | private $widget_options = array(); |
||
63 | |||
64 | /** |
||
65 | * The position of the widget. |
||
66 | * @api |
||
67 | * @since 2.0 |
||
68 | * @var string |
||
69 | */ |
||
70 | public $position = ''; |
||
71 | |||
72 | /** |
||
73 | * A unique ID for this widget. |
||
74 | * @api |
||
75 | * @since 2.0 |
||
76 | * @var string |
||
77 | */ |
||
78 | public $UID = ''; |
||
79 | |||
80 | /** |
||
81 | * The actual configuration for this widget instance. |
||
82 | * |
||
83 | * @api |
||
84 | * @since 2.0 |
||
85 | * @var \GV\Settings |
||
86 | */ |
||
87 | public $configuration; |
||
88 | |||
89 | /** |
||
90 | * Constructor. |
||
91 | * |
||
92 | * @param string $label The Widget label as shown in the admin. |
||
93 | * @param string $id The Widget ID, make this something unique. |
||
94 | * @param array $defaults Default footer/header Widget configuration. |
||
95 | * @param array $settings Advanced Widget settings. |
||
96 | * |
||
97 | * @return \GV\Widget |
||
|
|||
98 | */ |
||
99 | 40 | public function __construct( $label, $id, $defaults = array(), $settings = array() ) { |
|
137 | |||
138 | /** |
||
139 | * Define general widget settings |
||
140 | * @since 1.5.4 |
||
141 | * @return array $settings Default settings |
||
142 | */ |
||
143 | 40 | protected function get_default_settings() { |
|
165 | |||
166 | /** |
||
167 | * Get the Widget ID. |
||
168 | * |
||
169 | * @return string The Widget ID. |
||
170 | */ |
||
171 | 46 | public function get_widget_id() { |
|
174 | |||
175 | /** |
||
176 | * Get the widget settings |
||
177 | * |
||
178 | * @return array|null Settings array; NULL if not set for some reason. |
||
179 | */ |
||
180 | 1 | public function get_settings() { |
|
183 | |||
184 | /** |
||
185 | * Get a setting by the setting key. |
||
186 | * |
||
187 | * @param string $key Key for the setting |
||
188 | * |
||
189 | * @todo Use the \GV\Settings class later. For now subclasses may still expect and array instead. |
||
190 | * |
||
191 | * @return mixed|null Value of the setting; NULL if not set |
||
192 | */ |
||
193 | 1 | public function get_setting( $key ) { |
|
196 | |||
197 | /** |
||
198 | * Default widget areas. |
||
199 | * |
||
200 | * Usually overridden by the selected template. |
||
201 | * |
||
202 | * @return array The default areas where widgets can be rendered. |
||
203 | */ |
||
204 | 6 | public static function get_default_widget_areas() { |
|
224 | |||
225 | /** |
||
226 | * Register widget to become available in admin. And for lookup. |
||
227 | * |
||
228 | * @param array $widgets Usually just empty. Used to gather them all up. |
||
229 | * |
||
230 | * @return array $widgets |
||
231 | */ |
||
232 | 40 | public function register_widget( $widgets ) { |
|
246 | |||
247 | /** |
||
248 | * Assign template specific widget options |
||
249 | * |
||
250 | * @access protected |
||
251 | * |
||
252 | * @param array $options (default: array()) |
||
253 | * @param string $template (default: '') |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 1 | public function assign_widget_options( $options = array(), $template = '', $widget = '' ) { |
|
265 | |||
266 | /** |
||
267 | * Do shortcode if the Widget's shortcode exists. |
||
268 | * |
||
269 | * @param string $text Widget text to check |
||
270 | * @param null|\WP_Widget Empty if not called by WP_Widget, or a WP_Widget instance |
||
271 | * |
||
272 | * @return string Widget text |
||
273 | */ |
||
274 | 1 | public function maybe_do_shortcode( $text, $widget = null ) { |
|
280 | |||
281 | /** |
||
282 | * Add $this->shortcode_name shortcode to output self::render_frontend() |
||
283 | * |
||
284 | * @return void |
||
285 | */ |
||
286 | 3 | public function add_shortcode() { |
|
287 | 3 | if ( empty( $this->shortcode_name ) ) { |
|
288 | return; |
||
289 | } |
||
290 | |||
291 | 3 | if ( ! gravityview()->plugin->is_compatible() ) { |
|
292 | return; |
||
293 | } |
||
294 | |||
295 | 3 | if ( gravityview()->request->is_admin() ) { |
|
296 | return; |
||
297 | } |
||
298 | |||
299 | // If the widget shouldn't output on single entries, don't show it |
||
300 | 3 | if ( empty( $this->show_on_single ) && gravityview()->request->is_entry() ) { |
|
301 | 1 | gravityview()->log->debug( 'Skipping; set to not run on single entry.' ); |
|
302 | 1 | add_shortcode( $this->shortcode_name, '__return_null' ); |
|
303 | 1 | return; |
|
304 | } |
||
305 | |||
306 | 2 | global $post; |
|
307 | |||
308 | 2 | if ( ! is_object( $post ) || empty( $post->post_content ) || ! Shortcode::parse( $post->post_content ) ) { |
|
309 | 1 | gravityview()->log->debug( 'No shortcode present; not adding render_frontend shortcode.' ); |
|
310 | 1 | add_shortcode( $this->shortcode_name, '__return_null' ); |
|
311 | 1 | return; |
|
312 | } |
||
313 | |||
314 | 2 | add_shortcode( $this->shortcode_name, array( $this, 'render_shortcode') ); |
|
315 | 2 | } |
|
316 | |||
317 | /** |
||
318 | * Frontend logic. |
||
319 | * |
||
320 | * Override in child class. |
||
321 | * |
||
322 | * @param array $widget_args The Widget shortcode args. |
||
323 | * @param string $content The content. |
||
324 | * @param string|\GV\Template_Context $context The context, if available. |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function render_frontend( $widget_args, $content = '', $context = '' ) { |
||
330 | |||
331 | /** |
||
332 | * General validations when rendering the widget |
||
333 | * |
||
334 | * Always call this from your `render_frontend()` override! |
||
335 | * |
||
336 | * @return boolean True: render frontend; False: don't render frontend |
||
337 | */ |
||
338 | 4 | public function pre_render_frontend() { |
|
372 | |||
373 | /** |
||
374 | * Shortcode. |
||
375 | * |
||
376 | * @param array $atts The Widget shortcode args. |
||
377 | * @param string $content The content. |
||
378 | * @param string|\GV\Template_Context $context The context, if available. |
||
379 | * |
||
380 | * @return string Whatever the widget echoed. |
||
381 | */ |
||
382 | 1 | public function render_shortcode( $atts, $content = '', $context = '' ) { |
|
387 | |||
388 | /** |
||
389 | * Create the needed widget from a configuration array. |
||
390 | * |
||
391 | * @param array $configuration The configuration array. |
||
392 | * @see \GV\Widget::as_configuration() |
||
393 | * @internal |
||
394 | * @since 2.0 |
||
395 | * |
||
396 | * @return \GV\Widget|null The widget implementation from configuration or none. |
||
397 | */ |
||
398 | 39 | public static function from_configuration( $configuration ) { |
|
418 | |||
419 | /** |
||
420 | * Return an array of the old format. |
||
421 | * |
||
422 | * 'id' => string |
||
423 | * + whatever else specific fields may have |
||
424 | * |
||
425 | * @internal |
||
426 | * @since 2.0 |
||
427 | * |
||
428 | * @return array |
||
429 | */ |
||
430 | 6 | public function as_configuration() { |
|
435 | |||
436 | /** |
||
437 | * Return all registered widgets. |
||
438 | * |
||
439 | * @api |
||
440 | * @since 2.0 |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | 40 | public static function registered() { |
|
458 | |||
459 | /** |
||
460 | * Whether this Widget's been registered already or not. |
||
461 | * |
||
462 | * @api |
||
463 | * @since 2.0 |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | 40 | public function is_registered() { |
|
474 | } |
||
475 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.