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 WP_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 WP_Network, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class WP_Network { |
||
25 | |||
26 | /** |
||
27 | * Network ID. |
||
28 | * |
||
29 | * @since 4.4.0 |
||
30 | * @since 4.6.0 Converted from public to private to explicitly enable more intuitive |
||
31 | * access via magic methods. As part of the access change, the type was |
||
32 | * also changed from `string` to `int`. |
||
33 | * @access private |
||
34 | * @var int |
||
35 | */ |
||
36 | private $id; |
||
37 | |||
38 | /** |
||
39 | * Domain of the network. |
||
40 | * |
||
41 | * @since 4.4.0 |
||
42 | * @access public |
||
43 | * @var string |
||
44 | */ |
||
45 | public $domain = ''; |
||
46 | |||
47 | /** |
||
48 | * Path of the network. |
||
49 | * |
||
50 | * @since 4.4.0 |
||
51 | * @access public |
||
52 | * @var string |
||
53 | */ |
||
54 | public $path = ''; |
||
55 | |||
56 | /** |
||
57 | * The ID of the network's main site. |
||
58 | * |
||
59 | * Named "blog" vs. "site" for legacy reasons. A main site is mapped to |
||
60 | * the network when the network is created. |
||
61 | * |
||
62 | * A numeric string, for compatibility reasons. |
||
63 | * |
||
64 | * @since 4.4.0 |
||
65 | * @access private |
||
66 | * @var string |
||
67 | */ |
||
68 | private $blog_id = '0'; |
||
69 | |||
70 | /** |
||
71 | * Domain used to set cookies for this network. |
||
72 | * |
||
73 | * @since 4.4.0 |
||
74 | * @access public |
||
75 | * @var string |
||
76 | */ |
||
77 | public $cookie_domain = ''; |
||
78 | |||
79 | /** |
||
80 | * Name of this network. |
||
81 | * |
||
82 | * Named "site" vs. "network" for legacy reasons. |
||
83 | * |
||
84 | * @since 4.4.0 |
||
85 | * @access public |
||
86 | * @var string |
||
87 | */ |
||
88 | public $site_name = ''; |
||
89 | |||
90 | /** |
||
91 | * Retrieve a network from the database by its ID. |
||
92 | * |
||
93 | * @since 4.4.0 |
||
94 | * @access public |
||
95 | * |
||
96 | * @global wpdb $wpdb WordPress database abstraction object. |
||
97 | * |
||
98 | * @param int $network_id The ID of the network to retrieve. |
||
99 | * @return WP_Network|bool The network's object if found. False if not. |
||
100 | */ |
||
101 | View Code Duplication | public static function get_instance( $network_id ) { |
|
123 | |||
124 | /** |
||
125 | * Create a new WP_Network object. |
||
126 | * |
||
127 | * Will populate object properties from the object provided and assign other |
||
128 | * default properties based on that information. |
||
129 | * |
||
130 | * @since 4.4.0 |
||
131 | * @access public |
||
132 | * |
||
133 | * @param WP_Network|object $network A network object. |
||
134 | */ |
||
135 | public function __construct( $network ) { |
||
136 | foreach( get_object_vars( $network ) as $key => $value ) { |
||
137 | $this->$key = $value; |
||
138 | } |
||
139 | |||
140 | $this->_set_site_name(); |
||
141 | $this->_set_cookie_domain(); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Getter. |
||
146 | * |
||
147 | * Allows current multisite naming conventions when getting properties. |
||
148 | * |
||
149 | * @since 4.6.0 |
||
150 | * @access public |
||
151 | * |
||
152 | * @param string $key Property to get. |
||
153 | * @return mixed Value of the property. Null if not available. |
||
154 | */ |
||
155 | public function __get( $key ) { |
||
156 | switch ( $key ) { |
||
157 | case 'id'; |
||
158 | return (int) $this->id; |
||
159 | case 'blog_id': |
||
160 | return $this->blog_id; |
||
161 | case 'site_id': |
||
162 | return (int) $this->blog_id; |
||
163 | } |
||
164 | |||
165 | return null; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Isset-er. |
||
170 | * |
||
171 | * Allows current multisite naming conventions when checking for properties. |
||
172 | * |
||
173 | * @since 4.6.0 |
||
174 | * @access public |
||
175 | * |
||
176 | * @param string $key Property to check if set. |
||
177 | * @return bool Whether the property is set. |
||
178 | */ |
||
179 | public function __isset( $key ) { |
||
180 | switch ( $key ) { |
||
181 | case 'id': |
||
182 | case 'blog_id': |
||
183 | case 'site_id': |
||
184 | return true; |
||
185 | } |
||
186 | |||
187 | return false; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Setter. |
||
192 | * |
||
193 | * Allows current multisite naming conventions while setting properties. |
||
194 | * |
||
195 | * @since 4.6.0 |
||
196 | * @access public |
||
197 | * |
||
198 | * @param string $key Property to set. |
||
199 | * @param mixed $value Value to assign to the property. |
||
200 | */ |
||
201 | View Code Duplication | public function __set( $key, $value ) { |
|
202 | switch ( $key ) { |
||
203 | case 'id': |
||
204 | $this->id = (int) $value; |
||
205 | break; |
||
206 | case 'blog_id': |
||
207 | case 'site_id': |
||
208 | $this->blog_id = (string) $value; |
||
209 | break; |
||
210 | default: |
||
211 | $this->$key = $value; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Set the site name assigned to the network if one has not been populated. |
||
217 | * |
||
218 | * @since 4.4.0 |
||
219 | * @access private |
||
220 | */ |
||
221 | private function _set_site_name() { |
||
222 | if ( ! empty( $this->site_name ) ) { |
||
223 | return; |
||
224 | } |
||
225 | |||
226 | $default = ucfirst( $this->domain ); |
||
227 | $this->site_name = get_network_option( $this->id, 'site_name', $default ); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Set the cookie domain based on the network domain if one has |
||
232 | * not been populated. |
||
233 | * |
||
234 | * @todo What if the domain of the network doesn't match the current site? |
||
235 | * |
||
236 | * @since 4.4.0 |
||
237 | * @access private |
||
238 | */ |
||
239 | private function _set_cookie_domain() { |
||
249 | |||
250 | /** |
||
251 | * Retrieve the closest matching network for a domain and path. |
||
252 | * |
||
253 | * This will not necessarily return an exact match for a domain and path. Instead, it |
||
254 | * breaks the domain and path into pieces that are then used to match the closest |
||
255 | * possibility from a query. |
||
256 | * |
||
257 | * The intent of this method is to match a network during bootstrap for a |
||
258 | * requested site address. |
||
259 | * |
||
260 | * @since 4.4.0 |
||
261 | * @access public |
||
262 | * @static |
||
263 | * |
||
264 | * @param string $domain Domain to check. |
||
265 | * @param string $path Path to check. |
||
266 | * @param int|null $segments Path segments to use. Defaults to null, or the full path. |
||
267 | * @return WP_Network|bool Network object if successful. False when no network is found. |
||
268 | */ |
||
269 | public static function get_by_path( $domain = '', $path = '', $segments = null ) { |
||
410 | } |
||
411 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.