Complex classes like Email 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 Email, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | final class Email extends AbstractOptions |
||
12 | { |
||
13 | const DEFAULT_CHARSET = 'utf-8'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $from = ''; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $fromName = ''; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $replyTo = ''; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $replyToName = ''; |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $to = []; |
||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $cc = []; |
||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $bcc = []; |
||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $encoding = ''; |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $subject = ''; |
||
51 | /** |
||
52 | * @var string|Part|Message |
||
53 | */ |
||
54 | private $body = ''; |
||
55 | /** |
||
56 | * @var string|null |
||
57 | */ |
||
58 | private $template; |
||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private $templateParams = []; |
||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | private $attachments = []; |
||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $attachmentsDir = []; |
||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $charset = self::DEFAULT_CHARSET; |
||
75 | |||
76 | public function __construct($options = null) |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getFrom(): string |
||
89 | |||
90 | /** |
||
91 | * @param string $from |
||
92 | * @return $this|self |
||
93 | */ |
||
94 | public function setFrom(string $from): self |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getFromName(): string |
||
107 | |||
108 | /** |
||
109 | * @param string $fromName |
||
110 | * @return $this|self |
||
111 | */ |
||
112 | public function setFromName(string $fromName): self |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getReplyTo(): string |
||
125 | |||
126 | /** |
||
127 | * @param string $replyTo |
||
128 | * @return $this|self |
||
129 | */ |
||
130 | public function setReplyTo(string $replyTo): self |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getReplyToName(): string |
||
143 | |||
144 | /** |
||
145 | * @param string $replyToName |
||
146 | * @return $this|self |
||
147 | */ |
||
148 | public function setReplyToName(string $replyToName): self |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getTo(): array |
||
161 | |||
162 | /** |
||
163 | * @param array $to |
||
164 | * @return $this|self |
||
165 | */ |
||
166 | public function setTo(array $to): self |
||
171 | |||
172 | public function addTo(string $to): self |
||
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getCc(): array |
||
185 | |||
186 | /** |
||
187 | * @param array $cc |
||
188 | * @return $this|self |
||
189 | */ |
||
190 | public function setCc(array $cc): self |
||
195 | |||
196 | public function addCc(string $cc): self |
||
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | public function getBcc(): array |
||
209 | |||
210 | /** |
||
211 | * @param array $bcc |
||
212 | * @return $this|self |
||
213 | */ |
||
214 | public function setBcc(array $bcc): self |
||
219 | |||
220 | public function addBcc(string $bcc): self |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getEncoding(): string |
||
233 | |||
234 | /** |
||
235 | * @param string $encoding |
||
236 | * @return $this|self |
||
237 | */ |
||
238 | public function setEncoding(string $encoding): self |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getSubject(): string |
||
251 | |||
252 | /** |
||
253 | * @param string $subject |
||
254 | * @return $this|self |
||
255 | */ |
||
256 | public function setSubject(string $subject): self |
||
261 | |||
262 | /** |
||
263 | * @return string|Part|Message |
||
264 | */ |
||
265 | public function getBody() |
||
269 | |||
270 | /** |
||
271 | * @param string|Part|Message $body |
||
272 | * @return $this|self |
||
273 | * @throws InvalidArgumentException |
||
274 | */ |
||
275 | public function setBody($body): self |
||
284 | |||
285 | /** |
||
286 | * @param string|resource|array|Part $file |
||
287 | * @param string|null $filename |
||
288 | * @return $this |
||
289 | * @throws InvalidArgumentException |
||
290 | */ |
||
291 | public function addAttachment($file, string $filename = null): self |
||
304 | |||
305 | /** |
||
306 | * @param array $files |
||
307 | * @return $this |
||
308 | * @throws InvalidArgumentException |
||
309 | */ |
||
310 | public function addAttachments(array $files): self |
||
318 | |||
319 | /** |
||
320 | * @param array $files |
||
321 | * @return $this |
||
322 | * @throws InvalidArgumentException |
||
323 | */ |
||
324 | public function setAttachments(array $files): self |
||
331 | |||
332 | /** |
||
333 | * Returns the list of attachments |
||
334 | * @return array |
||
335 | */ |
||
336 | public function getAttachments(): array |
||
340 | |||
341 | /** |
||
342 | * @return array |
||
343 | */ |
||
344 | public function getAttachmentsDir(): array |
||
348 | |||
349 | /** |
||
350 | * @param array $attachmentsDir |
||
351 | * @return $this|self |
||
352 | */ |
||
353 | public function setAttachmentsDir(array $attachmentsDir): self |
||
358 | |||
359 | /** |
||
360 | * @return bool |
||
361 | */ |
||
362 | public function hasAttachments(): bool |
||
366 | |||
367 | /** |
||
368 | * Processes the attachments dir and merges the result with the attachments array, then returns the result |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | public function getComputedAttachments(): array |
||
401 | |||
402 | /** |
||
403 | * @return string|null |
||
404 | */ |
||
405 | public function getTemplate() |
||
409 | |||
410 | /** |
||
411 | * @param string|null $template |
||
412 | * @return $this|self |
||
413 | */ |
||
414 | public function setTemplate(string $template = null): self |
||
419 | |||
420 | /** |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function hasTemplate(): bool |
||
427 | |||
428 | /** |
||
429 | * @return array |
||
430 | */ |
||
431 | public function getTemplateParams(): array |
||
435 | |||
436 | /** |
||
437 | * @param array $templateParams |
||
438 | * @return $this|self |
||
439 | */ |
||
440 | public function setTemplateParams(array $templateParams): self |
||
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getCharset(): string |
||
453 | |||
454 | /** |
||
455 | * @param string $charset |
||
456 | * @return $this|self |
||
457 | */ |
||
458 | public function setCharset(string $charset): self |
||
463 | } |
||
464 |