Complex classes like PayloadBuilder 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 PayloadBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class PayloadBuilder implements PayloadBuilderInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var Configuration |
||
| 22 | */ |
||
| 23 | private $config; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var RandomNumberGenerator |
||
| 27 | */ |
||
| 28 | private $randomNumberGenerator; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param Configuration $config |
||
| 32 | * @param RandomNumberGenerator $randomNumberGenerator |
||
| 33 | */ |
||
| 34 | 60 | public function __construct(Configuration $config, RandomNumberGenerator $randomNumberGenerator) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @param Swift_Mime_Message $message |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | 57 | public function buildPayload(Swift_Mime_Message $message) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param Swift_Mime_Message $message |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | * @throws Exception |
||
| 76 | */ |
||
| 77 | 57 | private function buildRecipients(Swift_Mime_Message $message) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $email |
||
| 113 | * @param string $name |
||
| 114 | * @param array $tags |
||
| 115 | * @param array $metadata |
||
| 116 | * @param array $substitutionData |
||
| 117 | * @param string $originalEmail |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 54 | private function buildRecipient( |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param Swift_Mime_Message $message |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 54 | private function buildContent(Swift_Mime_Message $message) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param Swift_Mime_Message $message |
||
| 201 | * |
||
| 202 | * @return array|string |
||
| 203 | */ |
||
| 204 | 54 | private function buildFrom(Swift_Mime_Message $message) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param Swift_Mime_Message $message |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | 54 | private function buildHeaders(Swift_Mime_Message $message) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param Swift_Mime_Message $message |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | 54 | private function buildAttachments(Swift_Mime_Message $message) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param Swift_Mime_Message $message |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | 54 | private function buildCampaignId(Swift_Mime_Message $message) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param Swift_Mime_Message $message |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 54 | private function buildMetadata(Swift_Mime_Message $message) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param Swift_Mime_Message $message |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | 54 | private function buildSubstitutionData(Swift_Mime_Message $message) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param Swift_Mime_Message $message |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 54 | private function buildOptions(Swift_Mime_Message $message) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Convert *|foo|* to {{foo}} |
||
| 340 | * |
||
| 341 | * @param string |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 54 | private function convertAsteriskPipeToCurlyBraces($content) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param Swift_Mime_Message $message |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 54 | private function readUserContentType(Swift_Mime_Message $message) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $email |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 54 | private function overrideRecipient($email) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | 54 | private function configuredIpPoolShouldBeUsed() |
|
| 399 | } |
||
| 400 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.