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 | * regular expression: conditional inline style tags |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*-->/isU'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * regular expression: inline style tags |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $domLinkReplaceHelper = array( |
||
| 47 | 'orig' => array('[', ']', '{', '}',), |
||
| 48 | 'tmp' => array( |
||
| 49 | '!!!!SQUARE_BRACKET_LEFT!!!!', |
||
| 50 | '!!!!SQUARE_BRACKET_RIGHT!!!!', |
||
| 51 | '!!!!BRACKET_LEFT!!!!', |
||
| 52 | '!!!!BRACKET_RIGHT!!!!', |
||
| 53 | ), |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected static $domReplaceHelper = array( |
||
| 60 | 'orig' => array('&'), |
||
| 61 | 'tmp' => array('!!!!AMP!!!!'), |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * regular expression: css-comments |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The CSS to use |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $css; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Should the generated HTML be cleaned |
||
| 80 | * |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $cleanup = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The encoding to use. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $encoding = 'UTF-8'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The HTML to process |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $html; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Use inline-styles block as CSS |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $useInlineStylesBlock = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Use link block reference as CSS |
||
| 108 | * |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $loadCSSFromHTML = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Strip original style tags |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | private $stripOriginalStyleTags = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Exclude conditional inline-style blocks |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | private $excludeConditionalInlineStylesBlock = true; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
| 129 | * |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | private $excludeMediaQueries = true; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
| 136 | * |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | private $excludeCssCharset = true; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
| 143 | * later. |
||
| 144 | * |
||
| 145 | * @param null|string $html The HTML to process. |
||
| 146 | * @param null|string $css The CSS to use. |
||
| 147 | */ |
||
| 148 | 47 | public function __construct($html = null, $css = null) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Set HTML to process |
||
| 161 | * |
||
| 162 | * @param string $html The HTML to process. |
||
| 163 | */ |
||
| 164 | 45 | public function setHTML($html) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set CSS to use |
||
| 172 | * |
||
| 173 | * @param string $css The CSS to use. |
||
| 174 | */ |
||
| 175 | 43 | public function setCSS($css) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Sort an array on the specificity element |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | * |
||
| 185 | * @param Specificity[] $e1 The first element. |
||
| 186 | * @param Specificity[] $e2 The second element. |
||
| 187 | */ |
||
| 188 | 16 | private static function sortOnSpecificity($e1, $e2) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
| 203 | * |
||
| 204 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
| 205 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
| 206 | * you may also use the options parameter to specify additional |
||
| 207 | * Libxml parameters. Recommend these options: |
||
| 208 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
| 209 | * @param bool $path [optional] Set the path to your external css-files. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | * |
||
| 213 | * @throws Exception |
||
| 214 | */ |
||
| 215 | 45 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * get css from inline-html style-block |
||
| 295 | * |
||
| 296 | * @param string $html |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 28 | public function getCssFromInlineHtmlStyleBlock($html) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Process the loaded CSS |
||
| 322 | * |
||
| 323 | * @param $css |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 44 | private function processCSS($css) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $css |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | 44 | private function doCleanup($css) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * remove css media queries from the string |
||
| 434 | * |
||
| 435 | * @param string $css |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | 43 | private function stripeMediaQueries($css) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * remove charset from the string |
||
| 449 | * |
||
| 450 | * @param $css |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 44 | private function stripeCharsetInCss($css) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Process the CSS-properties |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | * |
||
| 464 | * @param string $propertyString The CSS-properties. |
||
| 465 | */ |
||
| 466 | 33 | private function processCSSProperties($propertyString) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Split a style string into an array of properties. |
||
| 507 | * The returned array can contain empty strings. |
||
| 508 | * |
||
| 509 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
| 510 | * |
||
| 511 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
| 512 | */ |
||
| 513 | 33 | private function splitIntoProperties($styles) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * create DOMDocument from HTML |
||
| 538 | * |
||
| 539 | * @param string $html |
||
| 540 | * @param int $libXMLOptions |
||
| 541 | * |
||
| 542 | * @return \DOMDocument |
||
| 543 | */ |
||
| 544 | 44 | private function createDOMDocument($html, $libXMLOptions = 0) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Get the encoding to use |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | 44 | private function getEncoding() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * create XPath |
||
| 603 | * |
||
| 604 | * @param \DOMDocument $document |
||
| 605 | * @param array $cssRules |
||
| 606 | * |
||
| 607 | * @return \DOMXPath |
||
| 608 | */ |
||
| 609 | 44 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
| 703 | |||
| 704 | /** |
||
| 705 | * @param \DOMElement $element |
||
| 706 | * @param array $ruleProperties |
||
| 707 | * |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | 31 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
| 757 | |||
| 758 | /** |
||
| 759 | * @param array $definedProperties |
||
| 760 | * |
||
| 761 | * @return array |
||
| 762 | */ |
||
| 763 | 31 | private function splitStyleIntoChunks(array $definedProperties) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Strip style tags into the generated HTML |
||
| 796 | * |
||
| 797 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 798 | * |
||
| 799 | * @return string |
||
| 800 | */ |
||
| 801 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
| 823 | |||
| 824 | /** |
||
| 825 | * Remove id and class attributes. |
||
| 826 | * |
||
| 827 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 828 | * |
||
| 829 | * @return string |
||
| 830 | */ |
||
| 831 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Should the IDs and classes be removed? |
||
| 841 | * |
||
| 842 | * @param bool $on Should we enable cleanup? |
||
| 843 | */ |
||
| 844 | 3 | public function setCleanup($on = true) |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Set the encoding to use with the DOMDocument |
||
| 851 | * |
||
| 852 | * @param string $encoding The encoding to use. |
||
| 853 | * |
||
| 854 | * @deprecated Doesn't have any effect |
||
| 855 | */ |
||
| 856 | public function setEncoding($encoding) |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Set use of inline styles block |
||
| 863 | * If this is enabled the class will use the style-block in the HTML. |
||
| 864 | * |
||
| 865 | * @param bool $on Should we process inline styles? |
||
| 866 | */ |
||
| 867 | 26 | public function setUseInlineStylesBlock($on = true) |
|
| 871 | |||
| 872 | /** |
||
| 873 | * Set use of inline link block |
||
| 874 | * If this is enabled the class will use the links reference in the HTML. |
||
| 875 | * |
||
| 876 | * @return void |
||
| 877 | * |
||
| 878 | * @param bool [optional] $on Should we process link styles? |
||
| 879 | */ |
||
| 880 | 2 | public function setLoadCSSFromHTML($on = true) |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Set strip original style tags |
||
| 887 | * If this is enabled the class will remove all style tags in the HTML. |
||
| 888 | * |
||
| 889 | * @param bool $on Should we process inline styles? |
||
| 890 | */ |
||
| 891 | 17 | public function setStripOriginalStyleTags($on = true) |
|
| 895 | |||
| 896 | /** |
||
| 897 | * Set exclude media queries |
||
| 898 | * |
||
| 899 | * If this is enabled the media queries will be removed before inlining the rules. |
||
| 900 | * |
||
| 901 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
| 902 | * |
||
| 903 | * @param bool $on |
||
| 904 | */ |
||
| 905 | 14 | public function setExcludeMediaQueries($on = true) |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Set exclude charset |
||
| 912 | * |
||
| 913 | * @param bool $on |
||
| 914 | */ |
||
| 915 | 1 | public function setExcludeCssCharset($on = true) |
|
| 919 | |||
| 920 | /** |
||
| 921 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
| 922 | * |
||
| 923 | * @param bool $on |
||
| 924 | */ |
||
| 925 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
| 929 | |||
| 930 | /** |
||
| 931 | * @param string $html |
||
| 932 | * |
||
| 933 | * @return string |
||
| 934 | */ |
||
| 935 | 44 | private function replaceToPreserveHtmlEntities($html) |
|
| 962 | |||
| 963 | /** |
||
| 964 | * @param string $html |
||
| 965 | * |
||
| 966 | * @return string |
||
| 967 | */ |
||
| 968 | 44 | private function putReplacedBackToPreserveHtmlEntities($html) |
|
| 976 | } |
||
| 977 |