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 |
||
49 | class MessagesController extends Controller { |
||
50 | |||
51 | /** @var AccountService */ |
||
52 | private $accountService; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $currentUserId; |
||
58 | |||
59 | /** |
||
60 | * @var ContactsIntegration |
||
61 | */ |
||
62 | private $contactsIntegration; |
||
63 | |||
64 | /** |
||
65 | * @var \OCA\Mail\Service\Logger |
||
66 | */ |
||
67 | private $logger; |
||
68 | |||
69 | /** |
||
70 | * @var \OCP\Files\Folder |
||
71 | */ |
||
72 | private $userFolder; |
||
73 | |||
74 | /** |
||
75 | * @var IL10N |
||
76 | */ |
||
77 | private $l10n; |
||
78 | |||
79 | /** |
||
80 | * @var IAccount[] |
||
81 | */ |
||
82 | private $accounts = []; |
||
83 | |||
84 | /** |
||
85 | * @param string $appName |
||
86 | * @param IRequest $request |
||
87 | * @param AccountService $accountService |
||
88 | * @param string $UserId |
||
89 | * @param $userFolder |
||
90 | * @param ContactsIntegration $contactsIntegration |
||
91 | * @param Logger $logger |
||
92 | * @param IL10N $l10n |
||
93 | */ |
||
94 | 12 | public function __construct($appName, |
|
95 | IRequest $request, |
||
96 | AccountService $accountService, |
||
97 | $UserId, |
||
98 | $userFolder, |
||
99 | ContactsIntegration $contactsIntegration, |
||
100 | Logger $logger, |
||
101 | IL10N $l10n) { |
||
102 | 12 | parent::__construct($appName, $request); |
|
103 | 12 | $this->accountService = $accountService; |
|
104 | 12 | $this->currentUserId = $UserId; |
|
105 | 12 | $this->userFolder = $userFolder; |
|
106 | 12 | $this->contactsIntegration = $contactsIntegration; |
|
107 | 12 | $this->logger = $logger; |
|
108 | 12 | $this->l10n = $l10n; |
|
109 | 12 | } |
|
110 | |||
111 | /** |
||
112 | * @NoAdminRequired |
||
113 | * @NoCSRFRequired |
||
114 | * |
||
115 | * @param int $accountId |
||
116 | * @param string $folderId |
||
117 | * @param int $from |
||
118 | * @param int $to |
||
119 | * @param string $filter |
||
120 | * @param array $ids |
||
121 | * @return JSONResponse |
||
122 | */ |
||
123 | public function index($accountId, $folderId, $from=0, $to=20, $filter=null, $ids=null) { |
||
124 | if (!is_null($ids)) { |
||
125 | $ids = explode(',', $ids); |
||
126 | |||
127 | return $this->loadMultiple($accountId, $folderId, $ids); |
||
|
|||
128 | } |
||
129 | $mailBox = $this->getFolder($accountId, $folderId); |
||
130 | |||
131 | $this->logger->debug("loading messages $from to $to of folder <$folderId>"); |
||
132 | |||
133 | $json = $mailBox->getMessages($from, $to-$from+1, $filter); |
||
134 | |||
135 | $ci = $this->contactsIntegration; |
||
136 | $json = array_map(function($j) use ($ci, $mailBox) { |
||
137 | if ($mailBox->getSpecialRole() === 'trash') { |
||
138 | $j['delete'] = (string)$this->l10n->t('Delete permanently'); |
||
139 | } |
||
140 | |||
141 | if ($mailBox->getSpecialRole() === 'sent') { |
||
142 | $j['fromEmail'] = $j['toEmail']; |
||
143 | $j['from'] = $j['to']; |
||
144 | if((count($j['toList']) > 1) || (count($j['ccList']) > 0)) { |
||
145 | $j['from'] .= ' ' . $this->l10n->t('& others'); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | $j['senderImage'] = $ci->getPhoto($j['fromEmail']); |
||
150 | return $j; |
||
151 | }, $json); |
||
152 | |||
153 | return new JSONResponse($json); |
||
154 | } |
||
155 | |||
156 | private function loadMessage($accountId, $folderId, $id) { |
||
179 | |||
180 | /** |
||
181 | * @NoAdminRequired |
||
182 | * @NoCSRFRequired |
||
183 | * |
||
184 | * @param int $accountId |
||
185 | * @param string $folderId |
||
186 | * @param mixed $id |
||
187 | * @return JSONResponse |
||
188 | */ |
||
189 | public function show($accountId, $folderId, $id) { |
||
190 | try { |
||
191 | $json = $this->loadMessage($accountId, $folderId, $id); |
||
192 | } catch (DoesNotExistException $ex) { |
||
193 | return new JSONResponse([], 404); |
||
194 | } |
||
195 | return new JSONResponse($json); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @NoAdminRequired |
||
200 | * @NoCSRFRequired |
||
201 | * |
||
202 | * @param int $accountId |
||
203 | * @param string $folderId |
||
204 | * @param string $messageId |
||
205 | * @return \OCA\Mail\Http\HtmlResponse |
||
206 | */ |
||
207 | 1 | public function getHtmlBody($accountId, $folderId, $messageId) { |
|
246 | |||
247 | /** |
||
248 | * @NoAdminRequired |
||
249 | * @NoCSRFRequired |
||
250 | * |
||
251 | * @param int $accountId |
||
252 | * @param string $folderId |
||
253 | * @param string $messageId |
||
254 | * @param string $attachmentId |
||
255 | * @return AttachmentDownloadResponse |
||
256 | */ |
||
257 | 1 | public function downloadAttachment($accountId, $folderId, $messageId, $attachmentId) { |
|
267 | |||
268 | /** |
||
269 | * @NoAdminRequired |
||
270 | * @NoCSRFRequired |
||
271 | * |
||
272 | * @param int $accountId |
||
273 | * @param string $folderId |
||
274 | * @param string $messageId |
||
275 | * @param int $attachmentId |
||
276 | * @param string $targetPath |
||
277 | * @return JSONResponse |
||
278 | */ |
||
279 | 2 | public function saveAttachment($accountId, $folderId, $messageId, $attachmentId, $targetPath) { |
|
280 | 2 | $mailBox = $this->getFolder($accountId, $folderId); |
|
281 | |||
282 | 2 | $attachmentIds = []; |
|
283 | 2 | if($attachmentId === 0) { |
|
284 | // Save all attachments |
||
285 | 1 | $m = $mailBox->getMessage($messageId); |
|
286 | $attachmentIds = array_map(function($a){ |
||
287 | 1 | return $a['id']; |
|
288 | 1 | }, $m->attachments); |
|
289 | 1 | } else { |
|
290 | 1 | $attachmentIds = [$attachmentId]; |
|
291 | } |
||
292 | |||
293 | 2 | foreach($attachmentIds as $attachmentId) { |
|
294 | 2 | $attachment = $mailBox->getAttachment($messageId, $attachmentId); |
|
295 | |||
296 | 2 | $fileName = $attachment->getName(); |
|
297 | 2 | $fileParts = pathinfo($fileName); |
|
298 | 2 | $fileName = $fileParts['filename']; |
|
299 | 2 | $fileExtension = $fileParts['extension']; |
|
300 | 2 | $fullPath = "$targetPath/$fileName.$fileExtension"; |
|
301 | 2 | $counter = 2; |
|
302 | 2 | while($this->userFolder->nodeExists($fullPath)) { |
|
303 | $fullPath = "$targetPath/$fileName ($counter).$fileExtension"; |
||
304 | $counter++; |
||
305 | } |
||
306 | |||
307 | 2 | $newFile = $this->userFolder->newFile($fullPath); |
|
308 | 2 | $newFile->putContent($attachment->getContents()); |
|
309 | 2 | } |
|
310 | |||
311 | 2 | return new JSONResponse(); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * @NoAdminRequired |
||
316 | * |
||
317 | * @param int $accountId |
||
318 | * @param string $folderId |
||
319 | * @param string $messageId |
||
320 | * @param array $flags |
||
321 | * @return JSONResponse |
||
322 | */ |
||
323 | 2 | public function setFlags($accountId, $folderId, $messageId, $flags) { |
|
337 | |||
338 | /** |
||
339 | * @NoAdminRequired |
||
340 | * |
||
341 | * @param int $accountId |
||
342 | * @param string $folderId |
||
343 | * @param string $id |
||
344 | * @return JSONResponse |
||
345 | */ |
||
346 | 3 | public function destroy($accountId, $folderId, $id) { |
|
347 | 3 | $this->logger->debug("deleting message <$id> of folder <$folderId>, account <$accountId>"); |
|
348 | try { |
||
349 | 3 | $account = $this->getAccount($accountId); |
|
350 | 2 | $account->deleteMessage(base64_decode($folderId), $id); |
|
351 | 1 | return new JSONResponse(); |
|
352 | |||
353 | 2 | } catch (DoesNotExistException $e) { |
|
354 | 2 | $this->logger->error("could not delete message <$id> of folder <$folderId>, " |
|
355 | 2 | . "account <$accountId> because it does not exist"); |
|
356 | 2 | return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param int $accountId |
||
362 | * @return \OCA\Mail\Service\IAccount |
||
363 | */ |
||
364 | 9 | private function getAccount($accountId) { |
|
365 | 9 | if (!array_key_exists($accountId, $this->accounts)) { |
|
366 | 9 | $this->accounts[$accountId] = $this->accountService->find($this->currentUserId, $accountId); |
|
367 | 8 | } |
|
368 | 8 | return $this->accounts[$accountId]; |
|
369 | } |
||
370 | |||
371 | /** |
||
372 | * @param int $accountId |
||
373 | * @param string $folderId |
||
374 | * @return IMailBox |
||
375 | */ |
||
376 | 6 | private function getFolder($accountId, $folderId) { |
|
380 | |||
381 | /** |
||
382 | * @param string $messageId |
||
383 | * @param $accountId |
||
384 | * @param $folderId |
||
385 | * @return callable |
||
386 | */ |
||
387 | private function enrichDownloadUrl($accountId, $folderId, $messageId, $attachment) { |
||
405 | |||
406 | /** |
||
407 | * @param $attachment |
||
408 | * |
||
409 | * Determines if the content of this attachment is an image |
||
410 | * |
||
411 | * @return boolean |
||
412 | */ |
||
413 | private function attachmentIsImage($attachment) { |
||
421 | |||
422 | /** |
||
423 | * @param type $attachment |
||
424 | * @return boolean |
||
425 | */ |
||
426 | private function attachmentIsCalendarEvent($attachment) { |
||
429 | |||
430 | /** |
||
431 | * @param string $accountId |
||
432 | * @param string $folderId |
||
433 | * @param string $messageId |
||
434 | * @return string |
||
435 | */ |
||
436 | private function buildHtmlBodyUrl($accountId, $folderId, $messageId) { |
||
444 | |||
445 | /** |
||
446 | * @param integer $accountId |
||
447 | * @param string $folderId |
||
448 | */ |
||
449 | private function loadMultiple($accountId, $folderId, $ids) { |
||
460 | |||
461 | /** |
||
462 | * @param $accountId |
||
463 | * @param $folderId |
||
464 | * @param $id |
||
465 | * @param $m |
||
466 | * @param IAccount $account |
||
467 | * @param IMailBox $mailBox |
||
468 | * @return mixed |
||
469 | */ |
||
470 | private function enhanceMessage($accountId, $folderId, $id, $m, IAccount $account, $mailBox) { |
||
496 | |||
497 | /** |
||
498 | * Get path to the icon of a file type |
||
499 | * |
||
500 | * @todo Inject IMimeTypeDetector once core 8.2+ is supported |
||
501 | * |
||
502 | * @param string $mimeType the MIME type |
||
503 | */ |
||
504 | private function mimeTypeIcon($mimeType) { |
||
514 | |||
515 | } |
||
516 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.