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 |
||
26 | abstract class Model extends BaseModel |
||
27 | { |
||
28 | /** |
||
29 | * The term's taxonomy |
||
30 | * @var string |
||
31 | */ |
||
32 | const TAXONOMY = ''; |
||
33 | |||
34 | /** |
||
35 | * The object type in WordPress |
||
36 | * @var string |
||
37 | */ |
||
38 | const OBJECT_TYPE = 'term'; |
||
39 | |||
40 | /** |
||
41 | * The primary ID property on the object |
||
42 | */ |
||
43 | const ID_PROPERTY = 'term_id'; |
||
44 | |||
45 | /** |
||
46 | * Model Constructor. |
||
47 | * |
||
48 | * @param array|WP_Term $term WP_Term |
||
49 | * |
||
50 | * @throws TaxonomyMismatchException |
||
51 | */ |
||
52 | View Code Duplication | public function __construct($term = []) |
|
67 | |||
68 | /** |
||
69 | * Retrieve a new instance by the ID. |
||
70 | * |
||
71 | * @param int|string $id Primary ID |
||
72 | * |
||
73 | * @return null|static |
||
74 | */ |
||
75 | public static function find($id) |
||
83 | |||
84 | /** |
||
85 | * Create a new instance from a term ID. |
||
86 | * |
||
87 | * @param int|string $id Term ID |
||
88 | * |
||
89 | * @throws TermNotFoundException |
||
90 | * |
||
91 | * @return static |
||
92 | */ |
||
93 | public static function fromID($id) |
||
101 | |||
102 | /** |
||
103 | * Create a new instance from a slug. |
||
104 | * |
||
105 | * @param string $slug Term slug |
||
106 | * |
||
107 | * @throws TermNotFoundException |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public static function fromSlug($slug) |
||
119 | |||
120 | /** |
||
121 | * Check if this term exists in the database. |
||
122 | * |
||
123 | * @return boolean |
||
124 | */ |
||
125 | public function exists() |
||
129 | |||
130 | /** |
||
131 | * Check if this term exists in the database as the child of the given parent. |
||
132 | * |
||
133 | * @param int|string|object $parent integer Parent term ID |
||
134 | * string Parent term slug or name |
||
135 | * object The parent term object/model. |
||
136 | * |
||
137 | * @return boolean True if the this term and the parent |
||
138 | * exist in the database, and the instance |
||
139 | * is a child of the given parent; |
||
140 | * otherwise false |
||
141 | */ |
||
142 | public function isChildOf($parent) |
||
150 | |||
151 | /** |
||
152 | * Get the parent term instance. |
||
153 | * |
||
154 | * @return static |
||
155 | */ |
||
156 | public function parent() |
||
160 | |||
161 | /** |
||
162 | * Get all ancestors of this term as a collection. |
||
163 | * |
||
164 | * @return Collection |
||
165 | */ |
||
166 | public function ancestors() |
||
171 | |||
172 | /** |
||
173 | * Get all children of this term as a collection. |
||
174 | * |
||
175 | * @return Collection |
||
176 | */ |
||
177 | public function children() |
||
182 | |||
183 | /** |
||
184 | * Get the Taxonomy model. |
||
185 | * |
||
186 | * @return Taxonomy|\Silk\Taxonomy\Builder |
||
187 | */ |
||
188 | public static function taxonomy() |
||
192 | |||
193 | /** |
||
194 | * Get the URL for this term. |
||
195 | * |
||
196 | * @return string|bool |
||
197 | */ |
||
198 | public function url() |
||
208 | |||
209 | /** |
||
210 | * Start a new query for terms of this type. |
||
211 | * |
||
212 | * @return QueryBuilder |
||
213 | */ |
||
214 | public function newQuery() |
||
218 | |||
219 | /** |
||
220 | * Save the term to the database. |
||
221 | * |
||
222 | * @throws WP_ErrorException |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function save() |
||
242 | |||
243 | /** |
||
244 | * Delete the term from the database. |
||
245 | * |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function delete() |
||
256 | |||
257 | /** |
||
258 | * Reload the term object from the database. |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function refresh() |
||
268 | } |
||
269 |
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.