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) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Setup properties needed for the process. |
||
| 167 | * |
||
| 168 | * @param Message\Queue $latestMessage |
||
| 169 | * @param Collection $allMessages |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | protected function setup(Message\Queue $latestMessage, Collection $allMessages) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Whether or not we have all the needed properties. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | abstract protected function validateData(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Process any messages queue that is to send messages to specific users. |
||
| 201 | * For example, assign issue to user to message the user about the issue. |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | protected function processDirectMessages() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Populate any data or properties. |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | protected function populateData() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Whether or not the latest message is about status change such as closed issue. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | abstract public function isStatusMessage(); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns the message subject. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | protected function getSubject() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns an array of data needed for the message. |
||
| 237 | * |
||
| 238 | * @param Message\Queue $queue |
||
| 239 | * @param array $extraData |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | protected function getMessageData(Message\Queue $queue, array $extraData = []) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Loop through all of the messages and combine its message data. |
||
| 273 | * |
||
| 274 | * @param Collection $changes |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function getCombineMessageData(Collection $changes) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return text to be used for the message heading. |
||
| 300 | * |
||
| 301 | * @param Message\Queue $queue |
||
| 302 | * @param Collection|null $changes |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function getMessageHeading(Message\Queue $queue, Collection $changes = null) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns collection of all users in a project that should receive the messages. |
||
| 320 | * |
||
| 321 | * @return Collection |
||
| 322 | */ |
||
| 323 | protected function getProjectUsers() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns the model that is belong to the queue message. |
||
| 338 | * |
||
| 339 | * @return Issue|Issue\Comment|Note |
||
| 340 | */ |
||
| 341 | protected function getModel() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns an instance of project issue. |
||
| 348 | * |
||
| 349 | * @return Issue |
||
| 350 | */ |
||
| 351 | abstract protected function getIssue(); |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns an instance of project. |
||
| 355 | * |
||
| 356 | * @return Project |
||
| 357 | */ |
||
| 358 | abstract protected function getProject(); |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns the id of a project. |
||
| 362 | * |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | abstract protected function getProjectId(); |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns collection of all of the users that must not receive messages. |
||
| 369 | * |
||
| 370 | * @return Collection |
||
| 371 | */ |
||
| 372 | protected function getExcludeUsers() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Exclude a user from receiving messages. |
||
| 383 | * |
||
| 384 | * @param User $user |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | protected function addToExcludeUsers(User $user) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Find user by id. This search the project users and fallback to excluded list of users. |
||
| 397 | * |
||
| 398 | * @param int $userId |
||
| 399 | * |
||
| 400 | * @return User |
||
| 401 | */ |
||
| 402 | protected function getUserById($userId) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns collection of all messages. |
||
| 415 | * |
||
| 416 | * @return Collection |
||
| 417 | */ |
||
| 418 | protected function getMessages() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Send a message to a user. |
||
| 429 | * |
||
| 430 | * @param User $user |
||
| 431 | * @param array $data |
||
| 432 | * |
||
| 433 | * @return mixed |
||
| 434 | */ |
||
| 435 | private function sendMessage(User $user, array $data) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Send a message to a collection of users, or send customised message per use logic. |
||
| 449 | * |
||
| 450 | * @param Collection $users |
||
| 451 | * @param array $data |
||
| 452 | * |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | protected function sendMessages(Collection $users, array $data) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get customised message per user logic. |
||
| 469 | * |
||
| 470 | * @param int $userId |
||
| 471 | * @param array $messagesData |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | protected function getUserMessageData($userId, array $messagesData) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Whether or not the user wants to receive the message. |
||
| 509 | * |
||
| 510 | * @param Project\User $user |
||
| 511 | * @param array $data |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | protected function wantToReceiveMessage(Project\User $user, array $data) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Send a message to al users in project and full subscribes. |
||
| 553 | * |
||
| 554 | * @param Message\Queue $queue |
||
| 555 | * |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | protected function sendMessageToAll(Message\Queue $queue) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Load the creator of an issue to the collection of project users. So we can send message to creator if needed. |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | protected function loadIssueCreatorToProjectUsers() |
||
| 601 | } |
||
| 602 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: