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 MessagesController 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 MessagesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class MessagesController extends Controller { |
||
54 | |||
55 | |||
56 | /** @var AccountService */ |
||
57 | private $accountService; |
||
58 | |||
59 | /** @var string */ |
||
60 | private $currentUserId; |
||
61 | |||
62 | /** @var ContactsIntegration */ |
||
63 | private $contactsIntegration; |
||
64 | |||
65 | /** @var Logger */ |
||
66 | private $logger; |
||
67 | |||
68 | /** @var Folder */ |
||
69 | private $userFolder; |
||
70 | |||
71 | /** @var IMimeTypeDetector */ |
||
72 | private $mimeTypeDetector; |
||
73 | |||
74 | /** @var IL10N */ |
||
75 | private $l10n; |
||
76 | |||
77 | /** @var IURLGenerator */ |
||
78 | private $urlGenerator; |
||
79 | |||
80 | /** @var IAccount[] */ |
||
81 | private $accounts = []; |
||
82 | |||
83 | /** |
||
84 | * @param string $appName |
||
85 | * @param IRequest $request |
||
86 | * @param AccountService $accountService |
||
87 | * @param string $UserId |
||
88 | * @param $userFolder |
||
89 | 12 | * @param ContactsIntegration $contactsIntegration |
|
90 | * @param Logger $logger |
||
91 | * @param IL10N $l10n |
||
92 | * @param IMimeTypeDetector $mimeTypeDetector |
||
93 | * @param IURLGenerator $urlGenerator |
||
94 | */ |
||
95 | View Code Duplication | public function __construct($appName, |
|
|
|||
96 | IRequest $request, |
||
97 | AccountService $accountService, |
||
98 | 12 | $UserId, |
|
99 | 12 | $userFolder, |
|
100 | 12 | ContactsIntegration $contactsIntegration, |
|
101 | 12 | Logger $logger, |
|
102 | 12 | IL10N $l10n, |
|
103 | 12 | IMimeTypeDetector $mimeTypeDetector, |
|
104 | 12 | IURLGenerator $urlGenerator) { |
|
105 | 12 | parent::__construct($appName, $request); |
|
106 | 12 | $this->accountService = $accountService; |
|
107 | $this->currentUserId = $UserId; |
||
108 | $this->userFolder = $userFolder; |
||
109 | $this->contactsIntegration = $contactsIntegration; |
||
110 | $this->logger = $logger; |
||
111 | $this->l10n = $l10n; |
||
112 | $this->mimeTypeDetector = $mimeTypeDetector; |
||
113 | $this->urlGenerator = $urlGenerator; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @NoAdminRequired |
||
118 | * @NoCSRFRequired |
||
119 | * |
||
120 | * @param int $accountId |
||
121 | * @param string $folderId |
||
122 | * @param int $from |
||
123 | * @param int $to |
||
124 | * @param string $filter |
||
125 | * @param array $ids |
||
126 | * @return JSONResponse |
||
127 | */ |
||
128 | public function index($accountId, $folderId, $from=0, $to=20, $filter=null, $ids=null) { |
||
160 | |||
161 | private function loadMessage($accountId, $folderId, $id) { |
||
184 | |||
185 | /** |
||
186 | * @NoAdminRequired |
||
187 | * @NoCSRFRequired |
||
188 | * |
||
189 | * @param int $accountId |
||
190 | * @param string $folderId |
||
191 | * @param mixed $id |
||
192 | * @return JSONResponse |
||
193 | */ |
||
194 | public function show($accountId, $folderId, $id) { |
||
202 | |||
203 | /** |
||
204 | 1 | * @NoAdminRequired |
|
205 | * @NoCSRFRequired |
||
206 | 1 | * |
|
207 | * @param int $accountId |
||
208 | 1 | * @param string $folderId |
|
209 | * @param string $messageId |
||
210 | * @return HtmlResponse|TemplateResponse |
||
211 | */ |
||
212 | public function getHtmlBody($accountId, $folderId, $messageId) { |
||
213 | try { |
||
214 | $mailBox = $this->getFolder($accountId, $folderId); |
||
215 | |||
216 | $m = $mailBox->getMessage($messageId, true); |
||
217 | $html = $m->getHtmlBody($accountId, $folderId, $messageId, function($cid) use ($m){ |
||
218 | 1 | $match = array_filter($m->attachments, function($a) use($cid){ |
|
219 | return $a['cid'] === $cid; |
||
220 | 1 | }); |
|
221 | $match = array_shift($match); |
||
222 | if (is_null($match)) { |
||
223 | 1 | return null; |
|
224 | 1 | } |
|
225 | 1 | return $match['id']; |
|
226 | 1 | }); |
|
227 | 1 | ||
228 | 1 | $htmlResponse = new HtmlResponse($html); |
|
229 | 1 | ||
230 | // Harden the default security policy |
||
231 | $policy = new ContentSecurityPolicy(); |
||
232 | 1 | $policy->allowEvalScript(false); |
|
233 | 1 | $policy->disallowScriptDomain('\'self\''); |
|
234 | $policy->disallowConnectDomain('\'self\''); |
||
235 | 1 | $policy->disallowFontDomain('\'self\''); |
|
236 | $policy->disallowMediaDomain('\'self\''); |
||
237 | $htmlResponse->setContentSecurityPolicy($policy); |
||
238 | |||
239 | // Enable caching |
||
240 | $htmlResponse->cacheFor(60 * 60); |
||
241 | $htmlResponse->addHeader('Pragma', 'cache'); |
||
242 | |||
243 | return $htmlResponse; |
||
244 | } catch(\Exception $ex) { |
||
245 | return new TemplateResponse($this->appName, 'error', ['message' => $ex->getMessage()], 'none'); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @NoAdminRequired |
||
251 | 1 | * @NoCSRFRequired |
|
252 | 1 | * |
|
253 | * @param int $accountId |
||
254 | 1 | * @param string $folderId |
|
255 | * @param string $messageId |
||
256 | 1 | * @param string $attachmentId |
|
257 | 1 | * @return AttachmentDownloadResponse |
|
258 | 1 | */ |
|
259 | 1 | public function downloadAttachment($accountId, $folderId, $messageId, $attachmentId) { |
|
269 | |||
270 | /** |
||
271 | * @NoAdminRequired |
||
272 | * @NoCSRFRequired |
||
273 | 2 | * |
|
274 | 2 | * @param int $accountId |
|
275 | * @param string $folderId |
||
276 | 2 | * @param string $messageId |
|
277 | 2 | * @param int $attachmentId |
|
278 | * @param string $targetPath |
||
279 | 1 | * @return JSONResponse |
|
280 | */ |
||
281 | 1 | public function saveAttachment($accountId, $folderId, $messageId, $attachmentId, $targetPath) { |
|
315 | |||
316 | /** |
||
317 | 2 | * @NoAdminRequired |
|
318 | 2 | * |
|
319 | * @param int $accountId |
||
320 | 2 | * @param string $folderId |
|
321 | 2 | * @param string $messageId |
|
322 | 2 | * @param array $flags |
|
323 | 1 | * @return JSONResponse |
|
324 | 1 | */ |
|
325 | 1 | public function setFlags($accountId, $folderId, $messageId, $flags) { |
|
339 | |||
340 | 3 | /** |
|
341 | 3 | * @NoAdminRequired |
|
342 | * |
||
343 | 3 | * @param int $accountId |
|
344 | 2 | * @param string $folderId |
|
345 | 1 | * @param string $id |
|
346 | * @return JSONResponse |
||
347 | 2 | */ |
|
348 | 2 | public function destroy($accountId, $folderId, $id) { |
|
361 | 8 | ||
362 | 8 | /** |
|
363 | * @param int $accountId |
||
364 | * @return IAccount |
||
365 | */ |
||
366 | private function getAccount($accountId) { |
||
367 | if (!array_key_exists($accountId, $this->accounts)) { |
||
368 | $this->accounts[$accountId] = $this->accountService->find($this->currentUserId, $accountId); |
||
369 | } |
||
370 | 6 | return $this->accounts[$accountId]; |
|
371 | 6 | } |
|
372 | 6 | ||
373 | /** |
||
374 | * @param int $accountId |
||
375 | * @param string $folderId |
||
376 | * @return IMailBox |
||
377 | */ |
||
378 | private function getFolder($accountId, $folderId) { |
||
382 | |||
383 | /** |
||
384 | * @param string $messageId |
||
385 | * @param $accountId |
||
386 | * @param $folderId |
||
387 | * @return callable |
||
388 | */ |
||
389 | private function enrichDownloadUrl($accountId, $folderId, $messageId, $attachment) { |
||
390 | $downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment', [ |
||
391 | 'accountId' => $accountId, |
||
392 | 'folderId' => $folderId, |
||
393 | 'messageId' => $messageId, |
||
394 | 'attachmentId' => $attachment['id'], |
||
395 | ]); |
||
396 | $downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl); |
||
397 | $attachment['downloadUrl'] = $downloadUrl; |
||
398 | $attachment['mimeUrl'] = $this->mimeTypeDetector->mimeTypeIcon($attachment['mime']); |
||
399 | |||
400 | if ($this->attachmentIsImage($attachment)) { |
||
401 | $attachment['isImage'] = true; |
||
402 | } else if ($this->attachmentIsCalendarEvent($attachment)) { |
||
403 | $attachment['isCalendarEvent'] = true; |
||
404 | } |
||
405 | return $attachment; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param $attachment |
||
410 | * |
||
411 | * Determines if the content of this attachment is an image |
||
412 | * |
||
413 | * @return boolean |
||
414 | */ |
||
415 | private function attachmentIsImage($attachment) { |
||
416 | return in_array( |
||
417 | $attachment['mime'], [ |
||
418 | 'image/jpeg', |
||
419 | 'image/png', |
||
420 | 'image/gif' |
||
421 | ]); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @param type $attachment |
||
426 | * @return boolean |
||
427 | */ |
||
428 | private function attachmentIsCalendarEvent($attachment) { |
||
429 | return $attachment['mime'] === 'text/calendar'; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @param string $accountId |
||
434 | * @param string $folderId |
||
435 | * @param string $messageId |
||
436 | * @return string |
||
437 | */ |
||
438 | private function buildHtmlBodyUrl($accountId, $folderId, $messageId) { |
||
439 | $htmlBodyUrl = $this->urlGenerator->linkToRoute('mail.messages.getHtmlBody', [ |
||
440 | 'accountId' => $accountId, |
||
441 | 'folderId' => $folderId, |
||
442 | 'messageId' => $messageId, |
||
443 | ]); |
||
444 | return $this->urlGenerator->getAbsoluteURL($htmlBodyUrl); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @param integer $accountId |
||
449 | * @param string $folderId |
||
450 | */ |
||
451 | private function loadMultiple($accountId, $folderId, $ids) { |
||
462 | |||
463 | /** |
||
464 | * @param $accountId |
||
465 | * @param $folderId |
||
466 | * @param $id |
||
467 | * @param $m |
||
468 | * @param IAccount $account |
||
469 | * @param IMailBox $mailBox |
||
470 | * @return mixed |
||
471 | */ |
||
472 | private function enhanceMessage($accountId, $folderId, $id, $m, IAccount $account, $mailBox) { |
||
498 | |||
499 | } |
||
500 |
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.