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 |
||
21 | class Album extends Model |
||
22 | { |
||
23 | use SupportsDeleteWhereIDsNotIn; |
||
24 | |||
25 | const UNKNOWN_ID = 1; |
||
26 | const UNKNOWN_NAME = 'Unknown Album'; |
||
27 | const UNKNOWN_COVER = 'unknown-album.png'; |
||
28 | |||
29 | protected $guarded = ['id']; |
||
30 | protected $hidden = ['updated_at']; |
||
31 | protected $casts = ['artist_id' => 'integer']; |
||
32 | protected $appends = ['is_compilation']; |
||
33 | |||
34 | public function artist() |
||
38 | |||
39 | public function songs() |
||
43 | |||
44 | public function getIsUnknownAttribute() |
||
48 | |||
49 | /** |
||
50 | * Get an album using some provided information. |
||
51 | * |
||
52 | * @param Artist $artist |
||
53 | * @param string $name |
||
54 | * @param bool $isCompilation |
||
55 | * |
||
56 | * @return self |
||
57 | */ |
||
58 | public static function get(Artist $artist, $name, $isCompilation = false) |
||
70 | |||
71 | /** |
||
72 | * Get extra information about the album from Last.fm. |
||
73 | * |
||
74 | * @return array|false |
||
75 | */ |
||
76 | public function getInfo() |
||
95 | |||
96 | /** |
||
97 | * Generate a cover from provided data. |
||
98 | * |
||
99 | * @param array $cover The cover data in array format, extracted by getID3. |
||
100 | * For example: |
||
101 | * [ |
||
102 | * 'data' => '<binary data>', |
||
103 | * 'image_mime' => 'image/png', |
||
104 | * 'image_width' => 512, |
||
105 | * 'image_height' => 512, |
||
106 | * 'imagetype' => 'PNG', // not always present |
||
107 | * 'picturetype' => 'Other', |
||
108 | * 'description' => '', |
||
109 | * 'datalength' => 7627, |
||
110 | * ] |
||
111 | */ |
||
112 | public function generateCover(array $cover) |
||
119 | |||
120 | /** |
||
121 | * Write a cover image file with binary data and update the Album with the new cover file. |
||
122 | * |
||
123 | * @param string $binaryData |
||
124 | * @param string $extension The file extension |
||
125 | */ |
||
126 | View Code Duplication | public function writeCoverFile($binaryData, $extension) |
|
134 | |||
135 | /** |
||
136 | * Copy a cover file from an existing image on the system. |
||
137 | * |
||
138 | * @param string $source The original image's full path. |
||
139 | */ |
||
140 | View Code Duplication | public function copyCoverFile($source) |
|
148 | |||
149 | /** |
||
150 | * Generate a random path for the cover image. |
||
151 | * |
||
152 | * @param string $extension The extension of the cover (without dot) |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | private function generateRandomCoverPath($extension) |
||
160 | |||
161 | public function setCoverAttribute($value) |
||
165 | |||
166 | public function getCoverAttribute($value) |
||
170 | |||
171 | /** |
||
172 | * Determine if the current album has a cover. |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function getHasCoverAttribute() |
||
180 | |||
181 | /** |
||
182 | * Sometimes the tags extracted from getID3 are HTML entity encoded. |
||
183 | * This makes sure they are always sane. |
||
184 | * |
||
185 | * @param $value |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getNameAttribute($value) |
||
193 | |||
194 | /** |
||
195 | * Determine if the album is a compilation. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function getIsCompilationAttribute() |
||
203 | } |
||
204 |
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.