Complex classes like AbstractSwiftTwigMailTemplate 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 AbstractSwiftTwigMailTemplate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | abstract class AbstractSwiftTwigMailTemplate implements SwiftMailTemplate |
||
8 | { |
||
9 | /** |
||
10 | * @var \Twig_Environment |
||
11 | */ |
||
12 | protected $twigEnvironment; |
||
13 | |||
14 | /** |
||
15 | * @var string|array |
||
16 | */ |
||
17 | protected $fromAddresses; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $fromName = null; |
||
23 | |||
24 | /** |
||
25 | * @var string|array |
||
26 | */ |
||
27 | protected $toAddresses; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $toName = null; |
||
33 | |||
34 | /** |
||
35 | * @var string|array |
||
36 | */ |
||
37 | protected $bccAddresses; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $bccName = null; |
||
43 | |||
44 | /** |
||
45 | * @var string|array |
||
46 | */ |
||
47 | protected $ccAddresses; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $ccName = null; |
||
53 | |||
54 | /** |
||
55 | * @var string|array |
||
56 | */ |
||
57 | protected $replyToAddresses; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $replyToName = null; |
||
63 | |||
64 | /** |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $maxLineLength = 1000; |
||
68 | |||
69 | /** |
||
70 | * @var int |
||
71 | */ |
||
72 | protected $priority; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $readReceiptTo; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $returnPath; |
||
83 | |||
84 | /** |
||
85 | * SwiftTwigMailGenerator constructor. |
||
86 | * |
||
87 | * @param \Twig_Environment $twig_Environment |
||
88 | */ |
||
89 | public function __construct(\Twig_Environment $twig_Environment) |
||
93 | |||
94 | /** |
||
95 | * @return \Twig_Template|\Twig_TemplateInterface |
||
96 | */ |
||
97 | abstract public function getTemplate(); |
||
98 | |||
99 | /** |
||
100 | * @param array $data |
||
101 | * |
||
102 | * @return \Swift_Message |
||
103 | */ |
||
104 | public function renderMail(array $data = []) :\Swift_Message |
||
176 | |||
177 | /** |
||
178 | * @param array|string $fromAddresses |
||
179 | */ |
||
180 | public function setFromAddresses($fromAddresses) |
||
184 | |||
185 | /** |
||
186 | * @param string $fromName |
||
187 | */ |
||
188 | public function setFromName($fromName) |
||
192 | |||
193 | /** |
||
194 | * @param array|string $toAddresses |
||
195 | */ |
||
196 | public function setToAddresses($toAddresses) |
||
200 | |||
201 | /** |
||
202 | * @param string $toName |
||
203 | */ |
||
204 | public function setToName($toName) |
||
208 | |||
209 | /** |
||
210 | * @param array|string $bccAddresses |
||
211 | */ |
||
212 | public function setBccAddresses($bccAddresses) |
||
216 | |||
217 | /** |
||
218 | * @param string $bccName |
||
219 | */ |
||
220 | public function setBccName($bccName) |
||
224 | |||
225 | /** |
||
226 | * @param array|string $ccAddresses |
||
227 | */ |
||
228 | public function setCcAddresses($ccAddresses) |
||
232 | |||
233 | /** |
||
234 | * @param string $ccName |
||
235 | */ |
||
236 | public function setCcName($ccName) |
||
240 | |||
241 | /** |
||
242 | * @param array|string $replyToAddresses |
||
243 | */ |
||
244 | public function setReplyToAddresses($replyToAddresses) |
||
248 | |||
249 | /** |
||
250 | * @param string $replyToName |
||
251 | */ |
||
252 | public function setReplyToName($replyToName) |
||
256 | |||
257 | /** |
||
258 | * @param int $maxLineLength |
||
259 | */ |
||
260 | public function setMaxLineLength($maxLineLength) |
||
264 | |||
265 | /** |
||
266 | * @param int $priority |
||
267 | */ |
||
268 | public function setPriority($priority) |
||
272 | |||
273 | /** |
||
274 | * @param string $readReceiptTo |
||
275 | */ |
||
276 | public function setReadReceiptTo($readReceiptTo) |
||
280 | |||
281 | /** |
||
282 | * @param string $returnPath |
||
283 | */ |
||
284 | public function setReturnPath($returnPath) |
||
288 | |||
289 | /** |
||
290 | * Removes the HTML tags from the text. |
||
291 | * |
||
292 | * @param string $s |
||
293 | * @param string $keep The list of tags to keep |
||
294 | * @param string $expand The list of tags to remove completely, along their content |
||
295 | */ |
||
296 | private function removeHtml(string $s, string $keep = '', string $expand = 'script|style|noframes|select|option') :string |
||
338 | |||
339 | /** |
||
340 | * @param string $s |
||
341 | * @param string $openTag |
||
342 | * @param string $closeTag |
||
343 | * |
||
344 | * @return mixed|string |
||
345 | */ |
||
346 | private function removeElement(string $s, string $openTag, string $closeTag) |
||
360 | |||
361 | /** |
||
362 | * @param string $s |
||
363 | * @param array $tagToKeep |
||
364 | * @param string $initial |
||
365 | * @param string $finalize |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | private function keepTag(string $s, array $tagToKeep, string $initial, string $finalize):string |
||
379 | } |
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: