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 |
||
52 | class MessagesController extends Controller { |
||
53 | |||
54 | /** @var AccountService */ |
||
55 | private $accountService; |
||
56 | |||
57 | /** @var string */ |
||
58 | private $currentUserId; |
||
59 | |||
60 | /** @var ContactsIntegration */ |
||
61 | private $contactsIntegration; |
||
62 | |||
63 | /** @var Logger */ |
||
64 | private $logger; |
||
65 | |||
66 | /** @var Folder */ |
||
67 | private $userFolder; |
||
68 | |||
69 | /** @var IMimeTypeDetector */ |
||
70 | private $mimeTypeDetector; |
||
71 | |||
72 | /** @var IL10N */ |
||
73 | private $l10n; |
||
74 | |||
75 | /** @var IAccount[] */ |
||
76 | private $accounts = []; |
||
77 | |||
78 | /** |
||
79 | * @param string $appName |
||
80 | * @param IRequest $request |
||
81 | * @param AccountService $accountService |
||
82 | * @param string $UserId |
||
83 | * @param $userFolder |
||
84 | * @param ContactsIntegration $contactsIntegration |
||
85 | * @param Logger $logger |
||
86 | * @param IL10N $l10n |
||
87 | * @param IMimeTypeDetector $mimeTypeDetector |
||
88 | */ |
||
89 | 12 | public function __construct($appName, |
|
90 | IRequest $request, |
||
91 | AccountService $accountService, |
||
92 | $UserId, |
||
93 | $userFolder, |
||
94 | ContactsIntegration $contactsIntegration, |
||
95 | Logger $logger, |
||
96 | IL10N $l10n, |
||
97 | IMimeTypeDetector $mimeTypeDetector) { |
||
98 | 12 | parent::__construct($appName, $request); |
|
99 | 12 | $this->accountService = $accountService; |
|
100 | 12 | $this->currentUserId = $UserId; |
|
101 | 12 | $this->userFolder = $userFolder; |
|
102 | 12 | $this->contactsIntegration = $contactsIntegration; |
|
103 | 12 | $this->logger = $logger; |
|
104 | 12 | $this->l10n = $l10n; |
|
105 | 12 | $this->mimeTypeDetector = $mimeTypeDetector; |
|
106 | 12 | } |
|
107 | |||
108 | /** |
||
109 | * @NoAdminRequired |
||
110 | * @NoCSRFRequired |
||
111 | * |
||
112 | * @param int $accountId |
||
113 | * @param string $folderId |
||
114 | * @param int $from |
||
115 | * @param int $to |
||
116 | * @param string $filter |
||
117 | * @param array $ids |
||
118 | * @return JSONResponse |
||
119 | */ |
||
120 | public function index($accountId, $folderId, $from=0, $to=20, $filter=null, $ids=null) { |
||
152 | |||
153 | private function loadMessage($accountId, $folderId, $id) { |
||
176 | |||
177 | /** |
||
178 | * @NoAdminRequired |
||
179 | * @NoCSRFRequired |
||
180 | * |
||
181 | * @param int $accountId |
||
182 | * @param string $folderId |
||
183 | * @param mixed $id |
||
184 | * @return JSONResponse |
||
185 | */ |
||
186 | public function show($accountId, $folderId, $id) { |
||
194 | |||
195 | /** |
||
196 | * @NoAdminRequired |
||
197 | * @NoCSRFRequired |
||
198 | * |
||
199 | * @param int $accountId |
||
200 | * @param string $folderId |
||
201 | * @param string $messageId |
||
202 | * @return HtmlResponse|TemplateResponse |
||
203 | */ |
||
204 | 1 | public function getHtmlBody($accountId, $folderId, $messageId) { |
|
240 | |||
241 | /** |
||
242 | * @NoAdminRequired |
||
243 | * @NoCSRFRequired |
||
244 | * |
||
245 | * @param int $accountId |
||
246 | * @param string $folderId |
||
247 | * @param string $messageId |
||
248 | * @param string $attachmentId |
||
249 | * @return AttachmentDownloadResponse |
||
250 | */ |
||
251 | 1 | public function downloadAttachment($accountId, $folderId, $messageId, $attachmentId) { |
|
261 | |||
262 | /** |
||
263 | * @NoAdminRequired |
||
264 | * @NoCSRFRequired |
||
265 | * |
||
266 | * @param int $accountId |
||
267 | * @param string $folderId |
||
268 | * @param string $messageId |
||
269 | * @param int $attachmentId |
||
270 | * @param string $targetPath |
||
271 | * @return JSONResponse |
||
272 | */ |
||
273 | 2 | public function saveAttachment($accountId, $folderId, $messageId, $attachmentId, $targetPath) { |
|
307 | |||
308 | /** |
||
309 | * @NoAdminRequired |
||
310 | * |
||
311 | * @param int $accountId |
||
312 | * @param string $folderId |
||
313 | * @param string $messageId |
||
314 | * @param array $flags |
||
315 | * @return JSONResponse |
||
316 | */ |
||
317 | 2 | public function setFlags($accountId, $folderId, $messageId, $flags) { |
|
331 | |||
332 | /** |
||
333 | * @NoAdminRequired |
||
334 | * |
||
335 | * @param int $accountId |
||
336 | * @param string $folderId |
||
337 | * @param string $id |
||
338 | * @return JSONResponse |
||
339 | */ |
||
340 | 3 | public function destroy($accountId, $folderId, $id) { |
|
353 | |||
354 | /** |
||
355 | * @param int $accountId |
||
356 | * @return IAccount |
||
357 | */ |
||
358 | 9 | private function getAccount($accountId) { |
|
364 | |||
365 | /** |
||
366 | * @param int $accountId |
||
367 | * @param string $folderId |
||
368 | * @return IMailBox |
||
369 | */ |
||
370 | 6 | private function getFolder($accountId, $folderId) { |
|
374 | |||
375 | /** |
||
376 | * @param string $messageId |
||
377 | * @param $accountId |
||
378 | * @param $folderId |
||
379 | * @return callable |
||
380 | */ |
||
381 | private function enrichDownloadUrl($accountId, $folderId, $messageId, $attachment) { |
||
399 | |||
400 | /** |
||
401 | * @param $attachment |
||
402 | * |
||
403 | * Determines if the content of this attachment is an image |
||
404 | * |
||
405 | * @return boolean |
||
406 | */ |
||
407 | private function attachmentIsImage($attachment) { |
||
415 | |||
416 | /** |
||
417 | * @param type $attachment |
||
418 | * @return boolean |
||
419 | */ |
||
420 | private function attachmentIsCalendarEvent($attachment) { |
||
423 | |||
424 | /** |
||
425 | * @param string $accountId |
||
426 | * @param string $folderId |
||
427 | * @param string $messageId |
||
428 | * @return string |
||
429 | */ |
||
430 | private function buildHtmlBodyUrl($accountId, $folderId, $messageId) { |
||
438 | |||
439 | /** |
||
440 | * @param integer $accountId |
||
441 | * @param string $folderId |
||
442 | */ |
||
443 | private function loadMultiple($accountId, $folderId, $ids) { |
||
454 | |||
455 | /** |
||
456 | * @param $accountId |
||
457 | * @param $folderId |
||
458 | * @param $id |
||
459 | * @param $m |
||
460 | * @param IAccount $account |
||
461 | * @param IMailBox $mailBox |
||
462 | * @return mixed |
||
463 | */ |
||
464 | private function enhanceMessage($accountId, $folderId, $id, $m, IAccount $account, $mailBox) { |
||
490 | |||
491 | } |
||
492 |
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.