Complex classes like CssToInlineStyles 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 CssToInlineStyles, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CssToInlineStyles |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * regular expression: css media queries |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private static $cssMediaQueriesRegEx = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * regular expression: css charset |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private static $cssCharsetRegEx = '/@charset [\'"][^\'"]+[\'"];/i'; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * regular expression: conditional inline style tags |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*?-->/is'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * regular expression: inline style tags |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * regular expression: css-comments |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The CSS to use |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $css; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Should the generated HTML be cleaned |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | private $cleanup = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The encoding to use. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $encoding = 'UTF-8'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The HTML to process |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $html; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Use inline-styles block as CSS |
||
| 81 | * |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $useInlineStylesBlock = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Use link block reference as CSS |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private $loadCSSFromHTML = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Strip original style tags |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | private $stripOriginalStyleTags = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Exclude conditional inline-style blocks |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | private $excludeConditionalInlineStylesBlock = true; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
| 109 | * |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | private $excludeMediaQueries = true; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
| 116 | * |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | private $excludeCssCharset = true; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
| 123 | * later. |
||
| 124 | * |
||
| 125 | * @param null|string $html The HTML to process. |
||
| 126 | * @param null|string $css The CSS to use. |
||
| 127 | */ |
||
| 128 | 44 | public function __construct($html = null, $css = null) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Set HTML to process |
||
| 141 | * |
||
| 142 | * @param string $html The HTML to process. |
||
| 143 | */ |
||
| 144 | 42 | public function setHTML($html) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Set CSS to use |
||
| 152 | * |
||
| 153 | * @param string $css The CSS to use. |
||
| 154 | */ |
||
| 155 | 40 | public function setCSS($css) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Sort an array on the specificity element |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | * |
||
| 165 | * @param Specificity[] $e1 The first element. |
||
| 166 | * @param Specificity[] $e2 The second element. |
||
| 167 | */ |
||
| 168 | 15 | private static function sortOnSpecificity($e1, $e2) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | * |
||
| 186 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
| 187 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, you may also |
||
| 188 | * use the options parameter to specify additional Libxml |
||
| 189 | * parameters. Recommend these options: LIBXML_HTML_NOIMPLIED | |
||
| 190 | * LIBXML_HTML_NODEFDTD |
||
| 191 | * @param $path false|string [optional] Set the path to your external css-files. |
||
| 192 | * |
||
| 193 | * @throws Exception |
||
| 194 | */ |
||
| 195 | 42 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * get css from inline-html style-block |
||
| 269 | * |
||
| 270 | * @param string $html |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | 27 | public function getCssFromInlineHtmlStyleBlock($html) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Process the loaded CSS |
||
| 296 | * |
||
| 297 | * @param $css |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 41 | private function processCSS($css) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $css |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | 41 | private function doCleanup($css) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * remove css media queries from the string |
||
| 408 | * |
||
| 409 | * @param string $css |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | 40 | private function stripeMediaQueries($css) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * remove charset from the string |
||
| 423 | * |
||
| 424 | * @param $css |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 41 | private function stripeCharsetInCss($css) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Process the CSS-properties |
||
| 435 | * |
||
| 436 | * @return array |
||
| 437 | * |
||
| 438 | * @param string $propertyString The CSS-properties. |
||
| 439 | */ |
||
| 440 | 31 | private function processCSSProperties($propertyString) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Split a style string into an array of properties. |
||
| 481 | * The returned array can contain empty strings. |
||
| 482 | * |
||
| 483 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
| 484 | * |
||
| 485 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
| 486 | */ |
||
| 487 | 31 | private function splitIntoProperties($styles) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * create DOMDocument from HTML |
||
| 511 | * |
||
| 512 | * @param string $html |
||
| 513 | * @param int $libXMLOptions |
||
| 514 | * |
||
| 515 | * @return \DOMDocument |
||
| 516 | */ |
||
| 517 | 41 | private function createDOMDocument($html, $libXMLOptions = 0) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Get the encoding to use |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | 41 | private function getEncoding() |
|
| 565 | |||
| 566 | /** |
||
| 567 | * create XPath |
||
| 568 | * |
||
| 569 | * @param \DOMDocument $document |
||
| 570 | * @param array $cssRules |
||
| 571 | * |
||
| 572 | * @return \DOMXPath |
||
| 573 | */ |
||
| 574 | 41 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @param \DOMElement $element |
||
| 669 | * @param array $ruleProperties |
||
| 670 | * |
||
| 671 | * @return array |
||
| 672 | */ |
||
| 673 | 29 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * @param array $definedProperties |
||
| 722 | * |
||
| 723 | * @return array |
||
| 724 | */ |
||
| 725 | 29 | private function splitStyleIntoChunks(array $definedProperties) |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Strip style tags into the generated HTML |
||
| 758 | * |
||
| 759 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 760 | * |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Remove id and class attributes. |
||
| 788 | * |
||
| 789 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 790 | * |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Should the IDs and classes be removed? |
||
| 803 | * |
||
| 804 | * @param bool $on Should we enable cleanup? |
||
| 805 | */ |
||
| 806 | 3 | public function setCleanup($on = true) |
|
| 810 | |||
| 811 | /** |
||
| 812 | * Set the encoding to use with the DOMDocument |
||
| 813 | * |
||
| 814 | * @param string $encoding The encoding to use. |
||
| 815 | * |
||
| 816 | * @deprecated Doesn't have any effect |
||
| 817 | */ |
||
| 818 | public function setEncoding($encoding) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Set use of inline styles block |
||
| 825 | * If this is enabled the class will use the style-block in the HTML. |
||
| 826 | * |
||
| 827 | * @param bool $on Should we process inline styles? |
||
| 828 | */ |
||
| 829 | 25 | public function setUseInlineStylesBlock($on = true) |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Set use of inline link block |
||
| 836 | * If this is enabled the class will use the links reference in the HTML. |
||
| 837 | * |
||
| 838 | * @return void |
||
| 839 | * @param bool [optional] $on Should we process link styles? |
||
| 840 | */ |
||
| 841 | 1 | public function setLoadCSSFromHTML($on = true) |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Set strip original style tags |
||
| 848 | * If this is enabled the class will remove all style tags in the HTML. |
||
| 849 | * |
||
| 850 | * @param bool $on Should we process inline styles? |
||
| 851 | */ |
||
| 852 | 16 | public function setStripOriginalStyleTags($on = true) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Set exclude media queries |
||
| 859 | * |
||
| 860 | * If this is enabled the media queries will be removed before inlining the rules. |
||
| 861 | * |
||
| 862 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
| 863 | * |
||
| 864 | * @param bool $on |
||
| 865 | */ |
||
| 866 | 13 | public function setExcludeMediaQueries($on = true) |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Set exclude charset |
||
| 873 | * |
||
| 874 | * @param bool $on |
||
| 875 | */ |
||
| 876 | 1 | public function setExcludeCssCharset($on = true) |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
| 883 | * |
||
| 884 | * @param bool $on |
||
| 885 | */ |
||
| 886 | 5 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
| 890 | |||
| 891 | } |
||
| 892 |