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 all the registered fields for each taxonomy |
||
| 17 | */ |
||
| 18 | private $fields = 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 string $field_name |
||
| 39 | * @param array $field_props |
||
| 40 | * @throws \RuntimeException if duplicate names are registered under the same taxonomy |
||
| 41 | */ |
||
| 42 | public function add_field( $taxonomy_name, $field_name, $field_props ) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Render the 'edit term' form for a given taxonomy |
||
| 72 | * |
||
| 73 | * @param object $term Taxonomy term |
||
| 74 | */ |
||
| 75 | public function render_edit_form( $term ) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Render the 'add new term' form for a given taxonomy |
||
| 90 | * |
||
| 91 | * @param string $taxonomy Taxonomy name |
||
| 92 | */ |
||
| 93 | public function render_add_form( $taxonomy ) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Update the meta values for a given term. Called once one of the add/edit |
||
| 107 | * forms is saved. |
||
| 108 | * |
||
| 109 | * @param type $term_id |
||
| 110 | */ |
||
| 111 | function update_term( $term_id ) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Add additional columns to the term table. |
||
| 126 | * |
||
| 127 | * @param array $columns |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | View Code Duplication | function modify_table_columns( $columns ) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Retrieve the data for a given column in the term table. |
||
| 147 | * |
||
| 148 | * @see https://developer.wordpress.org/reference/hooks/manage_this-screen-taxonomy_custom_column/ |
||
| 149 | * |
||
| 150 | * @param type $content |
||
| 151 | * @param type $column_name |
||
| 152 | * @param type $term_id |
||
| 153 | * @return type |
||
| 154 | */ |
||
| 155 | function modify_table_content( $content, $column_name, $term_id ) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Make custom table columns sortable. |
||
| 175 | * |
||
| 176 | * @param array $columns |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | View Code Duplication | function modify_table_sortable_columns( $columns ) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Modify terms_clauses to allow sorting custom WordPress Admin Table Columns by a custom Taxonomy Term meta |
||
| 197 | * |
||
| 198 | * @see https://developer.wordpress.org/reference/hooks/terms_clauses/ |
||
| 199 | * |
||
| 200 | * @global type $wpdb |
||
| 201 | * @param type $clauses |
||
| 202 | * @param type $taxonomies |
||
| 203 | * @param type $args |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | function sort_custom_column( $clauses, $taxonomies, $args ) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Modify the orderby clauses for a given taxonomy |
||
| 228 | * |
||
| 229 | * @param array $clauses |
||
| 230 | * @param string $taxonomy |
||
| 231 | * @param string $termmeta |
||
| 232 | */ |
||
| 233 | function modify_term_clauses( &$clauses, $taxonomy, $termmeta ) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * The default form field properties. This is merged with the user given |
||
| 244 | * properties. When the component is rendered, this will be merged with the |
||
| 245 | * component's properties as well. |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | private function default_props() |
||
| 261 | } |
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: