Complex classes like InteractsWithMailCatcher 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 InteractsWithMailCatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait InteractsWithMailCatcher |
||
8 | { |
||
9 | use InteractsWithSwiftEmailer; |
||
10 | |||
11 | /** |
||
12 | * Init setup |
||
13 | */ |
||
14 | public function setUp() |
||
19 | |||
20 | /** |
||
21 | * Init client and clean the messages |
||
22 | */ |
||
23 | public function initCatcher() |
||
28 | |||
29 | /** |
||
30 | * Clear the list from all emails |
||
31 | */ |
||
32 | public function cleanEmailMessages() |
||
36 | |||
37 | /** |
||
38 | * Assert one email was send so list is not empty |
||
39 | */ |
||
40 | public function assertEmailIsSent($description = '') |
||
44 | |||
45 | /** |
||
46 | * Assert that First Email's subject contains a string |
||
47 | * |
||
48 | * @param $needle |
||
49 | * @param string $description |
||
50 | */ |
||
51 | public function assertEmailFirstSubjectContains($needle, $description = '') |
||
55 | |||
56 | /** |
||
57 | * Assert that Last Email's subject contains a string |
||
58 | * |
||
59 | * @param $needle |
||
60 | * @param string $description |
||
61 | */ |
||
62 | public function assertEmailLastSubjectContains($needle, $description = '') |
||
66 | |||
67 | /** |
||
68 | * Assert that certain Email's subject contains a string |
||
69 | * |
||
70 | * @param $needle |
||
71 | * @param string $description |
||
72 | */ |
||
73 | public function assertEmailNthSubjectContains($needle, $nth, $description = '') |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Assert that the subject in the first Email is equal to a string |
||
81 | * @param $expected |
||
82 | * @param string $description |
||
83 | */ |
||
84 | public function assertEmailFirstSubjectEquals($expected, $description = '') |
||
88 | |||
89 | /** |
||
90 | * Assert that the subject in the last Email is equal to a string |
||
91 | * @param $expected |
||
92 | * @param string $description |
||
93 | */ |
||
94 | public function assertEmailLastSubjectEquals($expected, $description = '') |
||
98 | |||
99 | /** |
||
100 | * Assert that the subject in the Nth Email is equal to a string |
||
101 | * @param $expected |
||
102 | * @param $nth |
||
103 | * @param string $description |
||
104 | */ |
||
105 | public function assertEmailNthSubjectEquals($expected, $nth, $description = '') |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Assert that the subject of an Email is equal to a string |
||
113 | * @param $expected |
||
114 | * @param $email |
||
115 | * @param string $description |
||
116 | */ |
||
117 | public function assertEmailSubjectEquals($expected, $email, $description = '') |
||
121 | |||
122 | /** |
||
123 | * Assert HTML body of First Email contains a certain text |
||
124 | * |
||
125 | * @param $needle |
||
126 | * @param string $description |
||
127 | */ |
||
128 | public function assertEmailFirstHtmlContains($needle, $description = '') |
||
132 | |||
133 | /** |
||
134 | * Assert HTML body of Last Email contains a certain text |
||
135 | * |
||
136 | * @param $needle |
||
137 | * @param string $description |
||
138 | */ |
||
139 | public function assertEmailLastHtmlContains($needle, $description = '') |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Assert HTML body of certain Nth Email contains a certain text |
||
147 | * |
||
148 | * @param $needle |
||
149 | * @param $nth |
||
150 | * @param string $description |
||
151 | */ |
||
152 | public function assertEmailNthHtmlContains($needle, $nth, $description = '') |
||
156 | |||
157 | /** |
||
158 | * Assert HTML body of Email contains a certain text |
||
159 | * |
||
160 | * @param $needle |
||
161 | * @param $email |
||
162 | * @param string $description |
||
163 | */ |
||
164 | public function assertEmailHtmlContains($needle, $email, $description = '') |
||
169 | |||
170 | /** |
||
171 | * Assert Text body of First Email contains a certain text |
||
172 | * |
||
173 | * @param $needle |
||
174 | * @param string $description |
||
175 | */ |
||
176 | public function assertEmailFirstTextContains($needle, $description = '') |
||
180 | |||
181 | /** |
||
182 | * Assert Text body of Last Email contains a certain text |
||
183 | * |
||
184 | * @param $needle |
||
185 | * @param string $description |
||
186 | */ |
||
187 | public function assertEmailLastTextContains($needle, $description = '') |
||
191 | |||
192 | /** |
||
193 | * Assert Text body of Nth Email contains a certain text |
||
194 | * |
||
195 | * @param $needle |
||
196 | * @param $nth |
||
197 | * @param string $description |
||
198 | */ |
||
199 | public function assertEmailNthTextContains($needle, $nth, $description = '') |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Assert Text body of Email contains a certain text |
||
207 | * |
||
208 | * @param $needle |
||
209 | * @param string $description |
||
210 | */ |
||
211 | public function assertEmailTextContains($needle, $email, $description = '') |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Assert that the sender of first Email is equal to a string |
||
220 | * |
||
221 | * @param $expected |
||
222 | * @param string $description |
||
223 | */ |
||
224 | public function assertEmailFirstSenderEquals($expected, $description = '') |
||
228 | |||
229 | /** |
||
230 | * Assert that the sender of last Email is equal to a string |
||
231 | * |
||
232 | * @param $expected |
||
233 | * @param string $description |
||
234 | */ |
||
235 | public function assertEmailLastSenderEquals($expected, $description = '') |
||
239 | |||
240 | /** |
||
241 | * Assert that the sender of Nth Email is equal to a string |
||
242 | * |
||
243 | * @param $expected |
||
244 | * @param $nth |
||
245 | * @param string $description |
||
246 | */ |
||
247 | public function assertEmailNthSenderEquals($expected, $nth, $description = '') |
||
251 | |||
252 | /** |
||
253 | * Assert that the sender is equal to a string |
||
254 | * |
||
255 | * @param $expected |
||
256 | * @param $email |
||
257 | * @param string $description |
||
258 | */ |
||
259 | public function assertEmailSenderEquals($expected, $email, $description = '') |
||
264 | |||
265 | /** |
||
266 | * Assert that the recipients list of First Email contains a certain one |
||
267 | * |
||
268 | * @param $needle |
||
269 | * @param string $description |
||
270 | */ |
||
271 | public function assertEmailFirstRecipientsContain($needle, $description = '') |
||
275 | |||
276 | /** |
||
277 | * Assert that the recipients list of Last Email contains a certain one |
||
278 | * |
||
279 | * @param $needle |
||
280 | * @param string $description |
||
281 | */ |
||
282 | public function assertEmailLastRecipientsContain($needle, $description = '') |
||
286 | |||
287 | /** |
||
288 | * Assert that the recipients list of Nth Email contains a certain one |
||
289 | * |
||
290 | * @param $needle |
||
291 | * @param $nth |
||
292 | * @param string $description |
||
293 | */ |
||
294 | public function assertEmailNthRecipientsContain($needle, $nth, $description = '') |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Assert that the recipients list of Email contains a certain one |
||
302 | * |
||
303 | * @param $needle |
||
304 | * @param string $description |
||
305 | */ |
||
306 | public function assertEmailRecipientsContain($needle, $email, $description = '') |
||
311 | |||
312 | /** |
||
313 | * Assert that the cc list of First Mail contains a certain one |
||
314 | * |
||
315 | * @param $needle |
||
316 | * @param string $description |
||
317 | */ |
||
318 | public function assertEmailFirstCcContain($needle, $description = '') |
||
322 | |||
323 | /** |
||
324 | * Assert that the cc list of Last Mail contains a certain one |
||
325 | * |
||
326 | * @param $needle |
||
327 | * @param string $description |
||
328 | */ |
||
329 | public function assertEmailLastCcContain($needle, $description = '') |
||
333 | |||
334 | /** |
||
335 | * Assert that the cc list of Nth Mail contains a certain one |
||
336 | * |
||
337 | * @param $needle |
||
338 | * $param $nth |
||
339 | * @param string $description |
||
340 | */ |
||
341 | public function assertEmailNthCcContain($needle, $nth, $description = '') |
||
345 | |||
346 | /** |
||
347 | * Assert that the cc list of Email contains a certain one |
||
348 | * |
||
349 | * @param $needle |
||
350 | * @param string $description |
||
351 | */ |
||
352 | public function assertEmailCcContain($needle, $email, $description = '') |
||
357 | |||
358 | /** |
||
359 | * Assert that the Bcc exists on the First Email |
||
360 | * Mailcatcher doesn't add a seperate bcc field, it creates a new email |
||
361 | * with the bcc address as the recipient. |
||
362 | * |
||
363 | * @param $needle |
||
364 | * @param string $description |
||
365 | */ |
||
366 | public function assertEmailFirstBccContain($needle, $description = '') |
||
370 | |||
371 | |||
372 | /** |
||
373 | * Assert that the Bcc list of the Last Email contains a certain one |
||
374 | * |
||
375 | * @param $needle |
||
376 | * @param string $description |
||
377 | */ |
||
378 | public function assertEmailLastBccContain($needle, $description = '') |
||
382 | |||
383 | /** |
||
384 | * Assert that the Bcc list of the Nth Email contains a certain one |
||
385 | * |
||
386 | * @param $needle |
||
387 | * @param $nth |
||
388 | * @param string $description |
||
389 | */ |
||
390 | public function assertEmailNthtBccContain($needle, $nth, $description = '') |
||
394 | |||
395 | /** |
||
396 | * Assert that the Bcc list of Email contains a certain one |
||
397 | * |
||
398 | * @param $needle |
||
399 | * @param string $description |
||
400 | */ |
||
401 | public function assertEmailBccContain($needle, $email, $description = '') |
||
405 | |||
406 | /** |
||
407 | * Helper in order to hack the mailcatcher issue with Bcc field |
||
408 | * @param $needle |
||
409 | * @param $description |
||
410 | * @param $messages |
||
411 | */ |
||
412 | private function fetchEmailWithBcc($needle, $messages, $description) |
||
427 | |||
428 | /** |
||
429 | * Get the first message |
||
430 | */ |
||
431 | private function getEmailFirstMessage() |
||
440 | |||
441 | /** |
||
442 | * Get the last message |
||
443 | */ |
||
444 | private function getEmailLastMessage() |
||
453 | |||
454 | /** |
||
455 | * Get a certain message |
||
456 | */ |
||
457 | private function getEmailMessage($nth) |
||
466 | |||
467 | |||
468 | /** |
||
469 | * Get all messages |
||
470 | */ |
||
471 | private function getEmailMessages() |
||
476 | } |
||
477 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: