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 |
||
12 | class User extends Authenticatable |
||
13 | { |
||
14 | use NullableFields, ImageStorage, Notifiable; |
||
15 | |||
16 | /** |
||
17 | * The user status. |
||
18 | */ |
||
19 | const STATUS_NORMAL = 1; |
||
20 | |||
21 | /** |
||
22 | * The directory stores all users' avatars. |
||
23 | */ |
||
24 | const AVATAR_DIRECTORY = 'avatar'; |
||
25 | |||
26 | /** |
||
27 | * The avatar size. |
||
28 | */ |
||
29 | const AVATAR_SIZE = 200; |
||
30 | const ORIGINAL_AVATAR_SIZE = 640; |
||
31 | |||
32 | /** |
||
33 | * The attributes that should be visible in arrays. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $visible = [ |
||
38 | 'id', 'email', 'phone', 'username', 'avatar', 'original_avatar', 'status', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * The attributes that should be mutated to dates. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $dates = ['last_login_at']; |
||
47 | |||
48 | /** |
||
49 | * The attributes that should be saved as null when empty. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $nullable = [ |
||
54 | 'email', 'phone', 'username', 'avatar', 'original_avatar', |
||
55 | 'last_login_ip', 'registered_ip', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * The model's attributes. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $attributes = [ |
||
64 | 'status' => 1, |
||
65 | 'login_count' => 0, |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * Get the `avatar` attribute. |
||
70 | * |
||
71 | * @param string|null $value |
||
72 | * @return string|null |
||
73 | */ |
||
74 | public function getAvatarAttribute($value) |
||
78 | |||
79 | /** |
||
80 | * Set the `avatar` attribute. |
||
81 | * |
||
82 | * @param string|null $value |
||
83 | */ |
||
84 | public function setAvatarAttribute($value) |
||
85 | { |
||
86 | $this->attributes['avatar'] = $value; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the `original_avatar` attribute. |
||
91 | * |
||
92 | * @param string|null $value |
||
93 | * @return string|null |
||
94 | */ |
||
95 | public function getOriginalAvatarAttribute($value) |
||
96 | { |
||
97 | return $this->getAssetUrl($value, 'original_avatar'); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set the `original_avatar` attribute. |
||
102 | * |
||
103 | * @param string|null $value |
||
104 | */ |
||
105 | public function setOriginalAvatarAttribute($value) |
||
106 | { |
||
107 | $this->attributes['original_avatar'] = $value; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Update login info. |
||
112 | * |
||
113 | * @param bool $save |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function updateLoginInfo($save = true) |
||
117 | { |
||
118 | $this->login_count++; |
||
119 | $this->last_login_at = $this->freshTimestamp(); |
||
120 | $this->last_login_ip = Request::ip(); |
||
121 | |||
122 | if ($save) { |
||
123 | $this->save(); |
||
124 | } |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Update user info from social user. |
||
131 | * |
||
132 | * @param mixed $social |
||
133 | * @param array $user |
||
134 | * @param bool $save |
||
135 | */ |
||
136 | public function updateUserInfoWithSocialUser($social, $user, $save = false) |
||
137 | { |
||
138 | $this->storeAvatarFile(SocialAuth::getAvatarFromSocialUser($social, $user)); |
||
139 | |||
140 | $this->username = str_limit2(SocialAuth::getUsernameFromSocialUser($social, $user), 10); |
||
141 | |||
142 | if ($save) { |
||
143 | $this->save(); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Store the given file as user's avatar. |
||
149 | * |
||
150 | * @param mixed $file |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function storeAvatarFile($file) |
||
154 | { |
||
155 | if (is_string($file) && filter_var($file, FILTER_VALIDATE_URL) !== false) { |
||
156 | $file = app('image')->make($file); |
||
157 | } |
||
158 | |||
159 | if (($avatar = $this->storeImageFile(clone $file, 'avatar')) && |
||
160 | ($original_avatar = $this->storeImageFile($file, 'original_avatar')) |
||
161 | ) { |
||
162 | $this->avatar = $avatar; |
||
163 | $this->original_avatar = $original_avatar; |
||
|
|||
164 | |||
165 | return true; |
||
166 | } |
||
167 | |||
168 | return false; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get image filter. |
||
173 | * |
||
174 | * @see http://image.intervention.io/api/filter |
||
175 | * |
||
176 | * @param string|null $identifier |
||
177 | */ |
||
178 | protected function getImageFilter($identifier = null) |
||
179 | { |
||
180 | return (new Fit)->width(constant('static::'.strtoupper($identifier).'_SIZE')); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get image directory for the given attribute. |
||
185 | * |
||
186 | * @param string $attribute |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function getImageDirectory($attribute) |
||
190 | { |
||
191 | return static::AVATAR_DIRECTORY.'/'.dechex((int) date('Y') - 2010).'/'.dechex(date('W')); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get user's devices. |
||
196 | * |
||
197 | * @param bool $withTrashed |
||
198 | * @return \Illuminate\Support\Collection |
||
199 | */ |
||
200 | View Code Duplication | public function getDevices($withTrashed = false) |
|
201 | { |
||
202 | return Device::whereIn('id', function ($query) use ($withTrashed) { |
||
203 | $query->select('device_id')->from('user_devices')->where('user_id', $this->id); |
||
204 | |||
205 | if (! $withTrashed) { |
||
206 | $query->where('deleted_at', null); |
||
207 | } |
||
208 | })->get(); |
||
209 | } |
||
210 | } |
||
211 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.