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 post ids to clean up (make sure the connected items are only singular) |
||
| 28 | * |
||
| 29 | * @var array() |
||
| 30 | */ |
||
| 31 | public $cleanup_posts = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * the featured image id |
||
| 35 | * |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | public $featured_image = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * the banner image |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | public $banner_image = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
| 49 | * |
||
| 50 | * @since 1.0.0 |
||
| 51 | * |
||
| 52 | * @access private |
||
| 53 | */ |
||
| 54 | public function __construct() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Registers the admin page which will house the importer form. |
||
| 61 | */ |
||
| 62 | public function register_importer_page() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Enqueue the JS needed to contact wetu and return your result. |
||
| 68 | */ |
||
| 69 | public function admin_scripts() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Display the importer administration screen |
||
| 87 | */ |
||
| 88 | public function display_page() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The header of the item list |
||
| 119 | */ |
||
| 120 | public function table_header() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The footer of the item list |
||
| 138 | */ |
||
| 139 | public function table_footer() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * set_taxonomy with some terms |
||
| 157 | */ |
||
| 158 | public function set_taxonomy($taxonomy,$terms,$id) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * set_taxonomy with some terms |
||
| 194 | */ |
||
| 195 | public function team_member_checkboxes($selected=array()) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * set_taxonomy with some terms |
||
| 220 | */ |
||
| 221 | public function taxonomy_checkboxes($taxonomy=false,$selected=array()) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Saves the room data |
||
| 241 | */ |
||
| 242 | public function save_custom_field($value=false,$meta_key,$id,$decrease=false,$unique=true) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * grabs any attachments for the current item |
||
| 260 | */ |
||
| 261 | public function find_attachments($id=false) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks to see if an item is checked. |
||
| 287 | * |
||
| 288 | * @param $haystack array|string |
||
| 289 | * @param $needle string |
||
| 290 | * @param $echo bool |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function checked($haystack=false,$needle='',$echo=true) { |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Checks to see if an item is checked. |
||
| 305 | * |
||
| 306 | * @param $haystack array|string |
||
| 307 | * @param $needle string |
||
| 308 | * @param $echo bool |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function selected($haystack=false,$needle='',$echo=true) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Checks to see if an item is selected. If $echo is false, it will return the $type if conditions are true. |
||
| 323 | * |
||
| 324 | * @param $haystack array|string |
||
| 325 | * @param $needle string |
||
| 326 | * @param $type string |
||
| 327 | * @param $wrap bool |
||
| 328 | * @return $html string |
||
| 329 | */ |
||
| 330 | public function itemd($haystack=false,$needle='',$type='',$wrap=true) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Displays the importers navigation |
||
| 350 | * |
||
| 351 | * @param $tab string |
||
| 352 | */ |
||
| 353 | public function navigation($tab='') { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Grabs the custom fields, and resaves an array of unique items. |
||
| 370 | */ |
||
| 371 | public function cleanup_posts() { |
||
| 383 | } |
||
| 384 | $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
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.