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.*?-->/isU'; |
||
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 | 46 | 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 | 44 | public function setHTML($html) |
|
149 | |||
150 | /** |
||
151 | * Set CSS to use |
||
152 | * |
||
153 | * @param string $css The CSS to use. |
||
154 | */ |
||
155 | 42 | 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 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
185 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
186 | * you may also use the options parameter to specify additional |
||
187 | * Libxml parameters. Recommend these options: |
||
188 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
189 | * @param bool $path [optional] Set the path to your external css-files. |
||
190 | * |
||
191 | * @return string |
||
192 | * |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | 44 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
268 | |||
269 | /** |
||
270 | * get css from inline-html style-block |
||
271 | * |
||
272 | * @param string $html |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | 28 | public function getCssFromInlineHtmlStyleBlock($html) |
|
295 | |||
296 | /** |
||
297 | * Process the loaded CSS |
||
298 | * |
||
299 | * @param $css |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | 43 | private function processCSS($css) |
|
374 | |||
375 | /** |
||
376 | * @param string $css |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | 43 | private function doCleanup($css) |
|
407 | |||
408 | /** |
||
409 | * remove css media queries from the string |
||
410 | * |
||
411 | * @param string $css |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | 42 | private function stripeMediaQueries($css) |
|
422 | |||
423 | /** |
||
424 | * remove charset from the string |
||
425 | * |
||
426 | * @param $css |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | 43 | private function stripeCharsetInCss($css) |
|
434 | |||
435 | /** |
||
436 | * Process the CSS-properties |
||
437 | * |
||
438 | * @return array |
||
439 | * |
||
440 | * @param string $propertyString The CSS-properties. |
||
441 | */ |
||
442 | 32 | private function processCSSProperties($propertyString) |
|
480 | |||
481 | /** |
||
482 | * Split a style string into an array of properties. |
||
483 | * The returned array can contain empty strings. |
||
484 | * |
||
485 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
486 | * |
||
487 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
488 | */ |
||
489 | 32 | private function splitIntoProperties($styles) |
|
511 | |||
512 | /** |
||
513 | * create DOMDocument from HTML |
||
514 | * |
||
515 | * @param string $html |
||
516 | * @param int $libXMLOptions |
||
517 | * |
||
518 | * @return \DOMDocument |
||
519 | */ |
||
520 | 43 | private function createDOMDocument($html, $libXMLOptions = 0) |
|
558 | |||
559 | /** |
||
560 | * Get the encoding to use |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | 43 | private function getEncoding() |
|
568 | |||
569 | /** |
||
570 | * create XPath |
||
571 | * |
||
572 | * @param \DOMDocument $document |
||
573 | * @param array $cssRules |
||
574 | * |
||
575 | * @return \DOMXPath |
||
576 | */ |
||
577 | 43 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
671 | |||
672 | /** |
||
673 | * @param \DOMElement $element |
||
674 | * @param array $ruleProperties |
||
675 | * |
||
676 | * @return array |
||
677 | */ |
||
678 | 30 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
725 | |||
726 | /** |
||
727 | * @param array $definedProperties |
||
728 | * |
||
729 | * @return array |
||
730 | */ |
||
731 | 30 | private function splitStyleIntoChunks(array $definedProperties) |
|
761 | |||
762 | /** |
||
763 | * Strip style tags into the generated HTML |
||
764 | * |
||
765 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
766 | * |
||
767 | * @return string |
||
768 | */ |
||
769 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
791 | |||
792 | /** |
||
793 | * Remove id and class attributes. |
||
794 | * |
||
795 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
796 | * |
||
797 | * @return string |
||
798 | */ |
||
799 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
806 | |||
807 | /** |
||
808 | * Should the IDs and classes be removed? |
||
809 | * |
||
810 | * @param bool $on Should we enable cleanup? |
||
811 | */ |
||
812 | 3 | public function setCleanup($on = true) |
|
816 | |||
817 | /** |
||
818 | * Set the encoding to use with the DOMDocument |
||
819 | * |
||
820 | * @param string $encoding The encoding to use. |
||
821 | * |
||
822 | * @deprecated Doesn't have any effect |
||
823 | */ |
||
824 | public function setEncoding($encoding) |
||
828 | |||
829 | /** |
||
830 | * Set use of inline styles block |
||
831 | * If this is enabled the class will use the style-block in the HTML. |
||
832 | * |
||
833 | * @param bool $on Should we process inline styles? |
||
834 | */ |
||
835 | 26 | public function setUseInlineStylesBlock($on = true) |
|
839 | |||
840 | /** |
||
841 | * Set use of inline link block |
||
842 | * If this is enabled the class will use the links reference in the HTML. |
||
843 | * |
||
844 | * @return void |
||
845 | * |
||
846 | * @param bool [optional] $on Should we process link styles? |
||
847 | */ |
||
848 | 2 | public function setLoadCSSFromHTML($on = true) |
|
852 | |||
853 | /** |
||
854 | * Set strip original style tags |
||
855 | * If this is enabled the class will remove all style tags in the HTML. |
||
856 | * |
||
857 | * @param bool $on Should we process inline styles? |
||
858 | */ |
||
859 | 17 | public function setStripOriginalStyleTags($on = true) |
|
863 | |||
864 | /** |
||
865 | * Set exclude media queries |
||
866 | * |
||
867 | * If this is enabled the media queries will be removed before inlining the rules. |
||
868 | * |
||
869 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
870 | * |
||
871 | * @param bool $on |
||
872 | */ |
||
873 | 14 | public function setExcludeMediaQueries($on = true) |
|
877 | |||
878 | /** |
||
879 | * Set exclude charset |
||
880 | * |
||
881 | * @param bool $on |
||
882 | */ |
||
883 | 1 | public function setExcludeCssCharset($on = true) |
|
887 | |||
888 | /** |
||
889 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
890 | * |
||
891 | * @param bool $on |
||
892 | */ |
||
893 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
897 | |||
898 | } |
||
899 |