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']; |
||
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 close time of the device converted to hours and minutes |
||
116 | * If it is a device accessing the time use UTC |
||
117 | * If it is a user accessing the time use their preferred timezone |
||
118 | * |
||
119 | * @param string $value |
||
120 | * @return string |
||
121 | */ |
||
122 | View Code Duplication | public function getCloseTimeAttribute($value) |
|
132 | |||
133 | /** |
||
134 | * Set the open time to UTC |
||
135 | * If it is a device saving the time use UTC |
||
136 | * If it is a user saving the time use their preferred timezone |
||
137 | * |
||
138 | * @param string $value |
||
139 | * @return void |
||
140 | */ |
||
141 | View Code Duplication | public function setOpenTimeAttribute($value) |
|
154 | |||
155 | /** |
||
156 | * Set the close time to UTC |
||
157 | * If it is a device saving the time use UTC |
||
158 | * If it is a user saving the time use their preferred timezone |
||
159 | * |
||
160 | * @param string $value |
||
161 | * @return void |
||
162 | */ |
||
163 | View Code Duplication | public function setCloseTimeAttribute($value) |
|
176 | |||
177 | /** |
||
178 | * Accessor: Get the last time the server received and update call from the device converted to a |
||
179 | * user friendly format. The format is Month day 12hour:mins am/pm and will be in the user's preferred timezone |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getLastNetworkUpdateAtHumanAttribute() |
||
187 | |||
188 | /** |
||
189 | * Scope a query to only include devices belonging to a given location |
||
190 | * |
||
191 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
192 | * @param int $location_id |
||
193 | * @return \Illuminate\Database\Eloquent\Builder |
||
194 | */ |
||
195 | public function scopeByLocation($query, $location_id) |
||
199 | |||
200 | /** |
||
201 | * Scope a query to limit the included columns to only include what is publicly needed to be displayed on the |
||
202 | * dashboard |
||
203 | * |
||
204 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
205 | * @return \Illuminate\Database\Eloquent\Builder |
||
206 | */ |
||
207 | public function scopePublicDashData($query) |
||
224 | |||
225 | /** |
||
226 | * Create a new API token for the device. |
||
227 | */ |
||
228 | public function generateToken() |
||
235 | |||
236 | /** |
||
237 | * Get a device by uuid |
||
238 | * |
||
239 | * @param string $uuid |
||
240 | * @return Device|Illuminate\Database\Eloquent\Model |
||
241 | */ |
||
242 | public static function getDeviceByUUID($uuid) |
||
246 | |||
247 | /** |
||
248 | * Get the deviceimage record associated with the device. |
||
249 | */ |
||
250 | public function image() |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Get the sensors associated with the device. |
||
258 | */ |
||
259 | public function sensors() |
||
263 | |||
264 | /** |
||
265 | * Get the sensor data associated with the device. |
||
266 | */ |
||
267 | public function data() |
||
271 | |||
272 | /** |
||
273 | * Check if the device is ready for a cover command |
||
274 | * |
||
275 | * @return boolean |
||
276 | */ |
||
277 | public function isReadyForCommand() |
||
281 | |||
282 | /** |
||
283 | * Check if the current time is during the devices scheduled time to be open |
||
284 | * |
||
285 | * @return boolean |
||
286 | */ |
||
287 | public function isDuringScheduleOpen() |
||
301 | |||
302 | /** |
||
303 | * Get the covers actual status based on the current command and the devices status |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function actualCoverStatus() |
||
339 | |||
340 | /** |
||
341 | * Get the page number of the device for the dashboard device table pagination |
||
342 | * |
||
343 | * @param int $limit |
||
344 | * @return int |
||
345 | */ |
||
346 | public function dashPageNum($limit) |
||
355 | } |
||
356 |
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: