Complex classes like NotificationService 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 NotificationService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class NotificationService |
||
27 | { |
||
28 | /** |
||
29 | * The object manager |
||
30 | * |
||
31 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
32 | */ |
||
33 | protected $objectManager; |
||
34 | |||
35 | /** |
||
36 | * Registration repository |
||
37 | * |
||
38 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
39 | */ |
||
40 | protected $registrationRepository; |
||
41 | |||
42 | /** |
||
43 | * Email Service |
||
44 | * |
||
45 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
46 | */ |
||
47 | protected $emailService; |
||
48 | |||
49 | /** |
||
50 | * Hash Service |
||
51 | * |
||
52 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
53 | */ |
||
54 | protected $hashService; |
||
55 | |||
56 | /** |
||
57 | * FluidStandaloneService |
||
58 | * |
||
59 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
60 | */ |
||
61 | protected $fluidStandaloneService; |
||
62 | |||
63 | /** |
||
64 | * CustomNotificationLogRepository |
||
65 | * |
||
66 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
67 | */ |
||
68 | protected $customNotificationLogRepository; |
||
69 | |||
70 | /** |
||
71 | * AttachmentService |
||
72 | * |
||
73 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
74 | */ |
||
75 | protected $attachmentService; |
||
76 | |||
77 | /** |
||
78 | * @var EventDispatcherInterface |
||
79 | */ |
||
80 | protected $eventDispatcher; |
||
81 | |||
82 | /** |
||
83 | * DI for $attachmentService |
||
84 | * |
||
85 | * @param Notification\AttachmentService $attachmentService |
||
86 | */ |
||
87 | public function injectAttachmentService( |
||
92 | |||
93 | /** |
||
94 | * DI for $customNotificationLogRepository |
||
95 | 8 | * |
|
96 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
97 | 8 | */ |
|
98 | 2 | public function injectCustomNotificationLogRepository( |
|
103 | 6 | ||
104 | 6 | /** |
|
105 | * DI for $emailService |
||
106 | 6 | * |
|
107 | 2 | * @param EmailService $emailService |
|
108 | 2 | */ |
|
109 | 2 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
|
113 | 2 | ||
114 | 2 | /** |
|
115 | 2 | * DI for $fluidStandaloneService |
|
116 | 2 | * |
|
117 | 2 | * @param FluidStandaloneService $fluidStandaloneService |
|
118 | 6 | */ |
|
119 | 6 | public function injectFluidStandaloneService( |
|
124 | |||
125 | /** |
||
126 | * DI for $hashService |
||
127 | * |
||
128 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
129 | */ |
||
130 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
134 | |||
135 | /** |
||
136 | * DI for $objectManager |
||
137 | * |
||
138 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
139 | */ |
||
140 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
144 | |||
145 | 2 | /** |
|
146 | * DI for $registrationRepository |
||
147 | 2 | * |
|
148 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
|
149 | 2 | */ |
|
150 | 2 | public function injectRegistrationRepository( |
|
155 | |||
156 | /** |
||
157 | * @param EventDispatcherInterface $eventDispatcher |
||
158 | */ |
||
159 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
163 | |||
164 | /** |
||
165 | * Sends a custom notification defined by the given customNotification key |
||
166 | 34 | * to all confirmed users of the event |
|
167 | * |
||
168 | 34 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
169 | * @param string $customNotification CustomNotification |
||
170 | 34 | * @param array $settings Settings |
|
171 | 4 | * |
|
172 | * @return int Number of notifications sent |
||
173 | */ |
||
174 | 30 | public function sendCustomNotification($event, $customNotification, $settings) |
|
201 | |||
202 | 34 | /** |
|
203 | * Returns true if conditions are not met to send a custom notification |
||
204 | 34 | * |
|
205 | 34 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
206 | * @param array $settings |
||
207 | 34 | * @param string $customNotification |
|
208 | 6 | * |
|
209 | 6 | * @return bool |
|
210 | 6 | */ |
|
211 | 28 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
|
215 | 22 | ||
216 | 6 | /** |
|
217 | 6 | * Adds a logentry to the custom notification log |
|
218 | 6 | * |
|
219 | 16 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
220 | * @param string $details Details |
||
221 | * @param int $emailsSent E-Mails sent |
||
222 | */ |
||
223 | 16 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
|
232 | |||
233 | 34 | /** |
|
234 | * Sends a message to the user based on the given type |
||
235 | * |
||
236 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
237 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
238 | * @param array $settings Settings |
||
239 | * @param int $type Type |
||
240 | * @param string $customNotification CustomNotification |
||
241 | * |
||
242 | * @return bool TRUE if successful, else FALSE |
||
243 | */ |
||
244 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
336 | |||
337 | /** |
||
338 | 60 | * Returns an array with template and subject for the user message |
|
339 | * |
||
340 | * @param array $settings |
||
341 | 60 | * @param int $type Type |
|
342 | 60 | * @param string $customNotification |
|
343 | 60 | * @return array |
|
344 | 60 | */ |
|
345 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
||
379 | |||
380 | /** |
||
381 | * Sends a message to the admin based on the given type |
||
382 | * |
||
383 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
384 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
385 | * @param array $settings Settings |
||
386 | * @param int $type Type |
||
387 | * |
||
388 | * @return bool TRUE if successful, else FALSE |
||
389 | */ |
||
390 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
462 | |||
463 | /** |
||
464 | * Returns an array with template and subject for the admin message |
||
465 | * |
||
466 | * @param array $settings |
||
467 | * @param int $type Type |
||
468 | * @return array |
||
469 | */ |
||
470 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
500 | |||
501 | /** |
||
502 | * Returns the rendered HTML for the given template |
||
503 | * |
||
504 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
505 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
506 | * @param string $template Template |
||
507 | * @param array $settings Settings |
||
508 | * |
||
509 | * @return string |
||
510 | */ |
||
511 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
527 | } |
||
528 |
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: