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 |
||
10 | class Model extends BaseModel |
||
11 | { |
||
12 | /** |
||
13 | * The object type in WordPress |
||
14 | */ |
||
15 | const OBJECT_TYPE = 'user'; |
||
16 | |||
17 | /** |
||
18 | * The primary ID property on the object |
||
19 | */ |
||
20 | const ID_PROPERTY = 'ID'; |
||
21 | |||
22 | /** |
||
23 | * User Constructor. |
||
24 | * |
||
25 | * @param array|WP_User $user User object or array of attributes |
||
26 | */ |
||
27 | public function __construct($user = null) |
||
39 | |||
40 | /** |
||
41 | * Create a new instance from the user ID. |
||
42 | * |
||
43 | * @param string|int $id User ID |
||
44 | * |
||
45 | * @throws UserNotFoundException |
||
46 | * |
||
47 | * @return static |
||
48 | */ |
||
49 | View Code Duplication | public static function fromID($id) |
|
57 | |||
58 | /** |
||
59 | * Create a new instance from the username. |
||
60 | * |
||
61 | * @param string $username Username (login) |
||
62 | * |
||
63 | * @throws UserNotFoundException |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | public static function fromUsername($username) |
||
75 | |||
76 | /** |
||
77 | * Create a new instance from the user's email address. |
||
78 | * |
||
79 | * @param string $email User email address |
||
80 | * |
||
81 | * @throws UserNotFoundException |
||
82 | * |
||
83 | * @return static |
||
84 | */ |
||
85 | View Code Duplication | public static function fromEmail($email) |
|
93 | |||
94 | /** |
||
95 | * Create a new instance from the user's slug. |
||
96 | * |
||
97 | * @param string $slug User slug (nicename) |
||
98 | * |
||
99 | * @throws UserNotFoundException |
||
100 | * |
||
101 | * @return static |
||
102 | */ |
||
103 | public static function fromSlug($slug) |
||
104 | { |
||
105 | if (! $user = get_user_by('slug', $slug)) { |
||
106 | throw new UserNotFoundException("No user found with slug: $slug"); |
||
107 | } |
||
108 | |||
109 | return new static($user); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Create a new instance using the currently authenticated user. |
||
114 | * |
||
115 | * @return static |
||
116 | */ |
||
117 | public static function auth() |
||
121 | |||
122 | /** |
||
123 | * Get the URL for the user's posts archive. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function postsUrl() |
||
131 | |||
132 | /** |
||
133 | * Get a new query builder for the model. |
||
134 | * |
||
135 | * @return \Silk\Contracts\BuildsQueries |
||
136 | */ |
||
137 | public function newQuery() |
||
141 | |||
142 | /** |
||
143 | * Save the changes to the database. |
||
144 | * |
||
145 | * @throws WP_ErrorException |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | View Code Duplication | public function save() |
|
165 | |||
166 | /** |
||
167 | * Delete the modeled record from the database. |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function delete() |
||
179 | |||
180 | /** |
||
181 | * Reload the object from the database. |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function refresh() |
||
191 | |||
192 | /** |
||
193 | * Set the WP_User object on the model. |
||
194 | * |
||
195 | * @param WP_User $user |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | protected function setObject($user) |
||
205 | |||
206 | /** |
||
207 | * Normalize the user data object on the given User. |
||
208 | * |
||
209 | * This is necessary for object aliases and shorthand properties to work properly |
||
210 | * due to the fact that the WP_User's data object is a plain object which |
||
211 | * does not always contain all properties as is the case with other WP objects. |
||
212 | * |
||
213 | * @param WP_User $user |
||
214 | * |
||
215 | * @return WP_User |
||
216 | */ |
||
217 | protected function normalizeData(WP_User $user) |
||
241 | |||
242 | /** |
||
243 | * Get the aliased object. |
||
244 | * |
||
245 | * Most user data from the database is stored as an object on the user's `data` property. |
||
246 | * |
||
247 | * @return object|\stdClass |
||
248 | */ |
||
249 | protected function getAliasedObject() |
||
253 | } |
||
254 |