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 User extends Authenticatable |
||
| 11 | { |
||
| 12 | use Notifiable; |
||
| 13 | use SoftDeletes; |
||
| 14 | use LogsActivity; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The attributes that should be mutated to dates. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $dates = [ |
||
| 22 | 'deleted_at' |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The attributes that are mass assignable. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $fillable = [ |
||
| 31 | 'name', 'email', 'password', 'role', 'phone', 'preferred_device_id' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The attributes to ignore in the Activity Log |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $ignoreChangedAttributes = [ |
||
| 40 | 'updated_at', 'remember_token' |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The attributes to log in the Activity Log |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected static $logAttributes = [ |
||
| 49 | 'name', 'email', 'password', 'role', 'phone', 'preferred_device_id' |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Only log those that have actually changed after the update. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $logOnlyDirty = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The attributes that should be hidden for arrays. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $hidden = [ |
||
| 65 | 'password', 'remember_token', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Update the updated_at and created_at timestamps? |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | public $timestamps = true; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Route notifications for the Nexmo channel. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function routeNotificationForNexmo() |
||
| 81 | { |
||
| 82 | return $this->phone; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Is user Admin or better? |
||
| 87 | * |
||
| 88 | * @return boolean |
||
| 89 | */ |
||
| 90 | public function isAdmin() |
||
| 91 | { |
||
| 92 | return $this->role > 2; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Is user Manager or better? |
||
| 97 | * |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | public function isManager() |
||
| 101 | { |
||
| 102 | return $this->role > 1; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Is user User or better? |
||
| 107 | * |
||
| 108 | * @return boolean |
||
| 109 | */ |
||
| 110 | public function isUser() |
||
| 111 | { |
||
| 112 | return $this->role > 0; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Is user a guest? |
||
| 117 | * |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | public function isGuest() |
||
| 121 | { |
||
| 122 | return $this->role == 0; |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a list of managers. |
||
| 128 | * |
||
| 129 | * @return Users |
||
| 130 | */ |
||
| 131 | public function managers() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the users role as a string. |
||
| 138 | * |
||
| 139 | * @return Users |
||
| 140 | */ |
||
| 141 | public function roleString() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the preferred device of the user |
||
| 149 | */ |
||
| 150 | public function preferredDevice() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Accessor: Get the user's last update time in seconds/minutes/hours since update or converted to user |
||
| 157 | * friendly readable format. |
||
| 158 | * If the time is less then a day old then display time since it last updated |
||
| 159 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 160 | * and using the user's preferred timezone |
||
| 161 | * |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function getUpdatedAtHumanAttribute() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Accessor: Get the user's creation time in seconds/minutes/hours since update or converted to user |
||
| 175 | * friendly readable format. |
||
| 176 | * If the time is less then a day old then display time since creation |
||
| 177 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 178 | * and using the user's preferred timezone |
||
| 179 | * |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function getCreatedAtHumanAttribute() |
|
| 190 | } |
||
| 191 |