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 |
||
11 | class Device extends Model |
||
12 | { |
||
13 | use SoftDeletes; |
||
14 | use LogsActivity; |
||
15 | //use CausesActivity; |
||
16 | |||
17 | /** |
||
18 | * The attributes that should be mutated to dates. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $dates = [ |
||
23 | 'deleted_at', |
||
24 | 'last_network_update_at' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * The attributes that should be hidden for arrays. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $hidden = ['token']; |
||
33 | |||
34 | /** |
||
35 | * The attributes that are mass assignable. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $fillable = [ |
||
40 | 'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
||
41 | 'time', 'cover_command', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
||
42 | 'light_in', 'light_out', 'update_rate', 'image_rate', 'sensor_rate', |
||
43 | 'open_time', 'close_time', 'last_network_update_at', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * The attributes to ignore in the Activity Log |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $ignoreChangedAttributes = ['updated_at', 'last_network_update_at']; |
||
52 | |||
53 | /** |
||
54 | * The attributes to log in the Activity Log |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $logAttributes = [ |
||
59 | 'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
||
60 | 'time', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', |
||
61 | 'light_in', 'light_out', 'update_rate', 'image_rate', 'sensor_rate', |
||
62 | 'open_time', 'close_time' |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Only log those that have actually changed after the update. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected static $logOnlyDirty = true; |
||
71 | |||
72 | /** |
||
73 | * Update the updated_at and created_at timestamps? |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | public $timestamps = true; |
||
78 | |||
79 | /** |
||
80 | * Get the location for the device |
||
81 | */ |
||
82 | public function location() |
||
83 | { |
||
84 | return $this->belongsTo('App\Location', 'location_id'); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the site for the device using model accessor |
||
89 | */ |
||
90 | public function getSiteAttribute() |
||
91 | { |
||
92 | return $this->location->site ?? (object)[]; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Accessor: Get the open time of the device converted to hours and minutes |
||
97 | * If it is a device accessing the time use UTC |
||
98 | * If it is a user accessing the time use their preferred timezone |
||
99 | * |
||
100 | * @param string $value |
||
101 | * @return string |
||
102 | */ |
||
103 | View Code Duplication | public function getOpenTimeAttribute($value) |
|
113 | |||
114 | /** |
||
115 | * Accessor: Get the open time of the device converted to the users preferred time and |
||
116 | * converted to a user friendly format of h:i a |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | View Code Duplication | public function getOpenTimeHumanAttribute() |
|
127 | |||
128 | /** |
||
129 | * Accessor: Get the close time of the device converted to hours and minutes |
||
130 | * If it is a device accessing the time use UTC |
||
131 | * If it is a user accessing the time use their preferred timezone |
||
132 | * |
||
133 | * @param string $value |
||
134 | * @return string |
||
135 | */ |
||
136 | View Code Duplication | public function getCloseTimeAttribute($value) |
|
146 | |||
147 | /** |
||
148 | * Accessor: Get the close time of the device converted to the users preferred time and |
||
149 | * converted to a user friendly format of h:i a |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | View Code Duplication | public function getCloseTimeHumanAttribute() |
|
160 | |||
161 | /** |
||
162 | * Set the open time to UTC |
||
163 | * If it is a device saving the time use UTC |
||
164 | * If it is a user saving the time use their preferred timezone |
||
165 | * |
||
166 | * @param string $value |
||
167 | * @return void |
||
168 | */ |
||
169 | View Code Duplication | public function setOpenTimeAttribute($value) |
|
182 | |||
183 | /** |
||
184 | * Set the close time to UTC |
||
185 | * If it is a device saving the time use UTC |
||
186 | * If it is a user saving the time use their preferred timezone |
||
187 | * |
||
188 | * @param string $value |
||
189 | * @return void |
||
190 | */ |
||
191 | View Code Duplication | public function setCloseTimeAttribute($value) |
|
204 | |||
205 | /** |
||
206 | * Accessor: Get the last time the server received and update call from the device converted to a |
||
207 | * user friendly format. The format is Month day 12hour:mins am/pm and will be in the user's preferred timezone |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getLastNetworkUpdateAtHumanAttribute() |
||
215 | |||
216 | /** |
||
217 | * Accessor: Get the last time the server received and update call from the device converted to a |
||
218 | * user friendly detailed format. The format is Month day, year 12hour:mins am/pm and will be in the user's preferred timezone |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getLastNetworkUpdateAtDetailedAttribute() |
||
226 | |||
227 | /** |
||
228 | * Accessor: Get the devices last update time converted to a |
||
229 | * user friendly format. The format is Month day, year 12hour:mins am/pm and will be in the user's preferred timezone |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getUpdatedAtHumanAttribute() |
||
237 | |||
238 | /** |
||
239 | * Accessor: Get the devices created at time converted to a |
||
240 | * user friendly format. The format is Month day, year 12hour:mins am/pm and will be in the user's preferred timezone |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getCreatedAtHumanAttribute() |
||
248 | |||
249 | /** |
||
250 | * Scope a query to only include devices belonging to a given location |
||
251 | * |
||
252 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
253 | * @param int $location_id |
||
254 | * @return \Illuminate\Database\Eloquent\Builder |
||
255 | */ |
||
256 | public function scopeByLocation($query, $location_id) |
||
260 | |||
261 | /** |
||
262 | * Scope a query to limit the included columns to only include what is publicly needed to be displayed on the |
||
263 | * dashboard |
||
264 | * |
||
265 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
266 | * @return \Illuminate\Database\Eloquent\Builder |
||
267 | */ |
||
268 | public function scopePublicDashData($query) |
||
285 | |||
286 | /** |
||
287 | * Create a new API token for the device. |
||
288 | */ |
||
289 | public function generateToken() |
||
296 | |||
297 | /** |
||
298 | * Get a device by uuid |
||
299 | * |
||
300 | * @param string $uuid |
||
301 | * @return Device|Illuminate\Database\Eloquent\Model |
||
302 | */ |
||
303 | public static function getDeviceByUUID($uuid) |
||
307 | |||
308 | /** |
||
309 | * Get the deviceimage record associated with the device. |
||
310 | */ |
||
311 | public function image() |
||
315 | |||
316 | |||
317 | /** |
||
318 | * Get the sensors associated with the device. |
||
319 | */ |
||
320 | public function sensors() |
||
324 | |||
325 | /** |
||
326 | * Get the sensor data associated with the device. |
||
327 | */ |
||
328 | public function data() |
||
332 | |||
333 | /** |
||
334 | * Check if the device is ready for a cover command |
||
335 | * |
||
336 | * @return boolean |
||
337 | */ |
||
338 | public function isReadyForCommand() |
||
342 | |||
343 | /** |
||
344 | * Check if the current time is during the devices scheduled time to be open |
||
345 | * |
||
346 | * @return boolean |
||
347 | */ |
||
348 | public function isDuringScheduleOpen() |
||
362 | |||
363 | /** |
||
364 | * Get the covers actual status based on the current command and the devices status |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function actualCoverStatus() |
||
404 | |||
405 | /** |
||
406 | * Get the page number of the device for the dashboard device table pagination |
||
407 | * |
||
408 | * @param int $limit |
||
409 | * @return int |
||
410 | */ |
||
411 | public function dashPageNum($limit) |
||
420 | } |
||
421 |
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: