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: conditional inline style tags |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*?-->/is'; |
||
28 | |||
29 | /** |
||
30 | * regular expression: inline style tags |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
||
35 | |||
36 | /** |
||
37 | * regular expression: css-comments |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
42 | |||
43 | /** |
||
44 | * The CSS to use |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $css; |
||
49 | |||
50 | /** |
||
51 | * Should the generated HTML be cleaned |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $cleanup = false; |
||
56 | |||
57 | /** |
||
58 | * The encoding to use. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $encoding = 'UTF-8'; |
||
63 | |||
64 | /** |
||
65 | * The HTML to process |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $html; |
||
70 | |||
71 | /** |
||
72 | * Use inline-styles block as CSS |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | private $useInlineStylesBlock = false; |
||
77 | |||
78 | /** |
||
79 | * Use link block reference as CSS |
||
80 | * |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $loadCSSFromHTML = false; |
||
84 | |||
85 | /** |
||
86 | * Strip original style tags |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $stripOriginalStyleTags = false; |
||
91 | |||
92 | /** |
||
93 | * Exclude conditional inline-style blocks |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | private $excludeConditionalInlineStylesBlock = true; |
||
98 | |||
99 | /** |
||
100 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
101 | * |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $excludeMediaQueries = true; |
||
105 | |||
106 | /** |
||
107 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
108 | * later. |
||
109 | * |
||
110 | * @param null|string $html The HTML to process. |
||
111 | * @param null|string $css The CSS to use. |
||
112 | */ |
||
113 | 44 | public function __construct($html = null, $css = null) |
|
123 | |||
124 | /** |
||
125 | * Set HTML to process |
||
126 | * |
||
127 | * @param string $html The HTML to process. |
||
128 | */ |
||
129 | 42 | public function setHTML($html) |
|
134 | |||
135 | /** |
||
136 | * Set CSS to use |
||
137 | * |
||
138 | * @param string $css The CSS to use. |
||
139 | */ |
||
140 | 40 | public function setCSS($css) |
|
144 | |||
145 | /** |
||
146 | * Sort an array on the specificity element |
||
147 | * |
||
148 | * @return int |
||
149 | * |
||
150 | * @param Specificity[] $e1 The first element. |
||
151 | * @param Specificity[] $e2 The second element. |
||
152 | */ |
||
153 | 15 | private static function sortOnSpecificity($e1, $e2) |
|
165 | |||
166 | /** |
||
167 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
168 | * |
||
169 | * @return string |
||
170 | * |
||
171 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
172 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, you may also |
||
173 | * use the options parameter to specify additional Libxml |
||
174 | * parameters. Recommend these options: LIBXML_HTML_NOIMPLIED | |
||
175 | * LIBXML_HTML_NODEFDTD |
||
176 | * @param false|string [optional] Set the path to your external css-files. |
||
177 | * |
||
178 | * @throws Exception |
||
179 | */ |
||
180 | 42 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
251 | |||
252 | /** |
||
253 | * get css from inline-html style-block |
||
254 | * |
||
255 | * @param string $html |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 27 | public function getCssFromInlineHtmlStyleBlock($html) |
|
278 | |||
279 | /** |
||
280 | * Process the loaded CSS |
||
281 | * |
||
282 | * @param $css |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | 41 | private function processCSS($css) |
|
357 | |||
358 | /** |
||
359 | * @param string $css |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 41 | private function doCleanup($css) |
|
385 | |||
386 | /** |
||
387 | * remove css media queries from the string |
||
388 | * |
||
389 | * @param string $css |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | 40 | private function stripeMediaQueries($css) |
|
400 | |||
401 | /** |
||
402 | * Process the CSS-properties |
||
403 | * |
||
404 | * @return array |
||
405 | * |
||
406 | * @param string $propertyString The CSS-properties. |
||
407 | */ |
||
408 | 31 | private function processCSSProperties($propertyString) |
|
446 | |||
447 | /** |
||
448 | * Split a style string into an array of properties. |
||
449 | * The returned array can contain empty strings. |
||
450 | * |
||
451 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
452 | * |
||
453 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
454 | */ |
||
455 | 31 | private function splitIntoProperties($styles) |
|
476 | |||
477 | /** |
||
478 | * create DOMDocument from HTML |
||
479 | * |
||
480 | * @param $html |
||
481 | * |
||
482 | * @return \DOMDocument |
||
483 | */ |
||
484 | 41 | private function createDOMDocument($html, $libXMLOptions) |
|
517 | |||
518 | /** |
||
519 | * Get the encoding to use |
||
520 | * |
||
521 | * @return string |
||
522 | */ |
||
523 | 41 | private function getEncoding() |
|
527 | |||
528 | /** |
||
529 | * create XPath |
||
530 | * |
||
531 | * @param \DOMDocument $document |
||
532 | * @param array $cssRules |
||
533 | * |
||
534 | * @return \DOMXPath |
||
535 | */ |
||
536 | 41 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
628 | |||
629 | /** |
||
630 | * @param \DOMElement $element |
||
631 | * @param array $ruleProperties |
||
632 | * |
||
633 | * @return array |
||
634 | */ |
||
635 | 29 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
681 | |||
682 | /** |
||
683 | * @param array $definedProperties |
||
684 | * |
||
685 | * @return array |
||
686 | */ |
||
687 | 29 | private function splitStyleIntoChunks(array $definedProperties) |
|
717 | |||
718 | /** |
||
719 | * Strip style tags into the generated HTML |
||
720 | * |
||
721 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
722 | * |
||
723 | * @return string |
||
724 | */ |
||
725 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
747 | |||
748 | /** |
||
749 | * Remove id and class attributes. |
||
750 | * |
||
751 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
752 | * |
||
753 | * @return string |
||
754 | */ |
||
755 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
762 | |||
763 | /** |
||
764 | * Should the IDs and classes be removed? |
||
765 | * |
||
766 | * @param bool $on Should we enable cleanup? |
||
767 | */ |
||
768 | 3 | public function setCleanup($on = true) |
|
772 | |||
773 | /** |
||
774 | * Set the encoding to use with the DOMDocument |
||
775 | * |
||
776 | * @param string $encoding The encoding to use. |
||
777 | * |
||
778 | * @deprecated Doesn't have any effect |
||
779 | */ |
||
780 | public function setEncoding($encoding) |
||
784 | |||
785 | /** |
||
786 | * Set use of inline styles block |
||
787 | * If this is enabled the class will use the style-block in the HTML. |
||
788 | * |
||
789 | * @param bool $on Should we process inline styles? |
||
790 | */ |
||
791 | 25 | public function setUseInlineStylesBlock($on = true) |
|
795 | |||
796 | /** |
||
797 | * Set use of inline link block |
||
798 | * If this is enabled the class will use the links reference in the HTML. |
||
799 | * |
||
800 | * @return void |
||
801 | * @param bool [optional] $on Should we process link styles? |
||
802 | */ |
||
803 | 1 | public function setLoadCSSFromHTML($on = true) |
|
807 | |||
808 | /** |
||
809 | * Set strip original style tags |
||
810 | * If this is enabled the class will remove all style tags in the HTML. |
||
811 | * |
||
812 | * @param bool $on Should we process inline styles? |
||
813 | */ |
||
814 | 16 | public function setStripOriginalStyleTags($on = true) |
|
818 | |||
819 | /** |
||
820 | * Set exclude media queries |
||
821 | * |
||
822 | * If this is enabled the media queries will be removed before inlining the rules. |
||
823 | * |
||
824 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
825 | * |
||
826 | * @param bool $on |
||
827 | */ |
||
828 | 13 | public function setExcludeMediaQueries($on = true) |
|
832 | |||
833 | /** |
||
834 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
835 | * |
||
836 | * @param bool $on |
||
837 | */ |
||
838 | 5 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
842 | |||
843 | } |
||
844 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.