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 WETU_Importer_Admin 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 WETU_Importer_Admin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class WETU_Importer_Admin extends WETU_Importer { |
||
11 | |||
12 | /** |
||
13 | * The previously attached images |
||
14 | * |
||
15 | * @var array() |
||
16 | */ |
||
17 | public $found_attachments = array(); |
||
18 | |||
19 | /** |
||
20 | * The gallery ids for the found attachements |
||
21 | * |
||
22 | * @var array() |
||
23 | */ |
||
24 | public $gallery_meta = array(); |
||
25 | |||
26 | /** |
||
27 | * the featured image id |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | public $featured_image = false; |
||
32 | |||
33 | /** |
||
34 | * the banner image |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | public $banner_image = false; |
||
39 | |||
40 | /** |
||
41 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
42 | * |
||
43 | * @since 1.0.0 |
||
44 | * |
||
45 | * @access private |
||
46 | */ |
||
47 | public function __construct() { |
||
51 | |||
52 | /** |
||
53 | * Registers the admin page which will house the importer form. |
||
54 | */ |
||
55 | public function register_importer_page() { |
||
58 | |||
59 | /** |
||
60 | * Enqueue the JS needed to contact wetu and return your result. |
||
61 | */ |
||
62 | public function admin_scripts() { |
||
77 | |||
78 | /** |
||
79 | * Display the importer administration screen |
||
80 | */ |
||
81 | public function display_page() { |
||
109 | |||
110 | /** |
||
111 | * The header of the item list |
||
112 | */ |
||
113 | public function table_header() { |
||
128 | |||
129 | /** |
||
130 | * The footer of the item list |
||
131 | */ |
||
132 | public function table_footer() { |
||
147 | |||
148 | /** |
||
149 | * set_taxonomy with some terms |
||
150 | */ |
||
151 | public function set_taxonomy($taxonomy,$terms,$id) { |
||
184 | |||
185 | /** |
||
186 | * set_taxonomy with some terms |
||
187 | */ |
||
188 | public function team_member_checkboxes($selected=array()) { |
||
210 | |||
211 | /** |
||
212 | * set_taxonomy with some terms |
||
213 | */ |
||
214 | public function taxonomy_checkboxes($taxonomy=false,$selected=array()) { |
||
231 | |||
232 | /** |
||
233 | * Saves the room data |
||
234 | */ |
||
235 | public function save_custom_field($value=false,$meta_key,$id,$decrease=false,$unique=true) { |
||
250 | |||
251 | /** |
||
252 | * grabs any attachments for the current item |
||
253 | */ |
||
254 | public function find_attachments($id=false) { |
||
277 | |||
278 | /** |
||
279 | * Checks to see if an item is checked. |
||
280 | * |
||
281 | * @param $haystack array|string |
||
282 | * @param $needle string |
||
283 | * @param $echo bool |
||
284 | */ |
||
285 | View Code Duplication | public function checked($haystack=false,$needle='',$echo=true) { |
|
295 | |||
296 | /** |
||
297 | * Checks to see if an item is checked. |
||
298 | * |
||
299 | * @param $haystack array|string |
||
300 | * @param $needle string |
||
301 | * @param $echo bool |
||
302 | */ |
||
303 | View Code Duplication | public function selected($haystack=false,$needle='',$echo=true) { |
|
313 | |||
314 | /** |
||
315 | * Checks to see if an item is selected. If $echo is false, it will return the $type if conditions are true. |
||
316 | * |
||
317 | * @param $haystack array|string |
||
318 | * @param $needle string |
||
319 | * @param $type string |
||
320 | * @param $wrap bool |
||
321 | * @return $html string |
||
322 | */ |
||
323 | public function itemd($haystack=false,$needle='',$type='',$wrap=true) { |
||
340 | |||
341 | /** |
||
342 | * Displays the importers navigation |
||
343 | * |
||
344 | * @param $tab string |
||
345 | */ |
||
346 | public function navigation($tab='') { |
||
360 | } |
||
361 | $wetu_importer_admin = new WETU_Importer_Admin(); |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.