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 |
||
8 | class Site extends Model |
||
9 | { |
||
10 | /** |
||
11 | * The attributes that are mass assignable. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $fillable = [ |
||
16 | 'name' |
||
17 | ]; |
||
18 | |||
19 | public $timestamps = false; |
||
20 | |||
21 | /** |
||
22 | * Get the locations for the site. |
||
23 | */ |
||
24 | public function locations() |
||
28 | |||
29 | /** |
||
30 | * Get the devices for the site. |
||
31 | */ |
||
32 | public function devices() |
||
36 | |||
37 | /** |
||
38 | * Get all the sites starting with the site with the id supplied and the rest sorted by their id in descending order |
||
39 | * |
||
40 | * @param int $id |
||
41 | * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
||
42 | */ |
||
43 | View Code Duplication | public function orderedSitesBy($id) |
|
50 | |||
51 | /** |
||
52 | * Create a new site and return its id |
||
53 | * |
||
54 | * @param string $name |
||
55 | * @return int $id |
||
56 | */ |
||
57 | public function createSite($name) |
||
65 | } |
||
66 |
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.