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 |
||
7 | class Document extends Model |
||
8 | { |
||
9 | /** |
||
10 | * The attributes that are mass assignable. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $fillable = ['bibsys_id', 'bibliographic', 'holdings']; |
||
15 | |||
16 | /** |
||
17 | * The attributes that should be casted to native types. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $casts = [ |
||
22 | 'id' => 'integer', |
||
23 | 'bibliographic' => 'array', |
||
24 | 'holdings' => 'array', |
||
25 | 'xisbn' => 'array', |
||
26 | 'description' => 'array', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Get subjects associated with this document. |
||
31 | */ |
||
32 | public function subjects() |
||
36 | |||
37 | /** |
||
38 | * Get genres associated with this document. |
||
39 | */ |
||
40 | public function genres() |
||
44 | |||
45 | /** |
||
46 | * The cover belonging to the document. |
||
47 | */ |
||
48 | public function cover() |
||
52 | |||
53 | /** |
||
54 | * Get enrichments associated with this document. |
||
55 | */ |
||
56 | public function enrichments() |
||
60 | |||
61 | /** |
||
62 | * Get enrichments associated with this document. |
||
63 | */ |
||
64 | public function enrichmentsByService($serviceName) |
||
68 | |||
69 | /** |
||
70 | * The collections the document belongs to. |
||
71 | */ |
||
72 | public function collections() |
||
76 | |||
77 | View Code Duplication | public function storeCover($url) |
|
89 | |||
90 | View Code Duplication | public function storeCoverFromBlob($blob) |
|
102 | |||
103 | public function setCannotFindCover() |
||
111 | |||
112 | public function isElectronic() |
||
116 | } |
||
117 |
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.