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 | ||
| 121 | class Device extends Model | ||
| 122 | { | ||
| 123 | /** | ||
| 124 | * Indicates if the model should be timestamped. | ||
| 125 | * | ||
| 126 | * @var bool | ||
| 127 | */ | ||
| 128 | public $timestamps = false; | ||
| 129 | /** | ||
| 130 | * The table associated with the model. | ||
| 131 | * | ||
| 132 | * @var string | ||
| 133 | */ | ||
| 134 | protected $table = 'devices'; | ||
| 135 | /** | ||
| 136 | * The primary key column name. | ||
| 137 | * | ||
| 138 | * @var string | ||
| 139 | */ | ||
| 140 | protected $primaryKey = 'device_id'; | ||
| 141 | |||
| 142 | /** | ||
| 143 | * The attributes that are mass assignable. | ||
| 144 | * | ||
| 145 | * @var array | ||
| 146 | */ | ||
| 147 | protected $fillable = ['hostname', 'ip', 'status', 'status_reason']; | ||
| 148 | |||
| 149 | /** | ||
| 150 | * Initialize this class | ||
| 151 | */ | ||
| 152 | public static function boot() | ||
| 153 |     { | ||
| 154 | parent::boot(); | ||
| 155 | |||
| 156 |         static::deleting(function (Device $device) { | ||
| 157 | // delete related data | ||
| 158 | $device->ports()->delete(); | ||
| 159 | $device->syslogs()->delete(); | ||
| 160 | $device->eventlogs()->delete(); | ||
| 161 | }); | ||
| 162 | } | ||
| 163 | |||
| 164 | // ---- Helper Functions ---- | ||
| 165 | |||
| 166 | /** | ||
| 167 | * @return string | ||
| 168 | */ | ||
| 169 | public function logo() | ||
| 170 |     { | ||
| 171 | $icon = $this->icon; | ||
| 172 |         if (isset($icon)) { | ||
| 173 |             return asset('images/os/'.$icon.'.png'); | ||
| 174 |         } else { | ||
| 175 |             return asset('images/os/generic.png'); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @return string | ||
| 181 | */ | ||
| 182 | public function statusColour() | ||
| 183 |     { | ||
| 184 | $status = $this->status; | ||
| 185 | $ignore = $this->ignore; | ||
| 186 | $disabled = $this->disabled; | ||
| 187 |         if ($disabled == 1) { | ||
| 188 | return 'teal'; | ||
| 189 |         } elseif ($ignore == 1) { | ||
| 190 | return 'yellow'; | ||
| 191 |         } elseif ($status == 0) { | ||
| 192 | return 'danger'; | ||
| 193 |         } else { | ||
| 194 | return 'success'; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | // ---- Accessors/Mutators ---- | ||
| 199 | |||
| 200 | public function getIpAttribute($ip) | ||
| 201 |     { | ||
| 202 |         if (empty($ip)) { | ||
| 203 | return null; | ||
| 204 | } | ||
| 205 | // @ suppresses warning, inet_ntop() returns false if it fails | ||
| 206 | return @inet_ntop($ip) ?: null; | ||
| 207 | } | ||
| 208 | |||
| 209 | public function setIpAttribute($ip) | ||
| 210 |     { | ||
| 211 | $this->attributes['ip'] = inet_pton($ip); | ||
| 212 | } | ||
| 213 | |||
| 214 | // ---- Query scopes ---- | ||
| 215 | |||
| 216 | /** | ||
| 217 | * @param int $seconds | ||
| 218 | * @return string | ||
| 219 | */ | ||
| 220 | public function formatUptime($seconds) | ||
| 221 |     { | ||
| 222 |         if (empty($seconds)) { | ||
| 223 | $seconds = 0; | ||
| 224 | } | ||
| 225 |         $from = new \DateTime("@0"); | ||
| 226 |         $to = new \DateTime("@$seconds"); | ||
| 227 |         return $from->diff($to)->format('%a d, %h h, %i m and %s s'); | ||
| 228 | } | ||
| 229 | |||
| 230 | View Code Duplication | public function scopeIsUp($query) | |
|  | |||
| 231 |     { | ||
| 232 | return $query->where([ | ||
| 233 | ['status', '=', 1], | ||
| 234 | ['ignore', '=', 0], | ||
| 235 | ['disabled', '=', 0] | ||
| 236 | ]); | ||
| 237 | } | ||
| 238 | |||
| 239 | View Code Duplication | public function scopeIsDown($query) | |
| 240 |     { | ||
| 241 | return $query->where([ | ||
| 242 | ['status', '=', 0], | ||
| 243 | ['ignore', '=', 0], | ||
| 244 | ['disabled', '=', 0] | ||
| 245 | ]); | ||
| 246 | } | ||
| 247 | |||
| 248 | public function scopeIsIgnored($query) | ||
| 249 |     { | ||
| 250 | return $query->where([ | ||
| 251 | ['ignore', '=', 1], | ||
| 252 | ['disabled', '=', 0] | ||
| 253 | ]); | ||
| 254 | } | ||
| 255 | |||
| 256 | public function scopeNotIgnored($query) | ||
| 257 |     { | ||
| 258 | return $query->where([ | ||
| 259 | ['ignore', '=', 0] | ||
| 260 | ]); | ||
| 261 | } | ||
| 262 | |||
| 263 | public function scopeIsDisabled($query) | ||
| 264 |     { | ||
| 265 | return $query->where([ | ||
| 266 | ['disabled', '=', 1] | ||
| 267 | ]); | ||
| 268 | } | ||
| 269 | |||
| 270 | // ---- Define Relationships ---- | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Relationship to App\Models\Alerting\Alert | ||
| 274 | * | ||
| 275 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 276 | */ | ||
| 277 | public function alerts() | ||
| 278 |     { | ||
| 279 |         return $this->hasMany('App\Models\Alerting\Alert', 'device_id'); | ||
| 280 | } | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Relationship to App\Models\General\Eventlog | ||
| 284 | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||
| 285 | */ | ||
| 286 | public function eventlogs() | ||
| 287 |     { | ||
| 288 |         return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id'); | ||
| 289 | } | ||
| 290 | |||
| 291 | /** | ||
| 292 | * Relationship to App\Models\DeviceGroup | ||
| 293 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||
| 294 | */ | ||
| 295 | public function groups() | ||
| 296 |     { | ||
| 297 |         return $this->belongsToMany('App\Models\DeviceGroup', 'device_group_device', 'device_id', 'device_group_id'); | ||
| 298 | } | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Relationship to App\Models\Port | ||
| 302 | * Returns a list of the ports this device has. | ||
| 303 | */ | ||
| 304 | public function ports() | ||
| 305 |     { | ||
| 306 |         return $this->hasMany('App\Models\Port', 'device_id', 'device_id'); | ||
| 307 | } | ||
| 308 | |||
| 309 | /** | ||
| 310 | * Relationship to App\Models\Processor | ||
| 311 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 312 | */ | ||
| 313 | public function processors() | ||
| 314 |     { | ||
| 315 |         return $this->hasMany('App\Models\Processor', 'device_id'); | ||
| 316 | } | ||
| 317 | |||
| 318 | /** | ||
| 319 | * Relationship to App\Models\Alerting\Rule | ||
| 320 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 321 | */ | ||
| 322 | public function rules() | ||
| 326 | |||
| 327 | /** | ||
| 328 | * Relationship to App\Models\Sensor | ||
| 329 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 330 | */ | ||
| 331 | public function sensors() | ||
| 335 | |||
| 336 | /** | ||
| 337 | * Relationship to App\Models\Service | ||
| 338 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 339 | */ | ||
| 340 | public function services() | ||
| 341 |     { | ||
| 342 |         return $this->hasMany('App\Models\Service', 'device_id'); | ||
| 343 | } | ||
| 344 | |||
| 345 | /** | ||
| 346 | * Relationship to App\Models\Storage | ||
| 347 | * @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
| 348 | */ | ||
| 349 | public function storage() | ||
| 350 |     { | ||
| 351 |         return $this->hasMany('App\Models\Storage', 'device_id'); | ||
| 352 | } | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Relationship to App\Models\General\Syslog | ||
| 356 | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||
| 357 | */ | ||
| 358 | public function syslogs() | ||
| 362 | |||
| 363 | /** | ||
| 364 | * Relationship to App\Models\User | ||
| 365 | * Does not include users with global permissions. | ||
| 366 | * | ||
| 367 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||
| 368 | */ | ||
| 369 | public function users() | ||
| 373 | } | ||
| 374 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.