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 |
||
27 | abstract class Model |
||
28 | { |
||
29 | use QueryBuilder; |
||
30 | |||
31 | /** |
||
32 | * The term's taxonomy |
||
33 | * @var string |
||
34 | */ |
||
35 | const TAXONOMY = ''; |
||
36 | |||
37 | /** |
||
38 | * The term object |
||
39 | * @var WP_Term |
||
40 | */ |
||
41 | protected $term; |
||
42 | |||
43 | /** |
||
44 | * Model Constructor. |
||
45 | * |
||
46 | * @param mixed $term WP_Term to fill data from |
||
47 | */ |
||
48 | public function __construct(WP_Term $term = null) |
||
59 | |||
60 | /** |
||
61 | * Create a new instance from a WP_Term object. |
||
62 | * |
||
63 | * @param WP_Term $term [description] |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | public static function fromWpTerm(WP_Term $term) |
||
71 | |||
72 | /** |
||
73 | * Create a new instance from a term ID. |
||
74 | * |
||
75 | * @param int|string $id Term ID |
||
76 | * |
||
77 | * @return static |
||
78 | */ |
||
79 | View Code Duplication | public static function fromID($id) |
|
87 | |||
88 | /** |
||
89 | * Create a new instance from a slug. |
||
90 | * |
||
91 | * @param string $slug Term slug |
||
92 | * |
||
93 | * @return static |
||
94 | */ |
||
95 | View Code Duplication | public static function fromSlug($slug) |
|
103 | |||
104 | /** |
||
105 | * Create a new instance from an array of attributes. |
||
106 | * |
||
107 | * @param array $attributes [description] |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public static function fromArray(array $attributes) |
||
117 | |||
118 | /** |
||
119 | * Create a new term, and get the instance for it. |
||
120 | * |
||
121 | * @param array $attributes Term attributes |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | public static function create(array $attributes = []) |
||
134 | |||
135 | /** |
||
136 | * Save or update the term instance in the database. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function save() |
||
158 | |||
159 | /** |
||
160 | * Delete the term from the database. |
||
161 | * |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function delete() |
||
173 | |||
174 | /** |
||
175 | * Check if this term exists in the database. |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | public function exists() |
||
183 | |||
184 | /** |
||
185 | * Check if this term exists in the database as the child of the given parent. |
||
186 | * |
||
187 | * @param int|string|object $parent integer Parent term ID |
||
188 | * string Parent term slug or name |
||
189 | * object The parent term object/model. |
||
190 | * |
||
191 | * @return boolean True if the this term and the parent |
||
192 | * exist in the database, and the instance |
||
193 | * is a child of the given parent; |
||
194 | * otherwise false |
||
195 | */ |
||
196 | public function isChildOf($parent) |
||
204 | |||
205 | /** |
||
206 | * Get the parent term instance. |
||
207 | * |
||
208 | * @return static |
||
209 | */ |
||
210 | public function parent() |
||
214 | |||
215 | /** |
||
216 | * Get all ancestors of this term as a collection. |
||
217 | * |
||
218 | * @return Collection |
||
219 | */ |
||
220 | public function ancestors() |
||
227 | |||
228 | /** |
||
229 | * Meta API for this term |
||
230 | * |
||
231 | * @param string $key Meta key to retreive or empty to retreive all. |
||
232 | * |
||
233 | * @return object |
||
234 | */ |
||
235 | View Code Duplication | public function meta($key = '') |
|
245 | |||
246 | /** |
||
247 | * Get the Taxonomy model. |
||
248 | * |
||
249 | * @return Taxonomy |
||
250 | */ |
||
251 | public function taxonomy() |
||
255 | |||
256 | /** |
||
257 | * Start a new query for terms of this type. |
||
258 | * |
||
259 | * @return TermQueryBuilder |
||
260 | */ |
||
261 | public function newQuery() |
||
265 | |||
266 | /** |
||
267 | * Magic Getter. |
||
268 | * |
||
269 | * @param string $property Property name accessed |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public function __get($property) |
||
285 | |||
286 | /** |
||
287 | * Magic set checker. |
||
288 | * |
||
289 | * @param string $property Property name queried |
||
290 | * |
||
291 | * @return boolean |
||
292 | */ |
||
293 | public function __isset($property) |
||
297 | |||
298 | /** |
||
299 | * Magic Setter. |
||
300 | * |
||
301 | * @param string $property Property name assigned |
||
302 | * @param mixed $value Assigned property value |
||
303 | */ |
||
304 | public function __set($property, $value) |
||
308 | } |
||
309 |
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.