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 Site 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' |
||
| 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' |
||
| 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 locations for the site. |
||
| 54 | */ |
||
| 55 | public function locations() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the devices for the site. |
||
| 62 | */ |
||
| 63 | public function devices() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Accessor: Get the site's last update time in seconds/minutes/hours since update or converted to user |
||
| 70 | * friendly readable format. |
||
| 71 | * If the time is less then a day old then display time since it last updated |
||
| 72 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 73 | * and using the user's preferred timezone |
||
| 74 | * |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function getUpdatedAtHumanAttribute() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Accessor: Get the site's creation time in seconds/minutes/hours since update or converted to user |
||
| 88 | * friendly readable format. |
||
| 89 | * If the time is less then a day old then display time since creation |
||
| 90 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 91 | * and using the user's preferred timezone |
||
| 92 | * |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function getCreatedAtHumanAttribute() |
|
| 103 | } |
||
| 104 |
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: