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 | * Type object property aliases |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $objectAliases = [ |
||
27 | 'email' => 'user_email', |
||
28 | 'slug' => 'user_nicename', |
||
29 | 'username' => 'user_login', |
||
30 | 'password' => 'user_pass', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * User Constructor. |
||
35 | * |
||
36 | * @param WP_User $user |
||
37 | */ |
||
38 | public function __construct(WP_User $user = null) |
||
46 | |||
47 | /** |
||
48 | * Create a new instance from the user ID. |
||
49 | * |
||
50 | * @param string|int $id User ID |
||
51 | * |
||
52 | * @throws UserNotFoundException |
||
53 | * |
||
54 | * @return static |
||
55 | */ |
||
56 | View Code Duplication | public static function fromID($id) |
|
64 | |||
65 | /** |
||
66 | * Create a new instance from the username. |
||
67 | * |
||
68 | * @param string $username Username (login) |
||
69 | * |
||
70 | * @throws UserNotFoundException |
||
71 | * |
||
72 | * @return static |
||
73 | */ |
||
74 | public static function fromUsername($username) |
||
82 | |||
83 | /** |
||
84 | * Create a new instance from the user's email address. |
||
85 | * |
||
86 | * @param string $email User email address |
||
87 | * |
||
88 | * @throws UserNotFoundException |
||
89 | * |
||
90 | * @return static |
||
91 | */ |
||
92 | View Code Duplication | public static function fromEmail($email) |
|
100 | |||
101 | /** |
||
102 | * Create a new instance from the user's slug. |
||
103 | * |
||
104 | * @param string $slug User slug (nicename) |
||
105 | * |
||
106 | * @throws UserNotFoundException |
||
107 | * |
||
108 | * @return static |
||
109 | */ |
||
110 | public static function fromSlug($slug) |
||
118 | |||
119 | /** |
||
120 | * Get the URL for the user's posts archive. |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function postsUrl() |
||
128 | |||
129 | /** |
||
130 | * Get a new query builder for the model. |
||
131 | * |
||
132 | * @return \Silk\Contracts\BuildsQueries |
||
133 | */ |
||
134 | public function newQuery() |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Save the changes to the database. |
||
142 | * |
||
143 | * @throws WP_ErrorException |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function save() |
||
163 | |||
164 | /** |
||
165 | * Delete the modeled record from the database. |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function delete() |
||
177 | |||
178 | /** |
||
179 | * Reload the object from the database. |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function refresh() |
||
189 | } |
||
190 |
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.