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 |
||
| 8 | class Form |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Singleton The reference to *Singleton* instance of this class |
||
| 12 | */ |
||
| 13 | private static $instance; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Array Stores a form for each taxonomy |
||
| 17 | */ |
||
| 18 | private $forms = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Returns the *Singleton* instance of this class. |
||
| 22 | * |
||
| 23 | * @return Singleton The *Singleton* instance. |
||
| 24 | */ |
||
| 25 | public static function get_instance() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Add a form field to both the add & edit forms for a given taxonomy. |
||
| 36 | * |
||
| 37 | * @param string $taxonomy_name |
||
| 38 | * @param array $component |
||
| 39 | * @throws \RuntimeException if duplicate names are registered under the same taxonomy |
||
| 40 | */ |
||
| 41 | public function add_field( $taxonomy_name, $component ) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Render the 'edit term' form for a given taxonomy |
||
| 69 | * |
||
| 70 | * @param object $term Taxonomy term |
||
| 71 | */ |
||
| 72 | public function render_edit_form( $term ) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Render the 'add new term' form for a given taxonomy |
||
| 89 | * |
||
| 90 | * @param string $taxonomy Taxonomy name |
||
| 91 | */ |
||
| 92 | public function render_add_form( $taxonomy ) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Update the meta values for a given term. Called once one of the add/edit |
||
| 102 | * forms is saved. |
||
| 103 | * |
||
| 104 | * @param type $term_id |
||
| 105 | */ |
||
| 106 | function update_term( $term_id ) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Add additional columns to the term table. |
||
| 122 | * |
||
| 123 | * @param array $columns |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | View Code Duplication | function modify_table_columns( $columns ) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve the data for a given column in the term table. |
||
| 140 | * |
||
| 141 | * @see https://developer.wordpress.org/reference/hooks/manage_this-screen-taxonomy_custom_column/ |
||
| 142 | * |
||
| 143 | * @param type $content |
||
| 144 | * @param type $column_name |
||
| 145 | * @param type $term_id |
||
| 146 | * @return type |
||
| 147 | */ |
||
| 148 | function modify_table_content( $content, $column_name, $term_id ) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Make custom table columns sortable. |
||
| 165 | * |
||
| 166 | * @param array $columns |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | View Code Duplication | function modify_table_sortable_columns( $columns ) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Modify terms_clauses to allow sorting custom WordPress Admin Table Columns by a custom Taxonomy Term meta |
||
| 184 | * |
||
| 185 | * @see https://developer.wordpress.org/reference/hooks/terms_clauses/ |
||
| 186 | * |
||
| 187 | * @global type $wpdb |
||
| 188 | * @param type $clauses |
||
| 189 | * @param type $taxonomies |
||
| 190 | * @param type $args |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function sort_custom_column( $clauses, $taxonomies, $args ) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * The default form field properties. This is merged with the user given |
||
| 214 | * properties. When the component is rendered, this will be merged with the |
||
| 215 | * component's properties as well. |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | private function default_props() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Treverse the $fields array. |
||
| 234 | * |
||
| 235 | * @param collable $callback Called on each iteration |
||
| 236 | */ |
||
| 237 | private function traverse_components( $callback ) |
||
| 247 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: