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 |
||
| 22 | class Licenses extends Admin_Page { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor. |
||
| 26 | * |
||
| 27 | * @since 3.0.0 |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function __construct() { |
|
|
|
|||
| 30 | |||
| 31 | $this->id = $tab = 'licenses'; |
||
| 32 | $this->option_group = $page = 'settings'; |
||
| 33 | $this->label = __( 'Add-on Licenses', 'google-calendar-events' ); |
||
| 34 | //$this->description = __( 'Manage your premium add-on license keys.', 'google-calendar-events' ); |
||
| 35 | $this->sections = $this->add_sections(); |
||
| 36 | $this->fields = $this->add_fields(); |
||
| 37 | |||
| 38 | // Disabled the 'save changes' button for this page. |
||
| 39 | add_filter( 'simcal_admin_page_' . $page . '_' . $tab . '_submit', function() { return false; } ); |
||
| 40 | |||
| 41 | // Add html to page. |
||
| 42 | add_action( 'simcal_admin_page_' . $page . '_' . $tab . '_end', array( __CLASS__, 'html' ) ); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Add additional html. |
||
| 47 | * |
||
| 48 | * @since 3.0.0 |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public static function html() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add sections. |
||
| 68 | * |
||
| 69 | * @since 3.0.0 |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function add_sections() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Add fields. |
||
| 87 | * |
||
| 88 | * @since 3.0.0 |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function add_fields() { |
||
| 130 | |||
| 131 | } |
||
| 132 |
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.