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 |
||
23 | class Jetpack_Network { |
||
24 | |||
25 | /** |
||
26 | * Holds a static copy of Jetpack_Network for the singleton |
||
27 | * |
||
28 | * @since 2.9 |
||
29 | * @var Jetpack_Network |
||
30 | */ |
||
31 | private static $instance = null; |
||
32 | |||
33 | /** |
||
34 | * An instance of the connection manager object. |
||
35 | * |
||
36 | * @since 7.7 |
||
37 | * @var Automattic\Jetpack\Connection\Manager |
||
38 | */ |
||
39 | private $connection; |
||
40 | |||
41 | /** |
||
42 | * Name of the network wide settings |
||
43 | * |
||
44 | * @since 2.9 |
||
45 | * @var string |
||
46 | */ |
||
47 | private $settings_name = 'jetpack-network-settings'; |
||
48 | |||
49 | /** |
||
50 | * Defaults for settings found on the Jetpack > Settings page |
||
51 | * |
||
52 | * @since 2.9 |
||
53 | * @var array |
||
54 | */ |
||
55 | private $setting_defaults = array( |
||
56 | 'auto-connect' => 0, |
||
57 | 'sub-site-connection-override' => 1, |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @since 2.9 |
||
64 | */ |
||
65 | private function __construct() { |
||
100 | |||
101 | /** |
||
102 | * Sets a connection object. |
||
103 | * |
||
104 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
105 | */ |
||
106 | public function set_connection( Manager $connection ) { |
||
109 | |||
110 | /** |
||
111 | * Sets which modules get activated by default on subsite connection. |
||
112 | * Modules can be set in Network Admin > Jetpack > Settings |
||
113 | * |
||
114 | * @since 2.9 |
||
115 | * @deprecated since 7.7.0 |
||
116 | * |
||
117 | * @param array $modules List of modules. |
||
118 | */ |
||
119 | public function set_auto_activated_modules( $modules ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
122 | |||
123 | /** |
||
124 | * Registers new sites upon creation |
||
125 | * |
||
126 | * @since 2.9 |
||
127 | * @since 7.4.0 Uses a WP_Site object. |
||
128 | * @uses wp_initialize_site |
||
129 | * |
||
130 | * @param WP_Site $site the WordPress site object. |
||
131 | **/ |
||
132 | public function do_automatically_add_new_site( $site ) { |
||
137 | |||
138 | /** |
||
139 | * Adds .network-admin class to the body tag |
||
140 | * Helps distinguish network admin JP styles from regular site JP styles |
||
141 | * |
||
142 | * @since 2.9 |
||
143 | * |
||
144 | * @param String $classes current assigned body classes. |
||
145 | * @return String amended class string. |
||
146 | */ |
||
147 | public function body_class( $classes ) { |
||
150 | |||
151 | /** |
||
152 | * Provides access to an instance of Jetpack_Network |
||
153 | * |
||
154 | * This is how the Jetpack_Network object should *always* be accessed |
||
155 | * |
||
156 | * @since 2.9 |
||
157 | * @return Jetpack_Network |
||
158 | */ |
||
159 | public static function init() { |
||
166 | |||
167 | /** |
||
168 | * Registers the Multisite admin bar menu item shortcut. |
||
169 | * This shortcut helps users quickly and easily navigate to the Jetpack Network Admin |
||
170 | * menu from anywhere in their network. |
||
171 | * |
||
172 | * @since 2.9 |
||
173 | */ |
||
174 | public function register_menubar() { |
||
177 | |||
178 | /** |
||
179 | * Runs when Jetpack is deactivated from the network admin plugins menu. |
||
180 | * Each individual site will need to have Jetpack::disconnect called on it. |
||
181 | * Site that had Jetpack individually enabled will not be disconnected as |
||
182 | * on Multisite individually activated plugins are still activated when |
||
183 | * a plugin is deactivated network wide. |
||
184 | * |
||
185 | * @since 2.9 |
||
186 | **/ |
||
187 | public function deactivate() { |
||
211 | |||
212 | /** |
||
213 | * Adds a link to the Jetpack Network Admin page in the network admin menu bar. |
||
214 | * |
||
215 | * @since 2.9 |
||
216 | **/ |
||
217 | public function add_to_menubar() { |
||
233 | |||
234 | /** |
||
235 | * Returns various URL strings. Factory like |
||
236 | * |
||
237 | * $args can be a string or an array. |
||
238 | * If $args is an array there must be an element called name for the switch statement |
||
239 | * |
||
240 | * Currently supports: |
||
241 | * - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID ) |
||
242 | * - network_admin_page: Provides link to /wp-admin/network/JETPACK |
||
243 | * - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID ) |
||
244 | * |
||
245 | * @since 2.9 |
||
246 | * |
||
247 | * @param Mixed $args URL parameters. |
||
248 | * |
||
249 | * @return String |
||
250 | **/ |
||
251 | public function get_url( $args ) { |
||
290 | |||
291 | /** |
||
292 | * Adds the Jetpack menu item to the Network Admin area |
||
293 | * |
||
294 | * @since 2.9 |
||
295 | */ |
||
296 | public function add_network_admin_menu() { |
||
316 | |||
317 | /** |
||
318 | * Adds JP menu icon |
||
319 | * |
||
320 | * @since 2.9 |
||
321 | **/ |
||
322 | public function admin_menu_css() { |
||
325 | |||
326 | /** |
||
327 | * Provides functionality for the Jetpack > Sites page. |
||
328 | * Does not do the display! |
||
329 | * |
||
330 | * @since 2.9 |
||
331 | */ |
||
332 | public function jetpack_sites_list() { |
||
387 | |||
388 | /** |
||
389 | * Set the disconnect capability for multisite. |
||
390 | * |
||
391 | * @param array $caps The capabilities array. |
||
392 | */ |
||
393 | public function set_multisite_disconnect_cap( $caps ) { |
||
407 | |||
408 | /** |
||
409 | * Shows the Jetpack plugin notices. |
||
410 | */ |
||
411 | public function show_jetpack_notice() { |
||
425 | |||
426 | /** |
||
427 | * Disconnect functionality for an individual site |
||
428 | * |
||
429 | * @since 2.9 |
||
430 | * @see Jetpack_Network::jetpack_sites_list() |
||
431 | * |
||
432 | * @param int $site_id the site identifier. |
||
433 | */ |
||
434 | public function do_subsitedisconnect( $site_id = null ) { |
||
443 | |||
444 | /** |
||
445 | * Registers a subsite with the Jetpack servers |
||
446 | * |
||
447 | * @since 2.9 |
||
448 | * @todo Break apart into easier to manage chunks that can be unit tested |
||
449 | * @see Jetpack_Network::jetpack_sites_list(); |
||
450 | * |
||
451 | * @param int $site_id the site identifier. |
||
452 | */ |
||
453 | public function do_subsiteregister( $site_id = null ) { |
||
488 | |||
489 | /** |
||
490 | * Receives the registration response token. |
||
491 | * |
||
492 | * @param Object $token the received token. |
||
493 | */ |
||
494 | public function filter_register_user_token( $token ) { |
||
502 | |||
503 | /** |
||
504 | * Filters the registration request body to include additional properties. |
||
505 | * |
||
506 | * @param array $properties standard register request body properties. |
||
507 | * @return array amended properties. |
||
508 | */ |
||
509 | public function filter_register_request_body( $properties ) { |
||
542 | |||
543 | /** |
||
544 | * A hook handler for adding admin pages and subpages. |
||
545 | */ |
||
546 | public function wrap_network_admin_page() { |
||
549 | |||
550 | /** |
||
551 | * Handles the displaying of all sites on the network that are |
||
552 | * dis/connected to Jetpack |
||
553 | * |
||
554 | * @since 2.9 |
||
555 | * @see Jetpack_Network::jetpack_sites_list() |
||
556 | */ |
||
557 | public function network_admin_page() { |
||
601 | |||
602 | /** |
||
603 | * Stylized JP header formatting |
||
604 | * |
||
605 | * @since 2.9 |
||
606 | */ |
||
607 | public function network_admin_page_header() { |
||
615 | |||
616 | /** |
||
617 | * Fires when the Jetpack > Settings page is saved. |
||
618 | * |
||
619 | * @since 2.9 |
||
620 | */ |
||
621 | public function save_network_settings_page() { |
||
684 | |||
685 | /** |
||
686 | * A hook handler for adding admin pages and subpages. |
||
687 | */ |
||
688 | public function wrap_render_network_admin_settings_page() { |
||
691 | |||
692 | /** |
||
693 | * A hook rendering the admin settings page. |
||
694 | */ |
||
695 | public function render_network_admin_settings_page() { |
||
721 | |||
722 | /** |
||
723 | * Updates a site wide option |
||
724 | * |
||
725 | * @since 2.9 |
||
726 | * |
||
727 | * @param string $key option name. |
||
728 | * @param mixed $value option value. |
||
729 | * |
||
730 | * @return boolean |
||
731 | **/ |
||
732 | public function update_option( $key, $value ) { |
||
738 | |||
739 | /** |
||
740 | * Retrieves a site wide option |
||
741 | * |
||
742 | * @since 2.9 |
||
743 | * |
||
744 | * @param string $name - Name of the option in the database. |
||
745 | **/ |
||
746 | public function get_option( $name ) { |
||
756 | } |
||
757 |
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: