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 |
||
| 11 | class News extends Model |
||
| 12 | { |
||
| 13 | use DateFormat; |
||
| 14 | |||
| 15 | protected $table = 'news'; |
||
| 16 | public $timestamps = true; |
||
| 17 | |||
| 18 | protected $fillable = ['title', 'description', 'publish_date', 'expire_date', 'author_id', 'send_email']; |
||
| 19 | |||
| 20 | public function attachments() |
||
| 24 | |||
| 25 | public function author() |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param $query |
||
| 32 | */ |
||
| 33 | public function scopePublishedNews($query) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Store empty values as null in the DB. |
||
| 44 | * |
||
| 45 | * @param string $key |
||
| 46 | * @param mixed $value |
||
| 47 | * |
||
| 48 | * @return $this |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function setAttribute($key, $value) |
|
| 58 | |||
| 59 | public function getPublishDateAttribute() |
||
| 63 | |||
| 64 | public function getExpirationDateAttribute() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Email the news on the publish_date when a news article is created or updated. |
||
| 71 | */ |
||
| 72 | public function emailNews() |
||
| 81 | } |
||
| 82 |