Complex classes like SendMessagesAbstract 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 SendMessagesAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | abstract class SendMessagesAbstract |
||
30 | { |
||
31 | /** |
||
32 | * Instance of issue that this message belong to. |
||
33 | * |
||
34 | * @var Issue |
||
35 | */ |
||
36 | protected $issue; |
||
37 | |||
38 | /** |
||
39 | * Instance of project that this message belong to. |
||
40 | * |
||
41 | * @var Issue |
||
42 | */ |
||
43 | protected $project; |
||
44 | |||
45 | /** |
||
46 | * The latest message queued. |
||
47 | * |
||
48 | * @var Message\Queue |
||
49 | */ |
||
50 | protected $latestMessage; |
||
51 | |||
52 | /** |
||
53 | * Collection of all of the queued messages. |
||
54 | * |
||
55 | * @var Collection |
||
56 | */ |
||
57 | protected $allMessages; |
||
58 | |||
59 | /** |
||
60 | * Instance of a queued message that is for adding a record (ie. adding issue). |
||
61 | * |
||
62 | * @var Message\Queue |
||
63 | */ |
||
64 | protected $addMessage; |
||
65 | /** |
||
66 | * Collection of users that must not receive messages. |
||
67 | * |
||
68 | * @var Collection |
||
69 | */ |
||
70 | protected $excludeUsers; |
||
71 | /** |
||
72 | * Collection of all of the project users that should receive messages. |
||
73 | * |
||
74 | * @var Collection |
||
75 | */ |
||
76 | protected $projectUsers; |
||
77 | /** |
||
78 | * Collection of full subscribers that will always receive messages. |
||
79 | * |
||
80 | * @var Collection |
||
81 | */ |
||
82 | protected $fullSubscribers; |
||
83 | /** |
||
84 | * Name of message template. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $template; |
||
89 | |||
90 | /** |
||
91 | * @var Mailer |
||
92 | */ |
||
93 | protected $mailer; |
||
94 | |||
95 | /** |
||
96 | * Collection of all messages. |
||
97 | * |
||
98 | * @var Collection |
||
99 | */ |
||
100 | protected $messages; |
||
101 | |||
102 | /** |
||
103 | * Set instance of Mailer. |
||
104 | * |
||
105 | * @param Mailer $mailer |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function setMailer(Mailer $mailer) |
||
115 | |||
116 | /** |
||
117 | * The main method to process the massages queue and send them. |
||
118 | * |
||
119 | * @param Message\Queue $latestMessage |
||
120 | * @param Collection $changes |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function process(Message\Queue $latestMessage, Collection $changes) |
||
160 | |||
161 | /** |
||
162 | * Setup properties needed for the process. |
||
163 | * |
||
164 | * @param Message\Queue $latestMessage |
||
165 | * @param Collection $allMessages |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | protected function setup(Message\Queue $latestMessage, Collection $allMessages) |
||
187 | |||
188 | /** |
||
189 | * Whether or not we have all the needed properties. |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | abstract protected function validateData(); |
||
194 | |||
195 | /** |
||
196 | * Process any messages queue that is to send messages to specific users. |
||
197 | * For example, assign issue to user to message the user about the issue. |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | protected function processDirectMessages() |
||
204 | |||
205 | /** |
||
206 | * Populate any data or properties. |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | protected function populateData() |
||
213 | |||
214 | /** |
||
215 | * Whether or not the latest message is about status change such as closed issue. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | abstract public function isStatusMessage(); |
||
220 | |||
221 | /** |
||
222 | * Returns the message subject. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function getSubject() |
||
230 | |||
231 | /** |
||
232 | * Returns an array of data needed for the message. |
||
233 | * |
||
234 | * @param Message\Queue $queue |
||
235 | * @param array $extraData |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | protected function getMessageData(Message\Queue $queue, array $extraData = []) |
||
266 | |||
267 | /** |
||
268 | * Loop through all of the messages and combine its message data. |
||
269 | * |
||
270 | * @param Collection $changes |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | protected function getCombineMessageData(Collection $changes) |
||
293 | |||
294 | /** |
||
295 | * Return text to be used for the message heading. |
||
296 | * |
||
297 | * @param Message\Queue $queue |
||
298 | * @param Collection|null $changes |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function getMessageHeading(Message\Queue $queue, Collection $changes = null) |
||
313 | |||
314 | /** |
||
315 | * Returns collection of all users in a project that should receive the messages. |
||
316 | * |
||
317 | * @return Collection |
||
318 | */ |
||
319 | protected function getProjectUsers() |
||
331 | |||
332 | /** |
||
333 | * Returns the model that is belong to the queue message. |
||
334 | * |
||
335 | * @return Issue|Issue\Comment|Note |
||
336 | */ |
||
337 | protected function getModel() |
||
341 | |||
342 | /** |
||
343 | * Returns an instance of project issue. |
||
344 | * |
||
345 | * @return Issue |
||
346 | */ |
||
347 | abstract protected function getIssue(); |
||
348 | |||
349 | /** |
||
350 | * Returns an instance of project. |
||
351 | * |
||
352 | * @return Project |
||
353 | */ |
||
354 | abstract protected function getProject(); |
||
355 | |||
356 | /** |
||
357 | * Returns the id of a project. |
||
358 | * |
||
359 | * @return int |
||
360 | */ |
||
361 | abstract protected function getProjectId(); |
||
362 | |||
363 | /** |
||
364 | * Returns collection of all of the users that must not receive messages. |
||
365 | * |
||
366 | * @return Collection |
||
367 | */ |
||
368 | protected function getExcludeUsers() |
||
376 | |||
377 | /** |
||
378 | * Exclude a user from receiving messages. |
||
379 | * |
||
380 | * @param User $user |
||
381 | * |
||
382 | * @return $this |
||
383 | */ |
||
384 | protected function addToExcludeUsers(User $user) |
||
390 | |||
391 | /** |
||
392 | * Find user by id. This search the project users and fallback to excluded list of users. |
||
393 | * |
||
394 | * @param int $userId |
||
395 | * |
||
396 | * @return User |
||
397 | */ |
||
398 | protected function getUserById($userId) |
||
408 | |||
409 | /** |
||
410 | * Returns collection of all messages. |
||
411 | * |
||
412 | * @return Collection |
||
413 | */ |
||
414 | protected function getMessages() |
||
422 | |||
423 | /** |
||
424 | * Send a message to a user. |
||
425 | * |
||
426 | * @param User $user |
||
427 | * @param array $data |
||
428 | * |
||
429 | * @return mixed |
||
430 | */ |
||
431 | private function sendMessage(User $user, array $data) |
||
442 | |||
443 | /** |
||
444 | * Send a message to a collection of users, or send customised message per use logic. |
||
445 | * |
||
446 | * @param Collection $users |
||
447 | * @param array $data |
||
448 | * |
||
449 | * @return void |
||
450 | */ |
||
451 | protected function sendMessages(Collection $users, array $data) |
||
462 | |||
463 | /** |
||
464 | * Get customised message per user logic. |
||
465 | * |
||
466 | * @param int $userId |
||
467 | * @param array $messagesData |
||
468 | * |
||
469 | * @return array |
||
470 | */ |
||
471 | protected function getUserMessageData($userId, array $messagesData) |
||
502 | |||
503 | /** |
||
504 | * Whether or not the user wants to receive the message. |
||
505 | * |
||
506 | * @param Project\User $user |
||
507 | * @param array $data |
||
508 | * |
||
509 | * @return bool |
||
510 | */ |
||
511 | protected function wantToReceiveMessage(Project\User $user, array $data) |
||
546 | |||
547 | /** |
||
548 | * Send a message to al users in project and full subscribes. |
||
549 | * |
||
550 | * @param Message\Queue $queue |
||
551 | * |
||
552 | * @return void |
||
553 | */ |
||
554 | protected function sendMessageToAll(Message\Queue $queue) |
||
560 | |||
561 | /** |
||
562 | * Load the creator of an issue to the collection of project users. So we can send message to creator if needed. |
||
563 | * |
||
564 | * @return void |
||
565 | */ |
||
566 | protected function loadIssueCreatorToProjectUsers() |
||
597 | } |
||
598 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.