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 | * regular expression: css media queries |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private static $cssMediaQueriesRegEx = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU'; |
||
23 | |||
24 | /** |
||
25 | * regular expression: css charset |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private static $cssCharsetRegEx = '/@charset [\'"][^\'"]+[\'"];/i'; |
||
30 | |||
31 | /** |
||
32 | * regular expression: conditional inline style tags |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*-->/isU'; |
||
37 | |||
38 | /** |
||
39 | * regular expression: inline style tags |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
||
44 | |||
45 | /** |
||
46 | * regular expression: css-comments |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
51 | |||
52 | /** |
||
53 | * The CSS to use |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | private $css; |
||
58 | |||
59 | /** |
||
60 | * Should the generated HTML be cleaned |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $cleanup = false; |
||
65 | |||
66 | /** |
||
67 | * The encoding to use. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $encoding = 'UTF-8'; |
||
72 | |||
73 | /** |
||
74 | * The HTML to process |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | private $html; |
||
79 | |||
80 | /** |
||
81 | * Use inline-styles block as CSS |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $useInlineStylesBlock = false; |
||
86 | |||
87 | /** |
||
88 | * Use link block reference as CSS |
||
89 | * |
||
90 | * @var bool |
||
91 | */ |
||
92 | private $loadCSSFromHTML = false; |
||
93 | |||
94 | /** |
||
95 | * Strip original style tags |
||
96 | * |
||
97 | * @var bool |
||
98 | */ |
||
99 | private $stripOriginalStyleTags = false; |
||
100 | |||
101 | /** |
||
102 | * Exclude conditional inline-style blocks |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | private $excludeConditionalInlineStylesBlock = true; |
||
107 | |||
108 | /** |
||
109 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
110 | * |
||
111 | * @var bool |
||
112 | */ |
||
113 | private $excludeMediaQueries = true; |
||
114 | |||
115 | /** |
||
116 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
117 | * |
||
118 | * @var bool |
||
119 | */ |
||
120 | private $excludeCssCharset = true; |
||
121 | |||
122 | /** |
||
123 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
124 | * later. |
||
125 | * |
||
126 | * @param null|string $html The HTML to process. |
||
127 | * @param null|string $css The CSS to use. |
||
128 | */ |
||
129 | 48 | public function __construct($html = null, $css = null) |
|
139 | |||
140 | /** |
||
141 | * Set HTML to process |
||
142 | * |
||
143 | * @param string $html The HTML to process. |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | 46 | public function setHTML($html) |
|
154 | |||
155 | /** |
||
156 | * Set CSS to use |
||
157 | * |
||
158 | * @param string $css The CSS to use. |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | 44 | public function setCSS($css) |
|
168 | |||
169 | /** |
||
170 | * Sort an array on the specificity element |
||
171 | * |
||
172 | * @return int |
||
173 | * |
||
174 | * @param Specificity[] $e1 The first element. |
||
175 | * @param Specificity[] $e2 The second element. |
||
176 | */ |
||
177 | 17 | private static function sortOnSpecificity($e1, $e2) |
|
189 | |||
190 | /** |
||
191 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
192 | * |
||
193 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
194 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
195 | * you may also use the options parameter to specify additional |
||
196 | * Libxml parameters. Recommend these options: |
||
197 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
198 | * @param bool $path [optional] Set the path to your external css-files. |
||
199 | * |
||
200 | * @return string |
||
201 | * |
||
202 | * @throws Exception |
||
203 | */ |
||
204 | 46 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
270 | |||
271 | /** |
||
272 | * get css from inline-html style-block |
||
273 | * |
||
274 | * @param string $html |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 29 | public function getCssFromInlineHtmlStyleBlock($html) |
|
297 | |||
298 | /** |
||
299 | * Process the loaded CSS |
||
300 | * |
||
301 | * @param string $css |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | 45 | private function processCSS($css) |
|
376 | |||
377 | /** |
||
378 | * @param string $css |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | 45 | private function doCleanup($css) |
|
409 | |||
410 | /** |
||
411 | * remove css media queries from the string |
||
412 | * |
||
413 | * @param string $css |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | 44 | private function stripeMediaQueries($css) |
|
424 | |||
425 | /** |
||
426 | * remove charset from the string |
||
427 | * |
||
428 | * @param string $css |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | 45 | private function stripeCharsetInCss($css) |
|
436 | |||
437 | /** |
||
438 | * Process the CSS-properties |
||
439 | * |
||
440 | * @return array |
||
441 | * |
||
442 | * @param string $propertyString The CSS-properties. |
||
443 | */ |
||
444 | 34 | private function processCSSProperties($propertyString) |
|
482 | |||
483 | /** |
||
484 | * Split a style string into an array of properties. |
||
485 | * The returned array can contain empty strings. |
||
486 | * |
||
487 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
488 | * |
||
489 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
490 | */ |
||
491 | 34 | private function splitIntoProperties($styles) |
|
513 | |||
514 | /** |
||
515 | * create XPath |
||
516 | * |
||
517 | * @param \DOMDocument $document |
||
518 | * @param array $cssRules |
||
519 | * |
||
520 | * @return \DOMXPath |
||
521 | */ |
||
522 | 45 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
639 | |||
640 | /** |
||
641 | * @param \DOMElement $element |
||
642 | * @param array $ruleProperties |
||
643 | * |
||
644 | * @return string |
||
645 | */ |
||
646 | 32 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
693 | |||
694 | /** |
||
695 | * @param array $definedProperties |
||
696 | * |
||
697 | * @return array |
||
698 | */ |
||
699 | 32 | private function splitStyleIntoChunks(array $definedProperties) |
|
729 | |||
730 | /** |
||
731 | * Strip style tags into the generated HTML. |
||
732 | * |
||
733 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
734 | * |
||
735 | * @return string |
||
736 | */ |
||
737 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
759 | |||
760 | /** |
||
761 | * Remove id and class attributes. |
||
762 | * |
||
763 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
764 | * |
||
765 | * @return string |
||
766 | */ |
||
767 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
774 | |||
775 | /** |
||
776 | * Should the IDs and classes be removed? |
||
777 | * |
||
778 | * @param bool $on Should we enable cleanup? |
||
779 | * |
||
780 | * @return $this |
||
781 | */ |
||
782 | 3 | public function setCleanup($on = true) |
|
788 | |||
789 | /** |
||
790 | * Set the encoding to use with the DOMDocument |
||
791 | * |
||
792 | * @param string $encoding The encoding to use. |
||
793 | * |
||
794 | * @return $this |
||
795 | * |
||
796 | * @deprecated Doesn't have any effect |
||
797 | */ |
||
798 | public function setEncoding($encoding) |
||
804 | |||
805 | /** |
||
806 | * Set use of inline styles block. |
||
807 | * |
||
808 | * Info: If this is enabled the class will use the style-block in the HTML. |
||
809 | * |
||
810 | * @param bool $on Should we process inline styles? |
||
811 | * |
||
812 | * @return $this |
||
813 | */ |
||
814 | 27 | public function setUseInlineStylesBlock($on = true) |
|
820 | |||
821 | /** |
||
822 | * Set use of inline link block. |
||
823 | * |
||
824 | * Info: If this is enabled the class will use the links reference in the HTML. |
||
825 | * |
||
826 | * @param bool [optional] $on Should we process link styles? |
||
827 | * |
||
828 | * @return $this |
||
829 | */ |
||
830 | 2 | public function setLoadCSSFromHTML($on = true) |
|
836 | |||
837 | /** |
||
838 | * Set strip original style tags. |
||
839 | * |
||
840 | * Info: If this is enabled the class will remove all style tags in the HTML. |
||
841 | * |
||
842 | * @param bool $on Should we process inline styles? |
||
843 | * |
||
844 | * @return $this |
||
845 | */ |
||
846 | 17 | public function setStripOriginalStyleTags($on = true) |
|
852 | |||
853 | /** |
||
854 | * Set exclude media queries. |
||
855 | * |
||
856 | * Info: If this is enabled the media queries will be removed before inlining the rules. |
||
857 | * |
||
858 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
859 | * |
||
860 | * @param bool $on |
||
861 | * |
||
862 | * @return $this |
||
863 | */ |
||
864 | 14 | public function setExcludeMediaQueries($on = true) |
|
870 | |||
871 | /** |
||
872 | * Set exclude charset. |
||
873 | * |
||
874 | * @param bool $on |
||
875 | * |
||
876 | * @return $this |
||
877 | */ |
||
878 | 1 | public function setExcludeCssCharset($on = true) |
|
884 | |||
885 | /** |
||
886 | * Set exclude conditional inline-style blocks. |
||
887 | * |
||
888 | * e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
889 | * |
||
890 | * @param bool $on |
||
891 | * |
||
892 | * @return $this |
||
893 | */ |
||
894 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
900 | } |
||
901 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.