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 |
||
| 9 | class Model extends BaseModel |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The object type in WordPress |
||
| 13 | */ |
||
| 14 | const OBJECT_TYPE = 'user'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The primary ID property on the object |
||
| 18 | */ |
||
| 19 | const ID_PROPERTY = 'ID'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Type object property aliases |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $objectAliases = [ |
||
| 26 | 'email' => 'user_email', |
||
| 27 | 'slug' => 'user_nicename', |
||
| 28 | 'username' => 'user_login', |
||
| 29 | 'password' => 'user_pass', |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * User Constructor. |
||
| 34 | * |
||
| 35 | * @param WP_User $user |
||
| 36 | */ |
||
| 37 | public function __construct(WP_User $user = null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create a new instance from the user ID. |
||
| 48 | * |
||
| 49 | * @param string|int $id User ID |
||
| 50 | * |
||
| 51 | * @throws UserNotFoundException |
||
| 52 | * |
||
| 53 | * @return static |
||
| 54 | */ |
||
| 55 | View Code Duplication | public static function fromID($id) |
|
|
1 ignored issue
–
show
|
|||
| 56 | { |
||
| 57 | if (! $user = get_user_by('id', $id)) { |
||
| 58 | throw new UserNotFoundException("No user found with ID $id"); |
||
| 59 | } |
||
| 60 | |||
| 61 | return new static($user); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Create a new instance from the username. |
||
| 66 | * |
||
| 67 | * @param string $username Username (login) |
||
| 68 | * |
||
| 69 | * @throws UserNotFoundException |
||
| 70 | * |
||
| 71 | * @return static |
||
| 72 | */ |
||
| 73 | public static function fromUsername($username) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Create a new instance from the user's email address. |
||
| 84 | * |
||
| 85 | * @param string $email User email address |
||
| 86 | * |
||
| 87 | * @throws UserNotFoundException |
||
| 88 | * |
||
| 89 | * @return static |
||
| 90 | */ |
||
| 91 | View Code Duplication | public static function fromEmail($email) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Create a new instance from the user's slug. |
||
| 102 | * |
||
| 103 | * @param string $slug User slug (nicename) |
||
| 104 | * |
||
| 105 | * @throws UserNotFoundException |
||
| 106 | * |
||
| 107 | * @return static |
||
| 108 | */ |
||
| 109 | public static function fromSlug($slug) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get a new query builder for the model. |
||
| 120 | * |
||
| 121 | * @return \Silk\Contracts\BuildsQueries |
||
| 122 | */ |
||
| 123 | public function newQuery() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the map of action => class for resolving active actions. |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | protected function actionClasses() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Magic getter. |
||
| 143 | * |
||
| 144 | * @param string $property |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function __get($property) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Magic setter. |
||
| 159 | * |
||
| 160 | * @param string $property The property name |
||
| 161 | * @param mixed $value The new property value |
||
| 162 | */ |
||
| 163 | public function __set($property, $value) |
||
| 171 | } |
||
| 172 |