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 | * Create a new instance from the user ID. |
||
43 | * |
||
44 | * @param string|int $id User ID |
||
45 | * |
||
46 | * @throws UserNotFoundException |
||
47 | * |
||
48 | * @return static |
||
49 | */ |
||
50 | View Code Duplication | public static function fromID($id) |
|
58 | |||
59 | /** |
||
60 | * Create a new instance from the username. |
||
61 | * |
||
62 | * @param string $username Username (login) |
||
63 | * |
||
64 | * @throws UserNotFoundException |
||
65 | * |
||
66 | * @return static |
||
67 | */ |
||
68 | public static function fromUsername($username) |
||
76 | |||
77 | /** |
||
78 | * Create a new instance from the user's email address. |
||
79 | * |
||
80 | * @param string $email User email address |
||
81 | * |
||
82 | * @throws UserNotFoundException |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | View Code Duplication | public static function fromEmail($email) |
|
94 | |||
95 | /** |
||
96 | * Create a new instance from the user's slug. |
||
97 | * |
||
98 | * @param string $slug User slug (nicename) |
||
99 | * |
||
100 | * @throws UserNotFoundException |
||
101 | * |
||
102 | * @return static |
||
103 | */ |
||
104 | public static function fromSlug($slug) |
||
112 | |||
113 | /** |
||
114 | * Create a new instance using the currently authenticated user. |
||
115 | * |
||
116 | * @return static |
||
117 | */ |
||
118 | public static function auth() |
||
122 | |||
123 | /** |
||
124 | * Get the URL for the user's posts archive. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function postsUrl() |
||
132 | |||
133 | /** |
||
134 | * Get a new query builder for the model. |
||
135 | * |
||
136 | * @return \Silk\Contracts\BuildsQueries |
||
137 | */ |
||
138 | public function newQuery() |
||
142 | |||
143 | /** |
||
144 | * Save the changes to the database. |
||
145 | * |
||
146 | * @throws WP_ErrorException |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | View Code Duplication | public function save() |
|
166 | |||
167 | /** |
||
168 | * Delete the modeled record from the database. |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function delete() |
||
180 | |||
181 | /** |
||
182 | * Reload the object from the database. |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function refresh() |
||
192 | |||
193 | /** |
||
194 | * Set the WP_User object on the model. |
||
195 | * |
||
196 | * @param WP_User $user |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | protected function setObject($user) |
||
206 | |||
207 | /** |
||
208 | * Normalize the user data object on the given User. |
||
209 | * |
||
210 | * This is necessary for object aliases and shorthand properties to work properly |
||
211 | * due to the fact that the WP_User's data object is a plain object which |
||
212 | * does not always contain all properties as is the case with other WP objects. |
||
213 | * |
||
214 | * @param WP_User $user |
||
215 | * |
||
216 | * @return WP_User |
||
217 | */ |
||
218 | protected function normalizeData(WP_User $user) |
||
239 | |||
240 | /** |
||
241 | * Get the aliased object. |
||
242 | * |
||
243 | * Most user data from the database is stored as an object on the user's `data` property. |
||
244 | * |
||
245 | * @return object|\stdClass |
||
246 | */ |
||
247 | protected function getAliasedObject() |
||
251 | } |
||
252 |