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 Account extends Model |
||
11 | { |
||
12 | const ACCOUNT_DISABLE = 0; |
||
13 | const ACCOUNT_ENABLE = 1; |
||
14 | |||
15 | const ACCOUNT_STATUS = [ |
||
16 | 0 => [ 'text' => 'accounts.disabled', |
||
17 | 'icon' => 'fa-ban', |
||
18 | 'unicon' => '' ], |
||
19 | 1 => [ 'text' => 'accounts.enabled', |
||
20 | 'icon' => 'fa-globe', |
||
21 | 'unicon' => '' ] |
||
22 | ]; |
||
23 | |||
24 | const SEARCH_CRITERIA = [ |
||
25 | 'account' => ['netlogin'], |
||
26 | 'fullname' => ['firstname', 'lastname'], |
||
27 | 'group' => [['group' => 'name']], |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * The attributes that are mass assignable. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $fillable = ['firstname', 'lastname', 'netlogin', 'netpass', 'expire', 'status', 'group_id', 'category_id', 'created_by']; |
||
36 | |||
37 | public function creator() |
||
38 | { |
||
39 | return $this->hasOne('App\User', 'created_by'); |
||
40 | } |
||
41 | |||
42 | public function group() |
||
46 | |||
47 | View Code Duplication | public function getStatus() |
|
48 | { |
||
49 | if (array_key_exists((int)$this->status, self::ACCOUNT_STATUS)) { |
||
|
|||
50 | return self::ACCOUNT_STATUS[(int)$this->status]; |
||
51 | } else { |
||
52 | return null; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public function category() |
||
57 | { |
||
58 | return $this->hasOne('App\Category', 'id', 'category_id'); |
||
59 | } |
||
60 | |||
61 | public static function generateHash($password) |
||
62 | { |
||
63 | return APR1_MD5::hash($password); |
||
64 | } |
||
65 | |||
66 | public function enable() |
||
67 | { |
||
68 | $this->status = 1; |
||
69 | if (empty($this->expire) || $this->expire <= date('Y-m-d')) { |
||
70 | $this->expire = date_create('+90 day')->format('Y-m-d'); |
||
71 | } |
||
72 | $this->update(); |
||
73 | } |
||
74 | |||
75 | public function disable() |
||
76 | { |
||
77 | $this->status = 0; |
||
78 | $this->update(); |
||
79 | } |
||
80 | } |
||
81 |
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@property
annotation 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.