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 Model extends BaseModel |
||
12 | { |
||
13 | /** |
||
14 | * The object type in WordPress |
||
15 | */ |
||
16 | const OBJECT_TYPE = 'user'; |
||
17 | |||
18 | /** |
||
19 | * The primary ID property on the object |
||
20 | */ |
||
21 | const ID_PROPERTY = 'ID'; |
||
22 | |||
23 | /** |
||
24 | * User Constructor. |
||
25 | * |
||
26 | * @param array|WP_User $user User object or array of attributes |
||
27 | */ |
||
28 | public function __construct($user = null) |
||
40 | |||
41 | /** |
||
42 | * Retrieve a new instance by the ID. |
||
43 | * |
||
44 | * @param int|string $id Primary ID |
||
45 | * |
||
46 | * @return null|static |
||
47 | */ |
||
48 | public static function find($id) |
||
56 | |||
57 | /** |
||
58 | * Create a new instance from the user ID. |
||
59 | * |
||
60 | * @param string|int $id User ID |
||
61 | * |
||
62 | * @throws UserNotFoundException |
||
63 | * |
||
64 | * @return static |
||
65 | */ |
||
66 | View Code Duplication | public static function fromID($id) |
|
74 | |||
75 | /** |
||
76 | * Create a new instance from the username. |
||
77 | * |
||
78 | * @param string $username Username (login) |
||
79 | * |
||
80 | * @throws UserNotFoundException |
||
81 | * |
||
82 | * @return static |
||
83 | */ |
||
84 | public static function fromUsername($username) |
||
92 | |||
93 | /** |
||
94 | * Create a new instance from the user's email address. |
||
95 | * |
||
96 | * @param string $email User email address |
||
97 | * |
||
98 | * @throws UserNotFoundException |
||
99 | * |
||
100 | * @return static |
||
101 | */ |
||
102 | View Code Duplication | public static function fromEmail($email) |
|
110 | |||
111 | /** |
||
112 | * Create a new instance from the user's slug. |
||
113 | * |
||
114 | * @param string $slug User slug (nicename) |
||
115 | * |
||
116 | * @throws UserNotFoundException |
||
117 | * |
||
118 | * @return static |
||
119 | */ |
||
120 | View Code Duplication | public static function fromSlug($slug) |
|
128 | |||
129 | /** |
||
130 | * Create a new instance using the currently authenticated user. |
||
131 | * |
||
132 | * @return static |
||
133 | */ |
||
134 | public static function auth() |
||
138 | |||
139 | /** |
||
140 | * Get the URL for the user's posts archive. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function postsUrl() |
||
148 | |||
149 | /** |
||
150 | * Get a new query builder for the model. |
||
151 | * |
||
152 | * @return \Silk\Contracts\Query\BuildsQueries |
||
153 | */ |
||
154 | public function newQuery() |
||
158 | |||
159 | /** |
||
160 | * Save the changes to the database. |
||
161 | * |
||
162 | * @throws WP_ErrorException |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | View Code Duplication | public function save() |
|
182 | |||
183 | /** |
||
184 | * Delete the modeled record from the database. |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function delete() |
||
196 | |||
197 | /** |
||
198 | * Reload the object from the database. |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function refresh() |
||
208 | |||
209 | /** |
||
210 | * Set the WP_User object on the model. |
||
211 | * |
||
212 | * @param WP_User $user |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | protected function setObject($user) |
||
222 | |||
223 | /** |
||
224 | * Normalize the user data object on the given User. |
||
225 | * |
||
226 | * This is necessary for object aliases and shorthand properties to work properly |
||
227 | * due to the fact that the WP_User's data object is a plain object which |
||
228 | * does not always contain all properties as is the case with other WP objects. |
||
229 | * |
||
230 | * @param WP_User $user |
||
231 | * |
||
232 | * @return WP_User |
||
233 | */ |
||
234 | protected function normalizeData(WP_User $user) |
||
255 | |||
256 | /** |
||
257 | * Get the aliased object. |
||
258 | * |
||
259 | * Most user data from the database is stored as an object on the user's `data` property. |
||
260 | * |
||
261 | * @return object|\stdClass |
||
262 | */ |
||
263 | protected function getAliasedObject() |
||
267 | } |
||
268 |