Complex classes like BasePremailer 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 BasePremailer, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Luminaire\Premailer; |
||
| 31 | abstract class BasePremailer |
||
| 32 | { |
||
| 33 | |||
| 34 | const OPTION_STYLE_TAG = 'styleTag'; |
||
| 35 | const OPTION_STYLE_TAG_BODY = 1; |
||
| 36 | const OPTION_STYLE_TAG_HEAD = 2; |
||
| 37 | const OPTION_STYLE_TAG_REMOVE = 3; |
||
| 38 | |||
| 39 | const OPTION_HTML_COMMENTS = 'htmlComments'; |
||
| 40 | const OPTION_HTML_COMMENTS_KEEP = 1; |
||
| 41 | const OPTION_HTML_COMMENTS_REMOVE = 2; |
||
| 42 | |||
| 43 | const OPTION_HTML_CLASSES = 'htmlClasses'; |
||
| 44 | const OPTION_HTML_CLASSES_KEEP = 1; |
||
| 45 | const OPTION_HTML_CLASSES_REMOVE = 2; |
||
| 46 | |||
| 47 | const OPTION_TEXT_LINE_WIDTH = 'textLineWidth'; |
||
| 48 | |||
| 49 | const OPTION_CSS_WRITER_CLASS = 'cssWriterClass'; |
||
| 50 | const OPTION_CSS_WRITER_CLASS_COMPACT = '\Crossjoin\Css\Writer\Compact'; |
||
| 51 | const OPTION_CSS_WRITER_CLASS_PRETTY = '\Crossjoin\Css\Writer\Pretty'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The options for HTML/text generation |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $options = [ |
||
| 59 | self::OPTION_STYLE_TAG => self::OPTION_STYLE_TAG_BODY, |
||
| 60 | self::OPTION_HTML_CLASSES => self::OPTION_HTML_CLASSES_KEEP, |
||
| 61 | self::OPTION_HTML_COMMENTS => self::OPTION_HTML_COMMENTS_REMOVE, |
||
| 62 | self::OPTION_CSS_WRITER_CLASS => self::OPTION_CSS_WRITER_CLASS_COMPACT, |
||
| 63 | self::OPTION_TEXT_LINE_WIDTH => 75, |
||
| 64 | ]; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The charset for HTML/text output |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $charset = "UTF-8"; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The prepared HTML content |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $html; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The prepared text content |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $text; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The loaded DOM Document |
||
| 89 | * |
||
| 90 | * @var \DOMDocument |
||
| 91 | */ |
||
| 92 | protected $doc; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Sets the charset used in the HTML document and used for the output. |
||
| 96 | * |
||
| 97 | * @param string $charset |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function setCharset($charset) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Gets the charset used in the HTML document and used for the output. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getCharset() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Sets an option for the generation of the mail. |
||
| 119 | * |
||
| 120 | * @param string $name |
||
| 121 | * @param mixed $value |
||
| 122 | */ |
||
| 123 | public function setOption($name, $value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Gets an option for the generation of the mail. |
||
| 143 | * |
||
| 144 | * @param string|null $name |
||
| 145 | * @return mixed |
||
| 146 | * |
||
| 147 | * @throws \InvalidArgumentException |
||
| 148 | */ |
||
| 149 | public function getOption($name = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the prepared HTML version of the mail. |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getHtml() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Gets the prepared text version of the mail. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getText() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Gets the HTML content from the preferred source. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | abstract protected function getHtmlContent(); |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Load the DOM Document |
||
| 208 | * |
||
| 209 | * @return \DOMDocument |
||
| 210 | */ |
||
| 211 | protected function loadDocument() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Prepares the mail HTML/text content. |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function prepareContent() |
||
| 230 | { |
||
| 231 | if ( ! class_exists('\DOMDocument')) |
||
| 232 | { |
||
| 233 | throw new RuntimeException("Required extension 'dom' seems to be missing."); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->loadDocument(); |
||
| 237 | |||
| 238 | $xpath = new DOMXPath($this->doc); |
||
| 239 | $stylesheet = (new Parser\StylesheetParser($this->doc))->extract(); |
||
| 240 | |||
| 241 | $parser = new Parser\RelevantSelectorParser($stylesheet); |
||
| 242 | $selectors = $parser->extract(); |
||
| 243 | |||
| 244 | foreach ($xpath->query("descendant-or-self::*[@style]") as $element) |
||
| 245 | { |
||
| 246 | if ($element->attributes !== null) |
||
| 247 | { |
||
| 248 | $styleAttribute = $element->attributes->getNamedItem("style"); |
||
| 249 | |||
| 250 | $styleValue = ""; |
||
| 251 | |||
| 252 | if ($styleAttribute !== null) |
||
| 253 | { |
||
| 254 | $styleValue = (string) $styleAttribute->nodeValue; |
||
| 255 | } |
||
| 256 | |||
| 257 | if ($styleValue !== "") |
||
| 258 | { |
||
| 259 | $element->setAttribute('data-premailer-original-style', $styleValue); |
||
| 260 | $element->removeAttribute('style'); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | // Get all specificity values (to process the declarations in the correct order, |
||
| 266 | // without sorting the array by key, which perhaps could result in a changed |
||
| 267 | // order of selectors within the specificity). |
||
| 268 | $specificities = array_keys($selectors); |
||
| 269 | sort($specificities); |
||
| 270 | |||
| 271 | // Process all style declarations in the correct order |
||
| 272 | foreach ($specificities as $specificity) |
||
| 273 | { |
||
| 274 | /** @var StyleDeclaration[] $declarations */ |
||
| 275 | foreach ($selectors[$specificity] as $selector => $declarations) |
||
| 276 | { |
||
| 277 | $xpathQuery = (new CssSelector())->toXPath($selector); |
||
| 278 | $elements = $xpath->query($xpathQuery); |
||
| 279 | |||
| 280 | foreach ($elements as $element) |
||
| 281 | { |
||
| 282 | if ($element->attributes !== null) |
||
| 283 | { |
||
| 284 | $styleAttribute = $element->attributes->getNamedItem("style"); |
||
| 285 | |||
| 286 | $styleValue = ""; |
||
| 287 | |||
| 288 | if ($styleAttribute !== null) |
||
| 289 | { |
||
| 290 | $styleValue = (string) $styleAttribute->nodeValue; |
||
| 291 | } |
||
| 292 | |||
| 293 | $concat = ($styleValue === "") ? "" : ";"; |
||
| 294 | |||
| 295 | foreach ($declarations as $declaration) |
||
| 296 | { |
||
| 297 | $styleValue .= $concat . $declaration->getProperty() . ":" . $declaration->getValue(); |
||
| 298 | $concat = ";"; |
||
| 299 | } |
||
| 300 | |||
| 301 | $element->setAttribute('style', $styleValue); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | // Add temporarily removed style attributes again, after all styles have been applied to the elements |
||
| 308 | $elements = $xpath->query("descendant-or-self::*[@data-pre-mailer-original-style]"); |
||
| 309 | /** @var \DOMElement $element */ |
||
| 310 | foreach ($elements as $element) { |
||
| 311 | if ($element->attributes !== null) { |
||
| 312 | $styleAttribute = $element->attributes->getNamedItem("style"); |
||
| 313 | $styleValue = ""; |
||
| 314 | if ($styleAttribute !== null) { |
||
| 315 | $styleValue = (string)$styleAttribute->nodeValue; |
||
| 316 | } |
||
| 317 | |||
| 318 | $originalStyleAttribute = $element->attributes->getNamedItem("data-pre-mailer-original-style"); |
||
| 319 | $originalStyleValue = ""; |
||
| 320 | if ($originalStyleAttribute !== null) { |
||
| 321 | $originalStyleValue = (string)$originalStyleAttribute->nodeValue; |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($styleValue !== "" || $originalStyleValue !== "") { |
||
| 325 | $styleValue = ($styleValue !== "" ? $styleValue . ";" : "") . $originalStyleValue; |
||
| 326 | $element->setAttribute('style', $styleValue); |
||
| 327 | $element->removeAttribute('data-pre-mailer-original-style'); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | // Optionally remove class attributes in HTML tags |
||
| 333 | $optionHtmlClasses = $this->getOption(self::OPTION_HTML_CLASSES); |
||
| 334 | if ($optionHtmlClasses === self::OPTION_HTML_CLASSES_REMOVE) { |
||
| 335 | $nodesWithClass = []; |
||
| 336 | foreach ($xpath->query('descendant-or-self::*[@class]') as $nodeWithClass) { |
||
| 337 | $nodesWithClass[] = $nodeWithClass; |
||
| 338 | } |
||
| 339 | /** @var \DOMElement $nodeWithClass */ |
||
| 340 | foreach ($nodesWithClass as $nodeWithClass) { |
||
| 341 | $nodeWithClass->removeAttribute('class'); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | // Optionally remove HTML comments |
||
| 346 | $optionHtmlComments = $this->getOption(self::OPTION_HTML_COMMENTS); |
||
| 347 | if ($optionHtmlComments === self::OPTION_HTML_COMMENTS_REMOVE) { |
||
| 348 | $commentNodes = []; |
||
| 349 | foreach ($xpath->query('//comment()') as $comment) { |
||
| 350 | $commentNodes[] = $comment; |
||
| 351 | } |
||
| 352 | foreach ($commentNodes as $commentNode) { |
||
| 353 | $commentNode->parentNode->removeChild($commentNode); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | // Write XPath document back to DOM document |
||
| 358 | $newDoc = $xpath->document; |
||
| 359 | |||
| 360 | // Generate text version (before adding the styles again) |
||
| 361 | $this->text = $this->prepareText($newDoc); |
||
| 362 | |||
| 363 | // Optionally add styles tag to the HEAD or the BODY of the document |
||
| 364 | $optionStyleTag = $this->getOption(self::OPTION_STYLE_TAG); |
||
| 365 | if ($optionStyleTag === self::OPTION_STYLE_TAG_BODY || $optionStyleTag === self::OPTION_STYLE_TAG_HEAD) { |
||
| 366 | $cssWriterClass = $this->getOption(self::OPTION_CSS_WRITER_CLASS); |
||
| 367 | /** @var WriterAbstract $cssWriter */ |
||
| 368 | $cssWriter = new $cssWriterClass($parser->getStylesheetReader()->getStyleSheet()); |
||
| 369 | $styleNode = $newDoc->createElement("style"); |
||
| 370 | $styleNode->nodeValue = $cssWriter->getContent(); |
||
| 371 | |||
| 372 | if ($optionStyleTag === self::OPTION_STYLE_TAG_BODY) { |
||
| 373 | /** @var \DOMNode $bodyNode */ |
||
| 374 | foreach($newDoc->getElementsByTagName('body') as $bodyNode) { |
||
| 375 | $bodyNode->insertBefore($styleNode, $bodyNode->firstChild); |
||
| 376 | break; |
||
| 377 | } |
||
| 378 | } elseif ($optionStyleTag === self::OPTION_STYLE_TAG_HEAD) { |
||
| 379 | /** @var \DOMNode $headNode */ |
||
| 380 | foreach($newDoc->getElementsByTagName('head') as $headNode) { |
||
| 381 | $headNode->appendChild($styleNode); |
||
| 382 | break; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | // Generate HTML version |
||
| 388 | $this->html = $newDoc->saveHTML(); |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Prepares the mail text content. |
||
| 393 | * |
||
| 394 | * @param \DOMDocument $doc |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | protected function prepareText(DOMDocument $doc) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Converts HTML tags to text, to create a text version of an HTML document. |
||
| 468 | * |
||
| 469 | * @param \DOMNodeList $nodes |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | protected function convertHtmlToText(\DOMNodeList $nodes) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Validate the scalar value of the passed option |
||
| 556 | * |
||
| 557 | * @param string $name |
||
| 558 | * @param string|int $value |
||
| 559 | * @return void |
||
| 560 | * |
||
| 561 | * @throws \InvalidArgumentException |
||
| 562 | */ |
||
| 563 | protected function validateScalarOptionValue($name, $value) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Validate the possible value of the passed option |
||
| 590 | * |
||
| 591 | * @param string $name |
||
| 592 | * @param string|int $value |
||
| 593 | * @return void |
||
| 594 | * |
||
| 595 | * @throws \InvalidArgumentException |
||
| 596 | */ |
||
| 597 | protected function validatePossibleOptionValue($name, $value) |
||
| 638 | |||
| 639 | } |
||
| 640 |