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