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 namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures; |
||
12 | View Code Duplication | class AuthorWithInlineGlobalScope extends Model |
|
|
|||
13 | { |
||
14 | use Cachable; |
||
15 | use SoftDeletes; |
||
16 | |||
17 | protected $casts = [ |
||
18 | "finances" => "array", |
||
19 | ]; |
||
20 | protected $fillable = [ |
||
21 | 'name', |
||
22 | 'email', |
||
23 | "finances", |
||
24 | ]; |
||
25 | protected $table = "authors"; |
||
26 | |||
27 | protected static function boot() |
||
35 | |||
36 | public function books() : HasMany |
||
40 | |||
41 | public function printers() : HasManyThrough |
||
45 | |||
46 | public function profile() : HasOne |
||
50 | |||
51 | public function getLatestBookAttribute() |
||
58 | |||
59 | public function scopeStartsWithA(Builder $query) : Builder |
||
63 | |||
64 | public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder |
||
68 | } |
||
69 |
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.