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 array|WP_User $user User object or array of attributes |
||
| 37 | */ |
||
| 38 | public function __construct($user = null) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Create a new instance from the user ID. |
||
| 53 | * |
||
| 54 | * @param string|int $id User ID |
||
| 55 | * |
||
| 56 | * @throws UserNotFoundException |
||
| 57 | * |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | View Code Duplication | public static function fromID($id) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Create a new instance from the username. |
||
| 71 | * |
||
| 72 | * @param string $username Username (login) |
||
| 73 | * |
||
| 74 | * @throws UserNotFoundException |
||
| 75 | * |
||
| 76 | * @return static |
||
| 77 | */ |
||
| 78 | public static function fromUsername($username) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Create a new instance from the user's email address. |
||
| 89 | * |
||
| 90 | * @param string $email User email address |
||
| 91 | * |
||
| 92 | * @throws UserNotFoundException |
||
| 93 | * |
||
| 94 | * @return static |
||
| 95 | */ |
||
| 96 | View Code Duplication | public static function fromEmail($email) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Create a new instance from the user's slug. |
||
| 107 | * |
||
| 108 | * @param string $slug User slug (nicename) |
||
| 109 | * |
||
| 110 | * @throws UserNotFoundException |
||
| 111 | * |
||
| 112 | * @return static |
||
| 113 | */ |
||
| 114 | View Code Duplication | public static function fromSlug($slug) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Create a new instance using the currently authenticated user. |
||
| 125 | * |
||
| 126 | * @return static |
||
| 127 | */ |
||
| 128 | public static function auth() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the URL for the user's posts archive. |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function postsUrl() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get a new query builder for the model. |
||
| 145 | * |
||
| 146 | * @return \Silk\Contracts\BuildsQueries |
||
| 147 | */ |
||
| 148 | public function newQuery() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Save the changes to the database. |
||
| 155 | * |
||
| 156 | * @throws WP_ErrorException |
||
| 157 | * |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function save() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Delete the modeled record from the database. |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function delete() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Reload the object from the database. |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function refresh() |
||
| 202 | } |
||
| 203 |