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 lasso 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 lasso, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class lasso { |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | * |
||
23 | * @since 0.0.1 |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $plugin_slug = 'lasso'; |
||
28 | |||
29 | /** |
||
30 | * Instance of this class. |
||
31 | * |
||
32 | * @since 0.0.1 |
||
33 | * |
||
34 | * @var object |
||
35 | */ |
||
36 | protected static $instance = null; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * |
||
41 | * @since 0.0.1 |
||
42 | */ |
||
43 | private function __construct() { |
||
71 | |||
72 | /** |
||
73 | * Return the plugin slug. |
||
74 | * |
||
75 | * @since 0.0.1 |
||
76 | * |
||
77 | * @return Plugin slug variable. |
||
78 | */ |
||
79 | public function get_plugin_slug() { |
||
82 | |||
83 | /** |
||
84 | * Return an instance of this class. |
||
85 | * |
||
86 | * @since 0.0.1 |
||
87 | * |
||
88 | * @return object A single instance of this class. |
||
89 | */ |
||
90 | public static function get_instance() { |
||
99 | |||
100 | /** |
||
101 | * Fired when the plugin is activated. |
||
102 | * |
||
103 | * @since 0.0.1 |
||
104 | * |
||
105 | * @param boolean $network_wide True if WPMU superadmin uses |
||
106 | * "Network Activate" action, false if |
||
107 | * WPMU is disabled or plugin is |
||
108 | * activated on an individual blog. |
||
109 | */ |
||
110 | View Code Duplication | public static function activate( $network_wide ) { |
|
136 | |||
137 | /** |
||
138 | * Fired when the plugin is deactivated. |
||
139 | * |
||
140 | * @since 0.0.1 |
||
141 | * |
||
142 | * @param boolean $network_wide True if WPMU superadmin uses |
||
143 | * "Network Deactivate" action, false if |
||
144 | * WPMU is disabled or plugin is |
||
145 | * deactivated on an individual blog. |
||
146 | */ |
||
147 | View Code Duplication | public static function deactivate( $network_wide ) { |
|
174 | |||
175 | /** |
||
176 | * Fired when a new site is activated with a WPMU environment. |
||
177 | * |
||
178 | * @since 0.0.1 |
||
179 | * |
||
180 | * @param int $blog_id ID of the new blog. |
||
181 | */ |
||
182 | public function activate_new_site( $blog_id ) { |
||
193 | |||
194 | /** |
||
195 | * Get all blog ids of blogs in the current network that are: |
||
196 | * - not archived |
||
197 | * - not spam |
||
198 | * - not deleted |
||
199 | * |
||
200 | * @since 0.0.1 |
||
201 | * |
||
202 | * @return array|false The blog ids, false if no matches. |
||
203 | */ |
||
204 | private static function get_blog_ids() { |
||
216 | |||
217 | /** |
||
218 | * Fired for each blog when the plugin is activated. |
||
219 | * |
||
220 | * @since 0.0.1 |
||
221 | */ |
||
222 | private static function single_activate() { |
||
239 | |||
240 | /** |
||
241 | * Fired for each blog when the plugin is deactivated. |
||
242 | * |
||
243 | * @since 0.0.1 |
||
244 | */ |
||
245 | private static function single_deactivate() { |
||
248 | |||
249 | /** |
||
250 | * Load the plugin text domain for translation. |
||
251 | * |
||
252 | * @since 1.0.0 |
||
253 | */ |
||
254 | public function load_plugin_textdomain() { |
||
261 | |||
262 | // new ajax function to lock post for editing |
||
263 | public function editus_lock_post() |
||
277 | |||
278 | public static function enable_metasave($type) |
||
292 | |||
293 | public function editus_do_shortcode() |
||
302 | |||
303 | public function get_aesop_component() |
||
369 | } |
||
370 |
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.