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 |
||
42 | abstract class Model extends BaseModel |
||
43 | { |
||
44 | /** |
||
45 | * The post type of the post this model wraps |
||
46 | * @var string |
||
47 | */ |
||
48 | const POST_TYPE = ''; |
||
49 | |||
50 | /** |
||
51 | * The object type in WordPress |
||
52 | * @var string |
||
53 | */ |
||
54 | const OBJECT_TYPE = 'post'; |
||
55 | |||
56 | /** |
||
57 | * The primary ID property on the object |
||
58 | */ |
||
59 | const ID_PROPERTY = 'ID'; |
||
60 | |||
61 | /** |
||
62 | * Create a new instance |
||
63 | * |
||
64 | * @param array|WP_Post $post Post object or array of attributes |
||
65 | * |
||
66 | * @throws ModelPostTypeMismatchException |
||
67 | */ |
||
68 | View Code Duplication | public function __construct($post = []) |
|
83 | |||
84 | /** |
||
85 | * Retrieve a new instance by the ID. |
||
86 | * |
||
87 | * @param int|string $id Primary ID |
||
88 | * |
||
89 | * @return null|static |
||
90 | */ |
||
91 | public static function find($id) |
||
99 | |||
100 | /** |
||
101 | * Create a new instance from a Post with the given ID |
||
102 | * |
||
103 | * @param int|string $id Post ID of post to create the instance from |
||
104 | * |
||
105 | * @return static |
||
106 | */ |
||
107 | public static function fromID($id) |
||
117 | |||
118 | /** |
||
119 | * Create a new instance from a Post with the given slug |
||
120 | * |
||
121 | * @param string $slug the post slug |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | public static function fromSlug($slug) |
||
135 | |||
136 | /** |
||
137 | * Create a new instance from the global $post |
||
138 | * |
||
139 | * @return static |
||
140 | */ |
||
141 | public static function fromGlobal() |
||
149 | |||
150 | /** |
||
151 | * Get the post type identifier for this model |
||
152 | * |
||
153 | * @return string post type identifier (slug) |
||
154 | */ |
||
155 | public static function postTypeId() |
||
159 | |||
160 | /** |
||
161 | * Get the post type API |
||
162 | * |
||
163 | * @return mixed Loads an existing type as a new PostType, |
||
164 | * or returns a new PostTypeBuilder for registering a new type. |
||
165 | */ |
||
166 | public static function postType() |
||
170 | |||
171 | /** |
||
172 | * Get the permalink URL. |
||
173 | * |
||
174 | * @return string|bool The permalink URL, or false if the post does not exist. |
||
175 | */ |
||
176 | public function url() |
||
180 | |||
181 | /** |
||
182 | * Send the post to the trash |
||
183 | * |
||
184 | * If trash is disabled, the post or page is permanently deleted. |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function trash() |
||
196 | |||
197 | /** |
||
198 | * Restore a post or page from the Trash |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function untrash() |
||
210 | |||
211 | /** |
||
212 | * Get a new query builder for the model. |
||
213 | * |
||
214 | * @return QueryBuilder |
||
215 | */ |
||
216 | public function newQuery() |
||
220 | |||
221 | /** |
||
222 | * Save the post to the database. |
||
223 | * |
||
224 | * @throws WP_ErrorException |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | View Code Duplication | public function save() |
|
244 | |||
245 | /** |
||
246 | * Permanently delete the post from the database. |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function delete() |
||
258 | |||
259 | /** |
||
260 | * Update the modeled object with the current state from the database. |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function refresh() |
||
270 | } |
||
271 |
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.