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 |
||
11 | class Term_Meta_Container extends Container { |
||
12 | |||
13 | protected $term_id; |
||
14 | |||
15 | public $settings = array(); |
||
16 | |||
17 | /** |
||
18 | * {@inheritDoc} |
||
19 | */ |
||
20 | View Code Duplication | public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) { |
|
27 | |||
28 | /** |
||
29 | * Bind attach() and save() to the appropriate WordPress actions. |
||
30 | */ |
||
31 | public function init() { |
||
35 | |||
36 | /** |
||
37 | * Hook to relevant taxonomies |
||
38 | */ |
||
39 | View Code Duplication | public function hook_to_taxonomies() { |
|
47 | |||
48 | /** |
||
49 | * Checks whether the current save request is valid |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | View Code Duplication | public function is_valid_save() { |
|
61 | |||
62 | /** |
||
63 | * Perform save operation after successful is_valid_save() check. |
||
64 | * The call is propagated to all fields in the container. |
||
65 | * |
||
66 | * @param int $term_id ID of the term against which save() is ran |
||
67 | */ |
||
68 | View Code Duplication | public function save( $term_id = null ) { |
|
78 | |||
79 | /** |
||
80 | * Get environment array for page request (in admin) |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function get_environment_for_request() { |
||
98 | |||
99 | /** |
||
100 | * Perform checks whether the container should be attached during the current request |
||
101 | * |
||
102 | * @return bool True if the container is allowed to be attached |
||
103 | */ |
||
104 | public function is_valid_attach_for_request() { |
||
113 | |||
114 | /** |
||
115 | * Get environment array for object id |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function get_environment_for_object( $object_id ) { |
||
128 | |||
129 | /** |
||
130 | * Check container attachment rules against object id |
||
131 | * |
||
132 | * @param int $object_id |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function is_valid_attach_for_object( $object_id = null ) { |
||
145 | |||
146 | /** |
||
147 | * Add term meta for each of the container taxonomies |
||
148 | */ |
||
149 | View Code Duplication | public function attach() { |
|
157 | |||
158 | /** |
||
159 | * Output the container markup |
||
160 | */ |
||
161 | public function render( $term = null ) { |
||
168 | |||
169 | /** |
||
170 | * Set the term ID the container will operate with. |
||
171 | * |
||
172 | * @param int $term_id |
||
173 | */ |
||
174 | View Code Duplication | protected function set_term_id( $term_id ) { |
|
185 | |||
186 | /** |
||
187 | * Get array of taxonomies this container can appear on conditionally |
||
188 | * |
||
189 | * @return array<string> |
||
190 | */ |
||
191 | View Code Duplication | public function get_taxonomy_visibility() { |
|
206 | |||
207 | /** |
||
208 | * Show the container only on terms from the specified taxonomies. |
||
209 | * |
||
210 | * @deprecated |
||
211 | * @param string|array $taxonomies |
||
212 | * @return object $this |
||
213 | */ |
||
214 | public function show_on_taxonomy( $taxonomies ) { |
||
219 | |||
220 | /** |
||
221 | * Show the container only on particular term level. |
||
222 | * |
||
223 | * @deprecated |
||
224 | * @param int $term_level |
||
225 | * @return object $this |
||
226 | */ |
||
227 | public function show_on_level( $term_level ) { |
||
231 | } |
||
232 |
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.