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 |
||
10 | class ImageProcessor |
||
11 | { |
||
12 | const MIME_JPEG = 'image/jpeg'; |
||
13 | const MIME_JPG = 'image/jpg'; |
||
14 | const MIME_PNG = 'image/png'; |
||
15 | const MIME_GIF = 'image/gif'; |
||
16 | |||
17 | protected $location = 'upload/images'; |
||
18 | |||
19 | protected $filename; |
||
20 | |||
21 | /** |
||
22 | * @return Image |
||
23 | */ |
||
24 | public function getModel() |
||
28 | |||
29 | /** |
||
30 | * Upload and create image for imageable instance. |
||
31 | * |
||
32 | * @param $image |
||
33 | * @param $imageable |
||
34 | * @param null $data |
||
35 | * @param null $location |
||
36 | * @return static |
||
37 | * @throws Exception |
||
38 | */ |
||
39 | public function uploadAndCreate($image, $imageable, $data = null, $location = null) |
||
54 | |||
55 | /** |
||
56 | * Download image to server and get as UploadedFile object. |
||
57 | * |
||
58 | * @param $url |
||
59 | * @param $imageable |
||
60 | * @param array|null $data |
||
61 | * @param string $location |
||
62 | * @return static |
||
63 | */ |
||
64 | public function downloadAndUpload($url, $imageable, array $data = null, $location = 'upload/images/') |
||
81 | |||
82 | /** |
||
83 | * Validate incoming image. |
||
84 | * |
||
85 | * @param $image |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function validateImage($image) |
||
92 | |||
93 | /** |
||
94 | * Render image name. |
||
95 | * |
||
96 | * @param $image |
||
97 | * @return string |
||
98 | */ |
||
99 | private function getNewImageName($image) |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getLocation() |
||
114 | |||
115 | /** |
||
116 | * @param Image $image |
||
117 | */ |
||
118 | View Code Duplication | public function destroy($image) |
|
127 | |||
128 | /** |
||
129 | * Delete only image form folder. |
||
130 | * |
||
131 | * @param $image |
||
132 | * @return bool |
||
133 | */ |
||
134 | View Code Duplication | public function destroyImageOnly($image) |
|
143 | |||
144 | /** |
||
145 | * Set location. |
||
146 | * |
||
147 | * @param $location |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function setLocation($location) |
||
156 | |||
157 | /** |
||
158 | * @param $location |
||
159 | * @throws Exception |
||
160 | */ |
||
161 | protected function validateLocation($location) |
||
169 | |||
170 | protected function upload($image) |
||
188 | |||
189 | /** |
||
190 | * Change avatar. |
||
191 | * |
||
192 | * @param $avatar |
||
193 | * @param string $location |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function changeAvatar($avatar, $location = 'upload/images/user_avatars/') |
||
211 | |||
212 | /** |
||
213 | * Standart pattern. |
||
214 | * |
||
215 | * @param $location |
||
216 | * @param $filename |
||
217 | * @param $extension |
||
218 | * @return string |
||
219 | */ |
||
220 | private function pattern($filename, $extension, $location = null) |
||
227 | |||
228 | /** |
||
229 | * Parse extension. |
||
230 | * |
||
231 | * @param $mime |
||
232 | * @return string |
||
233 | */ |
||
234 | private function parseExtension($mime) |
||
255 | |||
256 | private function create($path, $imageable, array $data = null) |
||
266 | } |
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.