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 | * @param string $key |
||
44 | * @param mixed $value |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | View Code Duplication | public function setAttribute($key, $value) |
|
56 | |||
57 | public function getPublishDateAttribute($value) |
||
61 | |||
62 | public function getExpirationDateAttribute($value) |
||
66 | |||
67 | /** |
||
68 | * Email the news on the publish_date when a news article is created or updated. |
||
69 | */ |
||
70 | public function emailNews() |
||
79 | } |
||
80 |