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 Sensor extends Model |
||
| 10 | { |
||
| 11 | use LogsActivity; |
||
| 12 | |||
| 13 | protected $table = 'sensors'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Update the updated_at and created_at timestamps? |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | public $timestamps = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The attributes that are mass assignable. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $fillable = array('device_id', 'name', 'type'); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The attributes that are visible. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $visible = array('id', 'device_id', 'name', 'type'); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The attributes to log in the Activity Log |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected static $logAttributes = array('id', 'device_id', 'name', 'type'); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Only log those that have actually changed after the update. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected static $logOnlyDirty = true; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the device associated with the sensor. |
||
| 52 | */ |
||
| 53 | public function device() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the sensor data associated with the sensor. |
||
| 60 | */ |
||
| 61 | public function data() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the latest sensor data entry associated with the sensor. |
||
| 68 | */ |
||
| 69 | public function getLatestDataAttribute() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the last hour's sensor data averaged by the minute for the sensor. |
||
| 78 | */ |
||
| 79 | public function getLastHourMinutelyAvgDataAttribute() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the last day's sensor data averaged hourly for the sensor. |
||
| 90 | */ |
||
| 91 | public function getLastDayHourlyAvgDataAttribute() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the last weeks's sensor data averaged daily for the sensor. |
||
| 102 | */ |
||
| 103 | public function getLastWeekDailyAvgDataAttribute() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the last months's sensor data averaged daily for the sensor. |
||
| 114 | */ |
||
| 115 | public function getLastMonthDailyAvgDataAttribute() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the last year's sensor data averaged monthly for the sensor. |
||
| 126 | */ |
||
| 127 | public function getLastYearMonthlyAvgDataAttribute() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Accessor: Get the sensor's last update time in seconds/minutes/hours since update or converted to user |
||
| 138 | * friendly readable format. |
||
| 139 | * If the time is less then a day old then display time since it last updated |
||
| 140 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 141 | * and using the user's preferred timezone |
||
| 142 | * |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function getUpdatedAtHumanAttribute() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Accessor: Get the sensor's creation time in seconds/minutes/hours since update or converted to user |
||
| 156 | * friendly readable format. |
||
| 157 | * If the time is less then a day old then display time since creation |
||
| 158 | * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm |
||
| 159 | * and using the user's preferred timezone |
||
| 160 | * |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function getCreatedAtHumanAttribute() |
|
| 171 | } |
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: