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 | 47 | 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 | 45 | public function setHTML($html) |
|
149 | |||
150 | /** |
||
151 | * Set CSS to use |
||
152 | * |
||
153 | * @param string $css The CSS to use. |
||
154 | */ |
||
155 | 43 | 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 | 16 | 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 | 45 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
272 | |||
273 | /** |
||
274 | * get css from inline-html style-block |
||
275 | * |
||
276 | * @param string $html |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 28 | public function getCssFromInlineHtmlStyleBlock($html) |
|
299 | |||
300 | /** |
||
301 | * Process the loaded CSS |
||
302 | * |
||
303 | * @param $css |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | 44 | private function processCSS($css) |
|
378 | |||
379 | /** |
||
380 | * @param string $css |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 44 | private function doCleanup($css) |
|
411 | |||
412 | /** |
||
413 | * remove css media queries from the string |
||
414 | * |
||
415 | * @param string $css |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | 43 | private function stripeMediaQueries($css) |
|
426 | |||
427 | /** |
||
428 | * remove charset from the string |
||
429 | * |
||
430 | * @param $css |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | 44 | private function stripeCharsetInCss($css) |
|
438 | |||
439 | /** |
||
440 | * Process the CSS-properties |
||
441 | * |
||
442 | * @return array |
||
443 | * |
||
444 | * @param string $propertyString The CSS-properties. |
||
445 | */ |
||
446 | 33 | private function processCSSProperties($propertyString) |
|
484 | |||
485 | /** |
||
486 | * Split a style string into an array of properties. |
||
487 | * The returned array can contain empty strings. |
||
488 | * |
||
489 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
490 | * |
||
491 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
492 | */ |
||
493 | 33 | private function splitIntoProperties($styles) |
|
515 | |||
516 | /** |
||
517 | * create DOMDocument from HTML |
||
518 | * |
||
519 | * @param string $html |
||
520 | * @param int $libXMLOptions |
||
521 | * |
||
522 | * @return \DOMDocument |
||
523 | */ |
||
524 | 44 | private function createDOMDocument($html, $libXMLOptions = 0) |
|
570 | |||
571 | /** |
||
572 | * Get the encoding to use |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | 44 | private function getEncoding() |
|
580 | |||
581 | /** |
||
582 | * create XPath |
||
583 | * |
||
584 | * @param \DOMDocument $document |
||
585 | * @param array $cssRules |
||
586 | * |
||
587 | * @return \DOMXPath |
||
588 | */ |
||
589 | 44 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
683 | |||
684 | /** |
||
685 | * @param \DOMElement $element |
||
686 | * @param array $ruleProperties |
||
687 | * |
||
688 | * @return array |
||
689 | */ |
||
690 | 31 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
737 | |||
738 | /** |
||
739 | * @param array $definedProperties |
||
740 | * |
||
741 | * @return array |
||
742 | */ |
||
743 | 31 | private function splitStyleIntoChunks(array $definedProperties) |
|
773 | |||
774 | /** |
||
775 | * Strip style tags into the generated HTML |
||
776 | * |
||
777 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
778 | * |
||
779 | * @return string |
||
780 | */ |
||
781 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
803 | |||
804 | /** |
||
805 | * Remove id and class attributes. |
||
806 | * |
||
807 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
808 | * |
||
809 | * @return string |
||
810 | */ |
||
811 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
818 | |||
819 | /** |
||
820 | * Should the IDs and classes be removed? |
||
821 | * |
||
822 | * @param bool $on Should we enable cleanup? |
||
823 | */ |
||
824 | 3 | public function setCleanup($on = true) |
|
828 | |||
829 | /** |
||
830 | * Set the encoding to use with the DOMDocument |
||
831 | * |
||
832 | * @param string $encoding The encoding to use. |
||
833 | * |
||
834 | * @deprecated Doesn't have any effect |
||
835 | */ |
||
836 | public function setEncoding($encoding) |
||
840 | |||
841 | /** |
||
842 | * Set use of inline styles block |
||
843 | * If this is enabled the class will use the style-block in the HTML. |
||
844 | * |
||
845 | * @param bool $on Should we process inline styles? |
||
846 | */ |
||
847 | 26 | public function setUseInlineStylesBlock($on = true) |
|
851 | |||
852 | /** |
||
853 | * Set use of inline link block |
||
854 | * If this is enabled the class will use the links reference in the HTML. |
||
855 | * |
||
856 | * @return void |
||
857 | * |
||
858 | * @param bool [optional] $on Should we process link styles? |
||
859 | */ |
||
860 | 2 | public function setLoadCSSFromHTML($on = true) |
|
864 | |||
865 | /** |
||
866 | * Set strip original style tags |
||
867 | * If this is enabled the class will remove all style tags in the HTML. |
||
868 | * |
||
869 | * @param bool $on Should we process inline styles? |
||
870 | */ |
||
871 | 17 | public function setStripOriginalStyleTags($on = true) |
|
875 | |||
876 | /** |
||
877 | * Set exclude media queries |
||
878 | * |
||
879 | * If this is enabled the media queries will be removed before inlining the rules. |
||
880 | * |
||
881 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
882 | * |
||
883 | * @param bool $on |
||
884 | */ |
||
885 | 14 | public function setExcludeMediaQueries($on = true) |
|
889 | |||
890 | /** |
||
891 | * Set exclude charset |
||
892 | * |
||
893 | * @param bool $on |
||
894 | */ |
||
895 | 1 | public function setExcludeCssCharset($on = true) |
|
899 | |||
900 | /** |
||
901 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
902 | * |
||
903 | * @param bool $on |
||
904 | */ |
||
905 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
909 | |||
910 | /** |
||
911 | * @param string $html |
||
912 | * @return string |
||
913 | */ |
||
914 | 44 | private function replaceAmperstampToPreserveHtmlEntities($html) |
|
918 | |||
919 | /** |
||
920 | * @param string $html |
||
921 | * @return string |
||
922 | */ |
||
923 | 44 | private function putReplacedAmperstampBackToPreserveHtmlEntities($html) |
|
927 | } |
||
928 |