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:
Complex classes like Device often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Device, and based on these observations, apply Extract Interface, too.
| 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', 'update_rate', |
||
| 42 | 'image_rate', 'sensor_rate', 'open_time', 'close_time', 'last_network_update_at', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The attributes to ignore in the Activity Log |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected static $ignoreChangedAttributes = ['updated_at', 'last_network_update_at']; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The attributes to log in the Activity Log |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $logAttributes = [ |
||
| 58 | 'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', |
||
| 59 | 'time', 'cover_command', 'cover_status', 'error_msg', |
||
| 60 | 'update_rate', 'image_rate', 'sensor_rate', 'open_time', 'close_time' |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Only log those that have actually changed after the update. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected static $logOnlyDirty = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Update the updated_at and created_at timestamps? |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public $timestamps = true; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the location for the device |
||
| 79 | */ |
||
| 80 | public function location() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the site for the device using model accessor |
||
| 87 | */ |
||
| 88 | public function getSiteAttribute() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Accessor: Get the open time of the device converted to hours and minutes |
||
| 95 | * If it is a device accessing the time use UTC |
||
| 96 | * If it is a user accessing the time use their preferred timezone |
||
| 97 | * |
||
| 98 | * @param string $value |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function getOpenTimeAttribute($value) |
|
| 102 | { |
||
| 103 | $time = new Carbon($value, 'UTC'); |
||
| 104 | |||
| 105 | //If the user is logged in then use there preferred timezone |
||
| 106 | if (Auth::check()) |
||
| 107 | $time = $time->setTimezone(Auth::user()->timezone); |
||
|
|
|||
| 108 | |||
| 109 | return $time->format('H:i'); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Accessor: Get the open time of the device converted to the users preferred time and |
||
| 114 | * converted to a user friendly format of h:i a |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function getOpenTimeHumanAttribute() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Accessor: Get the close time of the device converted to hours and minutes |
||
| 128 | * If it is a device accessing the time use UTC |
||
| 129 | * If it is a user accessing the time use their preferred timezone |
||
| 130 | * |
||
| 131 | * @param string $value |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function getCloseTimeAttribute($value) |
|
| 135 | { |
||
| 136 | $time = new Carbon($value, 'UTC'); |
||
| 137 | |||
| 138 | //If the user is logged in then use there preferred timezone |
||
| 139 | if (Auth::check()) |
||
| 140 | $time = $time->setTimezone(Auth::user()->timezone); |
||
| 141 | |||
| 142 | return $time->format('H:i'); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Accessor: Get the close time of the device converted to the users preferred time and |
||
| 147 | * converted to a user friendly format of h:i a |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function getCloseTimeHumanAttribute() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Set the open time to UTC |
||
| 161 | * If it is a device saving the time use UTC |
||
| 162 | * If it is a user saving the time use their preferred timezone |
||
| 163 | * |
||
| 164 | * @param string $value |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function setOpenTimeAttribute($value) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Set the close time to UTC |
||
| 183 | * If it is a device saving the time use UTC |
||
| 184 | * If it is a user saving the time use their preferred timezone |
||
| 185 | * |
||
| 186 | * @param string $value |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function setCloseTimeAttribute($value) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Accessor: Get the last time the server received an update call from the device in seconds/minutes/hours since |
||
| 205 | * update or converted to user friendly readable format. |
||
| 206 | * If the time is less then a day old then display time since it last updated |
||
| 207 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 208 | * and using the user's preferred timezone |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getLastNetworkUpdateAtHumanAttribute() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Accessor: Get the devices last update time in seconds/minutes/hours since update or converted to user friendly |
||
| 222 | * readable format. |
||
| 223 | * If the time is less then a day old then display time since it last update |
||
| 224 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 225 | * and using the user's preferred timezone |
||
| 226 | * |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function getUpdatedAtHumanAttribute() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Accessor: Get the devices creation time in seconds/minutes/hours since creation or converted to user friendly |
||
| 240 | * readable format. |
||
| 241 | * If the time is less then a day old then display time since its creation |
||
| 242 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 243 | * and using the user's preferred timezone |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function getCreatedAtHumanAttribute() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Scope a query to only include devices belonging to a given location |
||
| 257 | * |
||
| 258 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 259 | * @param int $location_id |
||
| 260 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 261 | */ |
||
| 262 | public function scopeByLocation($query, $location_id) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Scope a query to limit the included columns to only include what is publicly needed to be displayed on the |
||
| 269 | * dashboard |
||
| 270 | * |
||
| 271 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 272 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 273 | */ |
||
| 274 | public function scopePublicDashData($query) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Create a new API token for the device. |
||
| 294 | */ |
||
| 295 | public function generateToken() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get a device by uuid |
||
| 305 | * |
||
| 306 | * @param string $uuid |
||
| 307 | * @return Device|Illuminate\Database\Eloquent\Model |
||
| 308 | */ |
||
| 309 | public static function getDeviceByUUID($uuid) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the deviceimage record associated with the device. |
||
| 316 | */ |
||
| 317 | public function image() |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the sensors associated with the device. |
||
| 325 | */ |
||
| 326 | public function sensors() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the sensor data associated with the device. |
||
| 333 | */ |
||
| 334 | public function data() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Check if the device is ready for a cover command |
||
| 341 | * |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | public function isReadyForCommand() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Check if the current time is during the devices scheduled time to be open |
||
| 351 | * |
||
| 352 | * @return boolean |
||
| 353 | */ |
||
| 354 | public function isDuringScheduleOpen() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the covers actual status based on the current command and the devices status |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function actualCoverStatus() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the page number of the device for the dashboard device table pagination |
||
| 413 | * |
||
| 414 | * @param int $limit |
||
| 415 | * @return int |
||
| 416 | */ |
||
| 417 | public function dashPageNum($limit) |
||
| 426 | } |
||
| 427 |
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: