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(.*)>(.*)</style>|isU'; |
||
49 | |||
50 | /** |
||
51 | * regular expression: css-comments |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
56 | |||
57 | /** |
||
58 | * The CSS to use |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $css; |
||
63 | |||
64 | /** |
||
65 | * Should the generated HTML be cleaned |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $cleanup = false; |
||
70 | |||
71 | /** |
||
72 | * The encoding to use. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $encoding = 'UTF-8'; |
||
77 | |||
78 | /** |
||
79 | * The HTML to process |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $html; |
||
84 | |||
85 | /** |
||
86 | * Use inline-styles block as CSS |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $useInlineStylesBlock = false; |
||
91 | |||
92 | /** |
||
93 | * Use link block reference as CSS |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | private $loadCSSFromHTML = false; |
||
98 | |||
99 | /** |
||
100 | * Strip original style tags |
||
101 | * |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $stripOriginalStyleTags = false; |
||
105 | |||
106 | /** |
||
107 | * Exclude conditional inline-style blocks |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | private $excludeConditionalInlineStylesBlock = true; |
||
112 | |||
113 | /** |
||
114 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $excludeMediaQueries = 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 $excludeCssCharset = true; |
||
126 | |||
127 | /** |
||
128 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
129 | * later. |
||
130 | * |
||
131 | * @param null|string $html The HTML to process. |
||
132 | * @param null|string $css The CSS to use. |
||
133 | */ |
||
134 | 52 | public function __construct($html = null, $css = null) |
|
148 | |||
149 | /** |
||
150 | * Set HTML to process |
||
151 | * |
||
152 | * @param string $html The HTML to process. |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | 50 | public function setHTML($html) |
|
163 | |||
164 | /** |
||
165 | * Set CSS to use |
||
166 | * |
||
167 | * @param string $css The CSS to use. |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | 46 | public function setCSS($css) |
|
177 | |||
178 | /** |
||
179 | * Sort an array on the specificity element |
||
180 | * |
||
181 | * @return int |
||
182 | * |
||
183 | * @param Specificity[] $e1 The first element. |
||
184 | * @param Specificity[] $e2 The second element. |
||
185 | */ |
||
186 | 18 | private static function sortOnSpecificity($e1, $e2) |
|
198 | |||
199 | /** |
||
200 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
201 | * |
||
202 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
203 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
204 | * you may also use the options parameter to specify additional |
||
205 | * Libxml parameters. Recommend these options: |
||
206 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
207 | * @param bool $path [optional] Set the path to your external css-files. |
||
208 | * |
||
209 | * @return string |
||
210 | * |
||
211 | * @throws Exception |
||
212 | */ |
||
213 | 50 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
279 | |||
280 | /** |
||
281 | * get css from inline-html style-block |
||
282 | * |
||
283 | * @param string $html |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | 29 | public function getCssFromInlineHtmlStyleBlock($html) |
|
306 | |||
307 | /** |
||
308 | * Process the loaded CSS |
||
309 | * |
||
310 | * @param string $css |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | 49 | private function processCSS($css) |
|
385 | |||
386 | /** |
||
387 | * @param string $css |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | 49 | private function doCleanup($css) |
|
418 | |||
419 | /** |
||
420 | * remove css media queries from the string |
||
421 | * |
||
422 | * @param string $css |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 48 | private function stripeMediaQueries($css) |
|
433 | |||
434 | /** |
||
435 | * remove charset from the string |
||
436 | * |
||
437 | * @param string $css |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 49 | private function stripeCharsetInCss($css) |
|
445 | |||
446 | /** |
||
447 | * Process the CSS-properties |
||
448 | * |
||
449 | * @return array |
||
450 | * |
||
451 | * @param string $propertyString The CSS-properties. |
||
452 | */ |
||
453 | 36 | private function processCSSProperties($propertyString) |
|
491 | |||
492 | /** |
||
493 | * Split a style string into an array of properties. |
||
494 | * The returned array can contain empty strings. |
||
495 | * |
||
496 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
497 | * |
||
498 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
499 | */ |
||
500 | 36 | private function splitIntoProperties($styles) |
|
522 | |||
523 | /** |
||
524 | * create XPath |
||
525 | * |
||
526 | * @param \DOMDocument $document |
||
527 | * @param array $cssRules |
||
528 | * |
||
529 | * @return \DOMXPath |
||
530 | */ |
||
531 | 49 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
646 | |||
647 | /** |
||
648 | * @param \DOMElement $element |
||
649 | * @param array $ruleProperties |
||
650 | * |
||
651 | * @return string |
||
652 | */ |
||
653 | 34 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
700 | |||
701 | /** |
||
702 | * @param array $definedProperties |
||
703 | * |
||
704 | * @return array |
||
705 | */ |
||
706 | 34 | private function splitStyleIntoChunks(array $definedProperties) |
|
736 | |||
737 | /** |
||
738 | * Strip style tags into the generated HTML. |
||
739 | * |
||
740 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
741 | */ |
||
742 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
764 | |||
765 | /** |
||
766 | * Remove id and class attributes. |
||
767 | * |
||
768 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
769 | */ |
||
770 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
777 | |||
778 | /** |
||
779 | * Should the IDs and classes be removed? |
||
780 | * |
||
781 | * @param bool $on Should we enable cleanup? |
||
782 | * |
||
783 | * @return $this |
||
784 | */ |
||
785 | 3 | public function setCleanup($on = true) |
|
791 | |||
792 | /** |
||
793 | * Set the encoding to use with the DOMDocument |
||
794 | * |
||
795 | * @param string $encoding The encoding to use. |
||
796 | * |
||
797 | * @return $this |
||
798 | * |
||
799 | * @deprecated Doesn't have any effect |
||
800 | */ |
||
801 | public function setEncoding($encoding) |
||
807 | |||
808 | /** |
||
809 | * Set use of inline styles block. |
||
810 | * |
||
811 | * Info: If this is enabled the class will use the style-block in the HTML. |
||
812 | * |
||
813 | * @param bool $on Should we process inline styles? |
||
814 | * |
||
815 | * @return $this |
||
816 | */ |
||
817 | 27 | public function setUseInlineStylesBlock($on = true) |
|
823 | |||
824 | /** |
||
825 | * Set use of inline link block. |
||
826 | * |
||
827 | * Info: If this is enabled the class will use the links reference in the HTML. |
||
828 | * |
||
829 | * @param bool [optional] $on Should we process link styles? |
||
830 | * |
||
831 | * @return $this |
||
832 | */ |
||
833 | 2 | public function setLoadCSSFromHTML($on = true) |
|
839 | |||
840 | /** |
||
841 | * Set strip original style tags. |
||
842 | * |
||
843 | * Info: If this is enabled the class will remove all style tags in the HTML. |
||
844 | * |
||
845 | * @param bool $on Should we process inline styles? |
||
846 | * |
||
847 | * @return $this |
||
848 | */ |
||
849 | 17 | public function setStripOriginalStyleTags($on = true) |
|
855 | |||
856 | /** |
||
857 | * Set exclude media queries. |
||
858 | * |
||
859 | * Info: If this is enabled the media queries will be removed before inlining the rules. |
||
860 | * |
||
861 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
862 | * |
||
863 | * @param bool $on |
||
864 | * |
||
865 | * @return $this |
||
866 | */ |
||
867 | 14 | public function setExcludeMediaQueries($on = true) |
|
873 | |||
874 | /** |
||
875 | * Set exclude charset. |
||
876 | * |
||
877 | * @param bool $on |
||
878 | * |
||
879 | * @return $this |
||
880 | */ |
||
881 | 1 | public function setExcludeCssCharset($on = true) |
|
887 | |||
888 | /** |
||
889 | * Set exclude conditional inline-style blocks. |
||
890 | * |
||
891 | * e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
892 | * |
||
893 | * @param bool $on |
||
894 | * |
||
895 | * @return $this |
||
896 | */ |
||
897 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
903 | } |
||
904 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.