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 |
||
| 7 | class User extends Authenticatable |
||
| 8 | { |
||
| 9 | const USER_LEVEL_LOCAL = 1; |
||
| 10 | const USER_LEVEL_GLOBAL = 3; |
||
| 11 | const USER_LEVEL_ADMIN = 5; |
||
| 12 | const USER_LEVEL_SUPER = 9; |
||
| 13 | |||
| 14 | const USER_DISABLED = 0; |
||
| 15 | const USER_ENABLED = 1; |
||
| 16 | |||
| 17 | const USER_STATUS = [ |
||
| 18 | 0 => [ 'text' => 'users.disabled', |
||
| 19 | 'icon' => '', |
||
| 20 | 'unicon' => '' ], |
||
| 21 | 1 => [ 'text' => 'users.enabled', |
||
| 22 | 'icon' => 'fa-check', |
||
| 23 | 'unicon' => '' ] |
||
| 24 | ]; |
||
| 25 | |||
| 26 | const USER_LEVEL = [ |
||
| 27 | 1 => [ 'text' => 'users.access.local', |
||
| 28 | 'icon' => 'fa-support', |
||
| 29 | 'unicon' => '' ], |
||
| 30 | 3 => [ 'text' => 'users.access.global', |
||
| 31 | 'icon' => 'fa-globe', |
||
| 32 | 'unicon' => '' ], |
||
| 33 | 5 => [ 'text' => 'users.access.admin', |
||
| 34 | 'icon' => 'fa-shield', |
||
| 35 | 'unicon' => '' ], |
||
| 36 | 9 => [ 'text' => 'users.access.super', |
||
| 37 | 'icon' => 'fa-rocket', |
||
| 38 | 'unicon' => '' ], |
||
| 39 | ]; |
||
| 40 | |||
| 41 | const SEARCH_CRITERIA = [ |
||
| 42 | 'email' => ['email'], |
||
| 43 | 'fullname' => ['firstname', 'lastname'], |
||
| 44 | 'group' => [['group' => 'name']], |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The attributes that are mass assignable. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $fillable = [ |
||
| 53 | 'firstname', 'lastname', 'email', 'password', 'status', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The attributes that should be hidden for arrays. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $hidden = [ |
||
| 62 | 'password', 'remember_token', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | View Code Duplication | public function getStatus() |
|
| 66 | { |
||
| 67 | if (array_key_exists((int)$this->status, self::USER_STATUS)) { |
||
|
|
|||
| 68 | return self::USER_STATUS[(int)$this->status]; |
||
| 69 | } else { |
||
| 70 | return null; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getLevel() |
||
| 75 | { |
||
| 76 | if (array_key_exists((int)$this->level, self::USER_LEVEL)) { |
||
| 77 | return self::USER_LEVEL[(int)$this->level]; |
||
| 78 | } else { |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function creator() |
||
| 87 | |||
| 88 | public function group() |
||
| 92 | } |
||
| 93 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.