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 |
||
| 50 | class User extends Model implements UserInterface |
||
| 51 | { |
||
| 52 | use Authenticatable, |
||
| 53 | CanResetPassword, |
||
| 54 | UserRelations, |
||
| 55 | UserScopes, |
||
| 56 | LoggedUser; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * User name is private. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | const PRIVATE_YES = 1; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * User name is public. |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | const PRIVATE_NO = 0; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * User status Deleted. |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | const DELETED_USERS = 1; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * User status not deleted. |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | const NOT_DELETED_USERS = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * User status active. (Standard). |
||
| 88 | * |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | const ACTIVE_USER = 1; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * User status blocked. (Too many login attempts). |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | const BLOCKED_USER = 2; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * User status inactive. (Cannot login at the moment). |
||
| 102 | * |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | const INACTIVE_USER = 0; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The database table used by the model. |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $table = 'users'; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The attributes that are mass assignable. |
||
| 116 | * |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status']; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The attributes excluded from the model's JSON form. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $hidden = ['password', 'remember_token']; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Collection of user permissions. |
||
| 130 | * |
||
| 131 | * @var Eloquent\Collection |
||
| 132 | */ |
||
| 133 | protected $permission; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Checks to see if $this user is current user. |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function me() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Whether or not the user has a permission. |
||
| 147 | * |
||
| 148 | * @param string $key |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function permission($key) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Load user permissions. |
||
| 166 | * |
||
| 167 | * @return Eloquent\Collection |
||
| 168 | */ |
||
| 169 | View Code Duplication | protected function loadPermissions() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Return user full name with property "fullname". |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getFullNameAttribute() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return user image. |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getImageAttribute() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns list of user statuses. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public static function getStatuses() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Whether or not the user is active. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function isActive() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Whether or not the user is inactive. |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isInactive() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Whether or not the user is blocked. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isBlocked() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Whether or not the user is normal user role. |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function isUser() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Whether or not the user is administrator. |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function isAdmin() |
||
| 267 | |||
| 268 | public function getRoleName() |
||
| 272 | } |
||
| 273 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: