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 |
||
25 | class NotificationService |
||
26 | { |
||
27 | /** |
||
28 | * The object manager |
||
29 | * |
||
30 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
31 | */ |
||
32 | protected $objectManager; |
||
33 | |||
34 | /** |
||
35 | * Registration repository |
||
36 | * |
||
37 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
38 | */ |
||
39 | protected $registrationRepository = null; |
||
40 | |||
41 | /** |
||
42 | * Email Service |
||
43 | * |
||
44 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
45 | */ |
||
46 | protected $emailService; |
||
47 | |||
48 | /** |
||
49 | * Hash Service |
||
50 | * |
||
51 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
52 | */ |
||
53 | protected $hashService; |
||
54 | |||
55 | /** |
||
56 | * FluidStandaloneService |
||
57 | * |
||
58 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
59 | */ |
||
60 | protected $fluidStandaloneService; |
||
61 | |||
62 | /** |
||
63 | * CustomNotificationLogRepository |
||
64 | * |
||
65 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
66 | */ |
||
67 | protected $customNotificationLogRepository = null; |
||
68 | |||
69 | /** |
||
70 | * AttachmentService |
||
71 | * |
||
72 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
73 | */ |
||
74 | protected $attachmentService; |
||
75 | |||
76 | /** |
||
77 | * @var EventDispatcherInterface |
||
78 | */ |
||
79 | protected $eventDispatcher; |
||
80 | |||
81 | /** |
||
82 | * DI for $attachmentService |
||
83 | * |
||
84 | * @param Notification\AttachmentService $attachmentService |
||
85 | */ |
||
86 | public function injectAttachmentService( |
||
91 | |||
92 | /** |
||
93 | * DI for $customNotificationLogRepository |
||
94 | * |
||
95 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
96 | */ |
||
97 | public function injectCustomNotificationLogRepository( |
||
102 | |||
103 | /** |
||
104 | * DI for $emailService |
||
105 | * |
||
106 | * @param EmailService $emailService |
||
107 | */ |
||
108 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
||
112 | |||
113 | /** |
||
114 | * DI for $fluidStandaloneService |
||
115 | * |
||
116 | * @param FluidStandaloneService $fluidStandaloneService |
||
117 | */ |
||
118 | public function injectFluidStandaloneService( |
||
123 | |||
124 | /** |
||
125 | * DI for $hashService |
||
126 | * |
||
127 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
128 | */ |
||
129 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
133 | |||
134 | /** |
||
135 | * DI for $objectManager |
||
136 | * |
||
137 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
138 | */ |
||
139 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
143 | |||
144 | /** |
||
145 | * DI for $registrationRepository |
||
146 | * |
||
147 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
148 | */ |
||
149 | public function injectRegistrationRepository( |
||
154 | |||
155 | /** |
||
156 | * @param EventDispatcherInterface $eventDispatcher |
||
157 | */ |
||
158 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
162 | |||
163 | /** |
||
164 | * Sends a custom notification defined by the given customNotification key |
||
165 | * to all confirmed users of the event |
||
166 | * |
||
167 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
168 | * @param string $customNotification CustomNotification |
||
169 | * @param array $settings Settings |
||
170 | * |
||
171 | * @return int Number of notifications sent |
||
172 | */ |
||
173 | public function sendCustomNotification($event, $customNotification, $settings) |
||
200 | |||
201 | /** |
||
202 | * Returns true if conditions are not met to send a custom notification |
||
203 | * |
||
204 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
205 | * @param array $settings |
||
206 | * @param string $customNotification |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
214 | |||
215 | /** |
||
216 | * Adds a logentry to the custom notification log |
||
217 | * |
||
218 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
219 | * @param string $details Details |
||
220 | * @param int $emailsSent E-Mails sent |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
233 | |||
234 | /** |
||
235 | * Sends a message to the user based on the given type |
||
236 | * |
||
237 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
238 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
239 | * @param array $settings Settings |
||
240 | * @param int $type Type |
||
241 | * @param string $customNotification CustomNotification |
||
242 | * |
||
243 | * @return bool TRUE if successful, else FALSE |
||
244 | */ |
||
245 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
337 | |||
338 | /** |
||
339 | * Returns an array with template and subject for the user message |
||
340 | * |
||
341 | * @param array $settings |
||
342 | * @param int $type Type |
||
343 | * @param string $customNotification |
||
344 | * @return array |
||
345 | */ |
||
346 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
||
380 | |||
381 | /** |
||
382 | * Sends a message to the admin based on the given type |
||
383 | * |
||
384 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
385 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
386 | * @param array $settings Settings |
||
387 | * @param int $type Type |
||
388 | * |
||
389 | * @return bool TRUE if successful, else FALSE |
||
390 | */ |
||
391 | 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: