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 |
||
| 14 | class CssToInlineStyles |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var CssSelectorConverter |
||
| 19 | */ |
||
| 20 | private $cssConverter; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * regular expression: css media queries |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private static $cssMediaQueriesRegEx = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * regular expression: css charset |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private static $cssCharsetRegEx = '/@charset [\'"][^\'"]+[\'"];/i'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * regular expression: conditional inline style tags |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*-->/isU'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * regular expression: inline style tags |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private static $styleTagRegEx = '|<style(?:\s.*)?>(.*)</style>|isU'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * regular expression: style-tag with 'cleanup'-css-class |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private static $styleTagWithCleanupClassRegEx = '|<style[^>]+class="cleanup"[^>]*>.*</style>|isU'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * regular expression: css-comments |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The CSS to use |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $css; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Should the generated HTML be cleaned |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | private $cleanup = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The encoding to use. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $encoding = 'UTF-8'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The HTML to process |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $html; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Use inline-styles block as CSS |
||
| 94 | * |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | private $useInlineStylesBlock = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Use link block reference as CSS |
||
| 101 | * |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | private $loadCSSFromHTML = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Strip original style tags |
||
| 108 | * |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $stripOriginalStyleTags = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Exclude conditional inline-style blocks |
||
| 115 | * |
||
| 116 | * @var bool |
||
| 117 | */ |
||
| 118 | private $excludeConditionalInlineStylesBlock = true; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | private $excludeMediaQueries = 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 $excludeCssCharset = true; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
| 136 | * later. |
||
| 137 | * |
||
| 138 | * @param null|string $html The HTML to process. |
||
| 139 | * @param null|string $css The CSS to use. |
||
| 140 | */ |
||
| 141 | 54 | public function __construct($html = null, $css = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Set HTML to process |
||
| 158 | * |
||
| 159 | * @param string $html The HTML to process. |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | 52 | public function setHTML($html) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Set CSS to use |
||
| 173 | * |
||
| 174 | * @param string $css The CSS to use. |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | 48 | public function setCSS($css) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Sort an array on the specificity element. |
||
| 187 | * |
||
| 188 | * @param Specificity[] $e1 The first element. |
||
| 189 | * @param Specificity[] $e2 The second element. |
||
| 190 | * |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | 20 | private static function sortOnSpecificity($e1, $e2) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS. |
||
| 208 | * |
||
| 209 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
| 210 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
| 211 | * you may also use the options parameter to specify additional |
||
| 212 | * Libxml parameters. Recommend these options: |
||
| 213 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
| 214 | * @param bool $path [optional] Set the path to your external css-files. |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | * |
||
| 218 | * @throws Exception |
||
| 219 | */ |
||
| 220 | 52 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * get css from inline-html style-block |
||
| 288 | * |
||
| 289 | * @param string $html |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 31 | public function getCssFromInlineHtmlStyleBlock($html) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Process the loaded CSS |
||
| 315 | * |
||
| 316 | * @param string $css |
||
| 317 | * |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | 51 | private function processCSS($css) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $css |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 51 | private function doCleanup($css) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * remove css media queries from the string |
||
| 427 | * |
||
| 428 | * @param string $css |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 50 | private function stripeMediaQueries($css) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * remove charset from the string |
||
| 442 | * |
||
| 443 | * @param string $css |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 51 | private function stripeCharsetInCss($css) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Process the CSS-properties |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | * |
||
| 457 | * @param string $propertyString The CSS-properties. |
||
| 458 | */ |
||
| 459 | 38 | private function processCSSProperties($propertyString) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Split a style string into an array of properties. |
||
| 500 | * The returned array can contain empty strings. |
||
| 501 | * |
||
| 502 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
| 503 | * |
||
| 504 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
| 505 | */ |
||
| 506 | 38 | private function splitIntoProperties($styles) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * create XPath |
||
| 531 | * |
||
| 532 | * @param \DOMDocument $document |
||
| 533 | * @param array $cssRules |
||
| 534 | * |
||
| 535 | * @return \DOMXPath |
||
| 536 | */ |
||
| 537 | 51 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * @param \DOMElement $element |
||
| 652 | * @param array $ruleProperties |
||
| 653 | * |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | 36 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * @param array $definedProperties |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | 36 | private function splitStyleIntoChunks(array $definedProperties) |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Strip style tags into the generated HTML. |
||
| 741 | * |
||
| 742 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 743 | */ |
||
| 744 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
| 766 | |||
| 767 | /** |
||
| 768 | * Remove id and class attributes. |
||
| 769 | * |
||
| 770 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
| 771 | */ |
||
| 772 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Should the IDs and classes be removed? |
||
| 782 | * |
||
| 783 | * @param bool $on Should we enable cleanup? |
||
| 784 | * |
||
| 785 | * @return $this |
||
| 786 | */ |
||
| 787 | 3 | public function setCleanup($on = true) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Set the encoding to use with the DOMDocument. |
||
| 796 | * |
||
| 797 | * @param string $encoding The encoding to use. |
||
| 798 | * |
||
| 799 | * @return $this |
||
| 800 | * |
||
| 801 | * @deprecated Doesn't have any effect |
||
| 802 | */ |
||
| 803 | public function setEncoding($encoding) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Set use of inline styles block. |
||
| 812 | * |
||
| 813 | * Info: If this is enabled the class will use the style-block in the HTML. |
||
| 814 | * |
||
| 815 | * @param bool $on Should we process inline styles? |
||
| 816 | * |
||
| 817 | * @return $this |
||
| 818 | */ |
||
| 819 | 29 | public function setUseInlineStylesBlock($on = true) |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Set use of inline link block. |
||
| 828 | * |
||
| 829 | * Info: If this is enabled the class will use the links reference in the HTML. |
||
| 830 | * |
||
| 831 | * @param bool [optional] $on Should we process link styles? |
||
| 832 | * |
||
| 833 | * @return $this |
||
| 834 | */ |
||
| 835 | 2 | public function setLoadCSSFromHTML($on = true) |
|
| 841 | |||
| 842 | /** |
||
| 843 | * Set strip original style tags. |
||
| 844 | * |
||
| 845 | * Info: If this is enabled the class will remove all style tags in the HTML. |
||
| 846 | * |
||
| 847 | * @param bool $on Should we process inline styles? |
||
| 848 | * |
||
| 849 | * @return $this |
||
| 850 | */ |
||
| 851 | 17 | public function setStripOriginalStyleTags($on = true) |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Set exclude media queries. |
||
| 860 | * |
||
| 861 | * Info: If this is enabled the media queries will be removed before inlining the rules. |
||
| 862 | * |
||
| 863 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
| 864 | * |
||
| 865 | * @param bool $on |
||
| 866 | * |
||
| 867 | * @return $this |
||
| 868 | */ |
||
| 869 | 14 | public function setExcludeMediaQueries($on = true) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Set exclude charset. |
||
| 878 | * |
||
| 879 | * @param bool $on |
||
| 880 | * |
||
| 881 | * @return $this |
||
| 882 | */ |
||
| 883 | 1 | public function setExcludeCssCharset($on = true) |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Set exclude conditional inline-style blocks. |
||
| 892 | * |
||
| 893 | * e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
| 894 | * |
||
| 895 | * @param bool $on |
||
| 896 | * |
||
| 897 | * @return $this |
||
| 898 | */ |
||
| 899 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
| 905 | } |
||
| 906 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.