Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Network 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 Jetpack_Network, and based on these observations, apply Extract Interface, too.
1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFilename |
||
21 | class Jetpack_Network { |
||
22 | |||
23 | /** |
||
24 | * Holds a static copy of Jetpack_Network for the singleton |
||
25 | * |
||
26 | * @since 2.9 |
||
27 | * @var Jetpack_Network |
||
28 | */ |
||
29 | private static $instance = null; |
||
30 | |||
31 | /** |
||
32 | * An instance of the connection manager object. |
||
33 | * |
||
34 | * @since 7.7 |
||
35 | * @var Automattic\Jetpack\Connection\Manager |
||
36 | */ |
||
37 | private $connection; |
||
38 | |||
39 | /** |
||
40 | * Name of the network wide settings |
||
41 | * |
||
42 | * @since 2.9 |
||
43 | * @var string |
||
44 | */ |
||
45 | private $settings_name = 'jetpack-network-settings'; |
||
46 | |||
47 | /** |
||
48 | * Defaults for settings found on the Jetpack > Settings page |
||
49 | * |
||
50 | * @since 2.9 |
||
51 | * @var array |
||
52 | */ |
||
53 | private $setting_defaults = array( |
||
54 | 'auto-connect' => 0, |
||
55 | 'sub-site-connection-override' => 1, |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * @since 2.9 |
||
62 | */ |
||
63 | private function __construct() { |
||
97 | |||
98 | /** |
||
99 | * Sets a connection object. |
||
100 | * |
||
101 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
102 | */ |
||
103 | public function set_connection( Manager $connection ) { |
||
106 | |||
107 | /** |
||
108 | * Sets which modules get activated by default on subsite connection. |
||
109 | * Modules can be set in Network Admin > Jetpack > Settings |
||
110 | * |
||
111 | * @since 2.9 |
||
112 | * @deprecated since 7.7.0 |
||
113 | * |
||
114 | * @param array $modules List of modules. |
||
115 | */ |
||
116 | public function set_auto_activated_modules( $modules ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
119 | |||
120 | /** |
||
121 | * Registers new sites upon creation |
||
122 | * |
||
123 | * @since 2.9 |
||
124 | * @since 7.4.0 Uses a WP_Site object. |
||
125 | * @uses wp_initialize_site |
||
126 | * |
||
127 | * @param WP_Site $site the WordPress site object. |
||
128 | **/ |
||
129 | public function do_automatically_add_new_site( $site ) { |
||
134 | |||
135 | /** |
||
136 | * Adds .network-admin class to the body tag |
||
137 | * Helps distinguish network admin JP styles from regular site JP styles |
||
138 | * |
||
139 | * @since 2.9 |
||
140 | * |
||
141 | * @param String $classes current assigned body classes. |
||
142 | * @return String amended class string. |
||
143 | */ |
||
144 | public function body_class( $classes ) { |
||
147 | |||
148 | /** |
||
149 | * Provides access to an instance of Jetpack_Network |
||
150 | * |
||
151 | * This is how the Jetpack_Network object should *always* be accessed |
||
152 | * |
||
153 | * @since 2.9 |
||
154 | * @return Jetpack_Network |
||
155 | */ |
||
156 | public static function init() { |
||
163 | |||
164 | /** |
||
165 | * Registers the Multisite admin bar menu item shortcut. |
||
166 | * This shortcut helps users quickly and easily navigate to the Jetpack Network Admin |
||
167 | * menu from anywhere in their network. |
||
168 | * |
||
169 | * @since 2.9 |
||
170 | */ |
||
171 | public function register_menubar() { |
||
174 | |||
175 | /** |
||
176 | * Runs when Jetpack is deactivated from the network admin plugins menu. |
||
177 | * Each individual site will need to have Jetpack::disconnect called on it. |
||
178 | * Site that had Jetpack individually enabled will not be disconnected as |
||
179 | * on Multisite individually activated plugins are still activated when |
||
180 | * a plugin is deactivated network wide. |
||
181 | * |
||
182 | * @since 2.9 |
||
183 | **/ |
||
184 | public function deactivate() { |
||
208 | |||
209 | /** |
||
210 | * Adds a link to the Jetpack Network Admin page in the network admin menu bar. |
||
211 | * |
||
212 | * @since 2.9 |
||
213 | **/ |
||
214 | public function add_to_menubar() { |
||
230 | |||
231 | /** |
||
232 | * Returns various URL strings. Factory like |
||
233 | * |
||
234 | * $args can be a string or an array. |
||
235 | * If $args is an array there must be an element called name for the switch statement |
||
236 | * |
||
237 | * Currently supports: |
||
238 | * - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID ) |
||
239 | * - network_admin_page: Provides link to /wp-admin/network/JETPACK |
||
240 | * - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID ) |
||
241 | * |
||
242 | * @since 2.9 |
||
243 | * |
||
244 | * @param Mixed $args URL parameters. |
||
245 | * |
||
246 | * @return String |
||
247 | **/ |
||
248 | public function get_url( $args ) { |
||
285 | |||
286 | /** |
||
287 | * Adds the Jetpack menu item to the Network Admin area |
||
288 | * |
||
289 | * @since 2.9 |
||
290 | */ |
||
291 | public function add_network_admin_menu() { |
||
311 | |||
312 | /** |
||
313 | * Adds JP menu icon |
||
314 | * |
||
315 | * @since 2.9 |
||
316 | **/ |
||
317 | public function admin_menu_css() { |
||
320 | |||
321 | /** |
||
322 | * Provides functionality for the Jetpack > Sites page. |
||
323 | * Does not do the display! |
||
324 | * |
||
325 | * @since 2.9 |
||
326 | */ |
||
327 | public function jetpack_sites_list() { |
||
382 | |||
383 | /** |
||
384 | * Shows the Jetpack plugin notices. |
||
385 | */ |
||
386 | public function show_jetpack_notice() { |
||
400 | |||
401 | /** |
||
402 | * Disconnect functionality for an individual site |
||
403 | * |
||
404 | * @since 2.9 |
||
405 | * @see Jetpack_Network::jetpack_sites_list() |
||
406 | * |
||
407 | * @param int $site_id the site identifier. |
||
408 | */ |
||
409 | public function do_subsitedisconnect( $site_id = null ) { |
||
418 | |||
419 | /** |
||
420 | * Registers a subsite with the Jetpack servers |
||
421 | * |
||
422 | * @since 2.9 |
||
423 | * @todo Break apart into easier to manage chunks that can be unit tested |
||
424 | * @see Jetpack_Network::jetpack_sites_list(); |
||
425 | * |
||
426 | * @param int $site_id the site identifier. |
||
427 | */ |
||
428 | public function do_subsiteregister( $site_id = null ) { |
||
463 | |||
464 | /** |
||
465 | * Receives the registration response token. |
||
466 | * |
||
467 | * @param Object $token the received token. |
||
468 | */ |
||
469 | public function filter_register_user_token( $token ) { |
||
477 | |||
478 | /** |
||
479 | * Filters the registration request body to include additional properties. |
||
480 | * |
||
481 | * @param Array $properties standard register request body properties. |
||
482 | * @return Array amended properties. |
||
483 | */ |
||
484 | public function filter_register_request_body( $properties ) { |
||
517 | |||
518 | /** |
||
519 | * A hook handler for adding admin pages and subpages. |
||
520 | */ |
||
521 | public function wrap_network_admin_page() { |
||
524 | |||
525 | /** |
||
526 | * Handles the displaying of all sites on the network that are |
||
527 | * dis/connected to Jetpack |
||
528 | * |
||
529 | * @since 2.9 |
||
530 | * @see Jetpack_Network::jetpack_sites_list() |
||
531 | */ |
||
532 | public function network_admin_page() { |
||
576 | |||
577 | /** |
||
578 | * Stylized JP header formatting |
||
579 | * |
||
580 | * @since 2.9 |
||
581 | */ |
||
582 | public function network_admin_page_header() { |
||
590 | |||
591 | /** |
||
592 | * Fires when the Jetpack > Settings page is saved. |
||
593 | * |
||
594 | * @since 2.9 |
||
595 | */ |
||
596 | public function save_network_settings_page() { |
||
677 | |||
678 | /** |
||
679 | * A hook handler for adding admin pages and subpages. |
||
680 | */ |
||
681 | public function wrap_render_network_admin_settings_page() { |
||
684 | |||
685 | /** |
||
686 | * A hook rendering the admin settings page. |
||
687 | */ |
||
688 | public function render_network_admin_settings_page() { |
||
714 | |||
715 | /** |
||
716 | * Updates a site wide option |
||
717 | * |
||
718 | * @since 2.9 |
||
719 | * |
||
720 | * @param string $key option name. |
||
721 | * @param mixed $value option value. |
||
722 | * |
||
723 | * @return boolean |
||
724 | **/ |
||
725 | public function update_option( $key, $value ) { |
||
731 | |||
732 | /** |
||
733 | * Retrieves a site wide option |
||
734 | * |
||
735 | * @since 2.9 |
||
736 | * |
||
737 | * @param string $name - Name of the option in the database. |
||
738 | **/ |
||
739 | public function get_option( $name ) { |
||
749 | } |
||
750 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: