Complex classes like Mailer 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 Mailer, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
29 | class Mailer implements MailerContract |
||
30 | { |
||
31 | |||
32 | const SPOOL_SEND_EVENT = 'swiftmailer.spool.send'; |
||
33 | |||
34 | /** |
||
35 | * @var $configs Config |
||
36 | */ |
||
37 | public $configs; |
||
38 | |||
39 | /** |
||
40 | * Stores default transport name |
||
41 | * |
||
42 | * @var $defaultTransport string |
||
43 | */ |
||
44 | protected $defaultTransport; |
||
45 | |||
46 | /** |
||
47 | * Disabled flag |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $disabled = false; |
||
52 | |||
53 | /** |
||
54 | * @var EventDispatcherAdapter |
||
55 | */ |
||
56 | protected $eventDispatcherAdapter; |
||
57 | |||
58 | /** |
||
59 | * @var EventDispatcher |
||
60 | */ |
||
61 | protected $eventDispatcher; |
||
62 | |||
63 | /** |
||
64 | * Default From: |
||
65 | * |
||
66 | * @var $from string |
||
67 | */ |
||
68 | protected $from; |
||
69 | |||
70 | /** |
||
71 | * @var Swift_Transport_SpoolTransport |
||
72 | */ |
||
73 | protected $spoolTransport; |
||
74 | |||
75 | /** |
||
76 | * Default To: |
||
77 | * |
||
78 | * @var $to string |
||
79 | */ |
||
80 | protected $to; |
||
81 | |||
82 | /** |
||
83 | * Registered transport storage |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $transports = []; |
||
88 | |||
89 | /** |
||
90 | * Mailer constructor. |
||
91 | * |
||
92 | * @param \Venta\Contracts\Config\Config $config |
||
93 | * @param EventDispatcher $eventDispatcher |
||
94 | * @throws Exception |
||
95 | */ |
||
96 | public function __construct(Config $config, EventDispatcher $eventDispatcher) |
||
103 | |||
104 | /** |
||
105 | * Get Swift_Message instance, setting default from if defined/not overwritten |
||
106 | * |
||
107 | * @return Swift_Message |
||
108 | */ |
||
109 | public function getMessageBuilder(): Swift_Message |
||
117 | |||
118 | /** |
||
119 | * @return Swift_Transport_SpoolTransport |
||
120 | */ |
||
121 | public function getSpoolTransport() |
||
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function isDisabled() : bool |
||
133 | |||
134 | /** |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function isSpoolEnabled() |
||
141 | |||
142 | /** |
||
143 | * @param string $transport |
||
144 | * @return Swift_Mailer |
||
145 | * @throws TransportException |
||
146 | */ |
||
147 | public function spoolWithTransport($transport = '') |
||
165 | |||
166 | /** |
||
167 | * Get Swift_Mailer with Swift_Transport |
||
168 | * |
||
169 | * @param string $transportName |
||
170 | * @return Swift_Mailer |
||
171 | */ |
||
172 | public function withTransport(string $transportName = ''): Swift_Mailer |
||
178 | |||
179 | /** |
||
180 | * Parse config file interpreting settings |
||
181 | * |
||
182 | * @param \Venta\Contracts\Config\Config $config |
||
183 | * @throws RuntimeException|Exception |
||
184 | * @return \Closure |
||
185 | */ |
||
186 | protected function configureTransport(Config $config) |
||
203 | |||
204 | /** |
||
205 | * Get mailer configs |
||
206 | * |
||
207 | * @param \Venta\Contracts\Config\Config $config |
||
208 | */ |
||
209 | protected function getMailerFromGlobalConfig(Config $config) |
||
222 | |||
223 | /** |
||
224 | * Returns proper transport closure factory |
||
225 | * |
||
226 | * @param $transportName |
||
227 | * @return \Closure |
||
228 | */ |
||
229 | protected function getTransport($transportName) |
||
243 | |||
244 | /** |
||
245 | * Wrap transport instantiation into closure passing necessary config params |
||
246 | * |
||
247 | * @param $transport |
||
248 | * @param $config Config |
||
249 | * @return \Closure |
||
250 | * @throws Exception |
||
251 | */ |
||
252 | protected function prepareTransportFactory($transport, Config $config) |
||
310 | |||
311 | /** |
||
312 | * Register transport factories |
||
313 | * |
||
314 | * @throws Exception |
||
315 | * @return array |
||
316 | */ |
||
317 | protected function registerTransports() |
||
348 | |||
349 | /** |
||
350 | * @return Swift_Transport_SpoolTransport|null |
||
351 | * @throws UnknownTransportException|TransportException|\Swift_IoException |
||
352 | */ |
||
353 | protected function setUpSpoolTransport() |
||
386 | |||
387 | /** |
||
388 | * @param \Venta\Contracts\Config\Config $config |
||
389 | * @throws TransportException |
||
390 | * @throws UnknownTransportException |
||
391 | */ |
||
392 | protected function validateTransportSettings(Config $config) |
||
416 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.