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:
1 | <?php |
||
21 | class WP_Network { |
||
22 | |||
23 | /** |
||
24 | * Network ID. |
||
25 | * |
||
26 | * A numeric string, for compatibility reasons. |
||
27 | * |
||
28 | * @since 4.4.0 |
||
29 | * @access public |
||
30 | * @var string |
||
31 | */ |
||
32 | public $id; |
||
33 | |||
34 | /** |
||
35 | * Domain of the network. |
||
36 | * |
||
37 | * @since 4.4.0 |
||
38 | * @access public |
||
39 | * @var string |
||
40 | */ |
||
41 | public $domain = ''; |
||
42 | |||
43 | /** |
||
44 | * Path of the network. |
||
45 | * |
||
46 | * @since 4.4.0 |
||
47 | * @access public |
||
48 | * @var string |
||
49 | */ |
||
50 | public $path = ''; |
||
51 | |||
52 | /** |
||
53 | * The ID of the network's main site. |
||
54 | * |
||
55 | * Named "blog" vs. "site" for legacy reasons. A main site is mapped to |
||
56 | * the network when the network is created. |
||
57 | * |
||
58 | * A numeric string, for compatibility reasons. |
||
59 | * |
||
60 | * @since 4.4.0 |
||
61 | * @access public |
||
62 | * @var string |
||
63 | */ |
||
64 | public $blog_id = 0; |
||
65 | |||
66 | /** |
||
67 | * Domain used to set cookies for this network. |
||
68 | * |
||
69 | * @since 4.4.0 |
||
70 | * @access public |
||
71 | * @var string |
||
72 | */ |
||
73 | public $cookie_domain = ''; |
||
74 | |||
75 | /** |
||
76 | * Name of this network. |
||
77 | * |
||
78 | * Named "site" vs. "network" for legacy reasons. |
||
79 | * |
||
80 | * @since 4.4.0 |
||
81 | * @access public |
||
82 | * @var string |
||
83 | */ |
||
84 | public $site_name = ''; |
||
85 | |||
86 | /** |
||
87 | * Retrieve a network from the database by its ID. |
||
88 | * |
||
89 | * @since 4.4.0 |
||
90 | * @access public |
||
91 | * |
||
92 | * @global wpdb $wpdb WordPress database abstraction object. |
||
93 | * |
||
94 | * @param int $network_id The ID of the network to retrieve. |
||
95 | * @return WP_Network|bool The network's object if found. False if not. |
||
96 | */ |
||
97 | View Code Duplication | public static function get_instance( $network_id ) { |
|
119 | |||
120 | /** |
||
121 | * Create a new WP_Network object. |
||
122 | * |
||
123 | * Will populate object properties from the object provided and assign other |
||
124 | * default properties based on that information. |
||
125 | * |
||
126 | * @since 4.4.0 |
||
127 | * @access public |
||
128 | * |
||
129 | * @param WP_Network|object $network A network object. |
||
130 | */ |
||
131 | public function __construct( $network ) { |
||
139 | |||
140 | /** |
||
141 | * Set the site name assigned to the network if one has not been populated. |
||
142 | * |
||
143 | * @since 4.4.0 |
||
144 | * @access private |
||
145 | */ |
||
146 | private function _set_site_name() { |
||
154 | |||
155 | /** |
||
156 | * Set the cookie domain based on the network domain if one has |
||
157 | * not been populated. |
||
158 | * |
||
159 | * @todo What if the domain of the network doesn't match the current site? |
||
160 | * |
||
161 | * @since 4.4.0 |
||
162 | * @access private |
||
163 | */ |
||
164 | private function _set_cookie_domain() { |
||
174 | |||
175 | /** |
||
176 | * Retrieve the closest matching network for a domain and path. |
||
177 | * |
||
178 | * This will not necessarily return an exact match for a domain and path. Instead, it |
||
179 | * breaks the domain and path into pieces that are then used to match the closest |
||
180 | * possibility from a query. |
||
181 | * |
||
182 | * The intent of this method is to match a network during bootstrap for a |
||
183 | * requested site address. |
||
184 | * |
||
185 | * @since 4.4.0 |
||
186 | * @access public |
||
187 | * @static |
||
188 | * |
||
189 | * @param string $domain Domain to check. |
||
190 | * @param string $path Path to check. |
||
191 | * @param int|null $segments Path segments to use. Defaults to null, or the full path. |
||
192 | * @return WP_Network|bool Network object if successful. False when no network is found. |
||
193 | */ |
||
194 | public static function get_by_path( $domain = '', $path = '', $segments = null ) { |
||
335 | } |
||
336 |
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.