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:
Complex classes like Content often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Content, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Content extends ApiController |
||
22 | { |
||
23 | public $maxSize = 512000; // in bytes, 500 * 1024 |
||
24 | public $maxResize = 150; |
||
25 | |||
26 | public $allowedExt = ['jpg', 'png', 'gif', 'jpeg', 'bmp', 'webp']; |
||
27 | |||
28 | /** |
||
29 | * Prepare configuratins before initialization |
||
30 | */ |
||
31 | public function before() |
||
44 | |||
45 | /** |
||
46 | * Change content item rating action |
||
47 | * @param string $type |
||
48 | * @param int $id |
||
49 | * @throws NativeException |
||
50 | * @throws ForbiddenException |
||
51 | * @throws NotFoundException |
||
52 | * @return string |
||
53 | */ |
||
54 | public function actionChangerate($type, $id) |
||
98 | |||
99 | /** |
||
100 | * Upload new files to content item gallery |
||
101 | * @param int $id |
||
102 | * @return string |
||
103 | * @throws ForbiddenException |
||
104 | * @throws NativeException |
||
105 | * @throws \Exception |
||
106 | */ |
||
107 | public function actionGalleryupload($id) |
||
108 | { |
||
109 | // check if id is passed |
||
110 | if (Str::likeEmpty($id)) { |
||
111 | throw new NativeException('Wrong input data'); |
||
112 | } |
||
113 | |||
114 | // check if user have permission to access there |
||
115 | View Code Duplication | if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('global/file')) { |
|
116 | throw new NativeException(__('Permissions to upload is denied')); |
||
117 | } |
||
118 | |||
119 | // check if directory exist |
||
120 | if (!Directory::exist('/upload/gallery/' . $id)) { |
||
121 | Directory::create('/upload/gallery/' . $id); |
||
122 | } |
||
123 | |||
124 | // get file object |
||
125 | /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */ |
||
126 | $file = $this->request->files->get('file'); |
||
127 | if ($file === null || $file->getError() !== 0) { |
||
128 | throw new NativeException(__('Unexpected error in upload process')); |
||
129 | } |
||
130 | |||
131 | // check file size |
||
132 | if ($file->getSize() < 1 || $file->getSize() > $this->maxSize) { |
||
133 | throw new ForbiddenException(__('File size is too big. Max size: %size%kb', ['size' => (int)($this->maxSize/1024)])); |
||
134 | } |
||
135 | |||
136 | // check file extension |
||
137 | if (!Arr::in($file->guessExtension(), $this->allowedExt)) { |
||
138 | throw new ForbiddenException(__('File extension is not allowed to upload. Allowed: %s%', ['s' => implode(', ', $this->allowedExt)])); |
||
139 | } |
||
140 | |||
141 | // create origin directory |
||
142 | $originPath = '/upload/gallery/' . $id . '/orig/'; |
||
143 | if (!Directory::exist($originPath)) { |
||
144 | Directory::create($originPath); |
||
145 | } |
||
146 | |||
147 | // lets make a new file name |
||
148 | $fileName = App::$Security->simpleHash($file->getClientOriginalName() . $file->getSize()); |
||
149 | $fileNewName = $fileName . '.' . $file->guessExtension(); |
||
150 | // check if image is already loaded |
||
151 | if (File::exist($originPath . $fileNewName)) { |
||
152 | throw new ForbiddenException(__('File is always exists!')); |
||
153 | } |
||
154 | // save file from tmp to gallery origin directory |
||
155 | $file->move(Normalize::diskFullPath($originPath), $fileNewName); |
||
156 | |||
157 | // lets resize preview image for it |
||
158 | $thumbPath = '/upload/gallery/' . $id . '/thumb/'; |
||
159 | if (!Directory::exist($thumbPath)) { |
||
160 | Directory::create($thumbPath); |
||
161 | } |
||
162 | |||
163 | $thumb = new Image(); |
||
164 | $thumb->setCacheDir(root . '/Private/Cache/images'); |
||
165 | |||
166 | // open original file, resize it and save |
||
167 | $thumbSaveName = Normalize::diskFullPath($thumbPath) . '/' . $fileName . '.jpg'; |
||
168 | $thumb->open(Normalize::diskFullPath($originPath) . DIRECTORY_SEPARATOR . $fileNewName) |
||
169 | ->cropResize($this->maxResize) |
||
170 | ->save($thumbSaveName, 'jpg', 90); |
||
171 | $thumb = null; |
||
172 | |||
173 | $this->setJsonHeader(); |
||
174 | return json_encode(['status' => 1, 'file' => [ |
||
175 | 'thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', |
||
176 | 'url' => '/upload/gallery/' . $id . '/orig/' . $fileNewName, |
||
177 | 'name' => $fileNewName |
||
178 | ]]); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Show gallery images from upload directory |
||
183 | * @param int $id |
||
184 | * @return string |
||
185 | * @throws NotFoundException |
||
186 | * @throws NativeException |
||
187 | */ |
||
188 | public function actionGallerylist($id) |
||
189 | { |
||
190 | // check if id is passed |
||
191 | if (Str::likeEmpty($id)) { |
||
192 | throw new NativeException('Wrong input data'); |
||
193 | } |
||
194 | |||
195 | // check if user have permission to access there |
||
196 | View Code Duplication | if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('global/file')) { |
|
197 | throw new NativeException('Permission denied'); |
||
198 | } |
||
199 | |||
200 | $thumbDir = Normalize::diskFullPath('/upload/gallery/' . $id . '/orig/'); |
||
201 | if (!Directory::exist($thumbDir)) { |
||
202 | throw new NotFoundException('Nothing found'); |
||
203 | } |
||
204 | |||
205 | $files = Directory::scan($thumbDir, null, true); |
||
206 | if ($files === false || !Obj::isArray($files) || count($files) < 1) { |
||
207 | throw new NotFoundException('Nothing found'); |
||
208 | } |
||
209 | |||
210 | $output = []; |
||
211 | foreach ($files as $file) { |
||
212 | $fileExt = Str::lastIn($file, '.'); |
||
213 | $fileName = Str::sub($file, 0, -Str::length($fileExt)); |
||
214 | $output[] = [ |
||
215 | 'thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', |
||
216 | 'url' => '/upload/gallery/' . $id . '/orig/' . $file, |
||
217 | 'name' => $file, |
||
218 | 'size' => File::size($file) |
||
219 | ]; |
||
220 | } |
||
221 | |||
222 | $this->setJsonHeader(); |
||
223 | return json_encode(['status' => 1, 'files' => $output]); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Remove items from gallery (preview+full) |
||
228 | * @param int $id |
||
229 | * @param string $file |
||
230 | * @throws ForbiddenException |
||
231 | * @throws NativeException |
||
232 | * @return string |
||
233 | */ |
||
234 | public function actionGallerydelete($id, $file) |
||
262 | } |
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.