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 |
||
| 9 | class Location extends Model |
||
| 10 | { |
||
| 11 | use LogsActivity; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The attributes that are mass assignable. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $fillable = [ |
||
| 19 | 'name', 'site_id' |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The attributes to ignore in the Activity Log |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected static $ignoreChangedAttributes = ['updated_at']; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The attributes to log in the Activity Log |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected static $logAttributes = [ |
||
| 35 | 'name', 'site_id' |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Only log those that have actually changed after the update. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $logOnlyDirty = true; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Update the updated_at and created_at timestamps? |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | public $timestamps = true; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the site for the location |
||
| 54 | */ |
||
| 55 | public function site() |
||
| 56 | { |
||
| 57 | return $this->belongsTo('App\Site', 'site_id'); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get devices for the location |
||
| 62 | */ |
||
| 63 | public function devices() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Scope a query to only include locations belonging to a given site |
||
| 70 | * |
||
| 71 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 72 | * @param int $site_id |
||
| 73 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 74 | */ |
||
| 75 | public function scopeBySite($query, $site_id) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Accessor: Get the location's last update time in seconds/minutes/hours since update or converted to user |
||
| 82 | * friendly readable format. |
||
| 83 | * If the time is less then a day old then display time since it last updated |
||
| 84 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 85 | * and using the user's preferred timezone |
||
| 86 | * |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function getUpdatedAtHumanAttribute() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Accessor: Get the location's creation time in seconds/minutes/hours since update or converted to user |
||
| 100 | * friendly readable format. |
||
| 101 | * If the time is less then a day old then display time since creation |
||
| 102 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 103 | * and using the user's preferred timezone |
||
| 104 | * |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function getCreatedAtHumanAttribute() |
|
| 115 | } |
||
| 116 |
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: