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 | 46 | public function setHTML($html) |
|
150 | |||
151 | /** |
||
152 | * Set CSS to use |
||
153 | * |
||
154 | * @param string $css The CSS to use. |
||
155 | */ |
||
156 | 44 | public function setCSS($css) |
|
160 | |||
161 | /** |
||
162 | * Sort an array on the specificity element |
||
163 | * |
||
164 | * @return int |
||
165 | * |
||
166 | * @param Specificity[] $e1 The first element. |
||
167 | * @param Specificity[] $e2 The second element. |
||
168 | */ |
||
169 | 17 | private static function sortOnSpecificity($e1, $e2) |
|
181 | |||
182 | /** |
||
183 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
184 | * |
||
185 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
186 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
187 | * you may also use the options parameter to specify additional |
||
188 | * Libxml parameters. Recommend these options: |
||
189 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
190 | * @param bool $path [optional] Set the path to your external css-files. |
||
191 | * |
||
192 | * @return string |
||
193 | * |
||
194 | * @throws Exception |
||
195 | */ |
||
196 | 46 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
262 | |||
263 | /** |
||
264 | * get css from inline-html style-block |
||
265 | * |
||
266 | * @param string $html |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 29 | public function getCssFromInlineHtmlStyleBlock($html) |
|
289 | |||
290 | /** |
||
291 | * Process the loaded CSS |
||
292 | * |
||
293 | * @param $css |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | 45 | private function processCSS($css) |
|
368 | |||
369 | /** |
||
370 | * @param string $css |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | 45 | private function doCleanup($css) |
|
401 | |||
402 | /** |
||
403 | * remove css media queries from the string |
||
404 | * |
||
405 | * @param string $css |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | 44 | private function stripeMediaQueries($css) |
|
416 | |||
417 | /** |
||
418 | * remove charset from the string |
||
419 | * |
||
420 | * @param $css |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 45 | private function stripeCharsetInCss($css) |
|
428 | |||
429 | /** |
||
430 | * Process the CSS-properties |
||
431 | * |
||
432 | * @return array |
||
433 | * |
||
434 | * @param string $propertyString The CSS-properties. |
||
435 | */ |
||
436 | 34 | private function processCSSProperties($propertyString) |
|
474 | |||
475 | /** |
||
476 | * Split a style string into an array of properties. |
||
477 | * The returned array can contain empty strings. |
||
478 | * |
||
479 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
480 | * |
||
481 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
482 | */ |
||
483 | 34 | private function splitIntoProperties($styles) |
|
505 | |||
506 | /** |
||
507 | * create XPath |
||
508 | * |
||
509 | * @param \DOMDocument $document |
||
510 | * @param array $cssRules |
||
511 | * |
||
512 | * @return \DOMXPath |
||
513 | */ |
||
514 | 45 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
631 | |||
632 | /** |
||
633 | * @param \DOMElement $element |
||
634 | * @param array $ruleProperties |
||
635 | * |
||
636 | * @return array |
||
637 | */ |
||
638 | 32 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
685 | |||
686 | /** |
||
687 | * @param array $definedProperties |
||
688 | * |
||
689 | * @return array |
||
690 | */ |
||
691 | 32 | private function splitStyleIntoChunks(array $definedProperties) |
|
721 | |||
722 | /** |
||
723 | * Strip style tags into the generated HTML |
||
724 | * |
||
725 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
726 | * |
||
727 | * @return string |
||
728 | */ |
||
729 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
751 | |||
752 | /** |
||
753 | * Remove id and class attributes. |
||
754 | * |
||
755 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
756 | * |
||
757 | * @return string |
||
758 | */ |
||
759 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
766 | |||
767 | /** |
||
768 | * Should the IDs and classes be removed? |
||
769 | * |
||
770 | * @param bool $on Should we enable cleanup? |
||
771 | */ |
||
772 | 3 | public function setCleanup($on = true) |
|
776 | |||
777 | /** |
||
778 | * Set the encoding to use with the DOMDocument |
||
779 | * |
||
780 | * @param string $encoding The encoding to use. |
||
781 | * |
||
782 | * @deprecated Doesn't have any effect |
||
783 | */ |
||
784 | public function setEncoding($encoding) |
||
788 | |||
789 | /** |
||
790 | * Set use of inline styles block |
||
791 | * If this is enabled the class will use the style-block in the HTML. |
||
792 | * |
||
793 | * @param bool $on Should we process inline styles? |
||
794 | */ |
||
795 | 27 | public function setUseInlineStylesBlock($on = true) |
|
799 | |||
800 | /** |
||
801 | * Set use of inline link block |
||
802 | * If this is enabled the class will use the links reference in the HTML. |
||
803 | * |
||
804 | * @return void |
||
805 | * |
||
806 | * @param bool [optional] $on Should we process link styles? |
||
807 | */ |
||
808 | 2 | public function setLoadCSSFromHTML($on = true) |
|
812 | |||
813 | /** |
||
814 | * Set strip original style tags |
||
815 | * If this is enabled the class will remove all style tags in the HTML. |
||
816 | * |
||
817 | * @param bool $on Should we process inline styles? |
||
818 | */ |
||
819 | 17 | public function setStripOriginalStyleTags($on = true) |
|
823 | |||
824 | /** |
||
825 | * Set exclude media queries |
||
826 | * |
||
827 | * If this is enabled the media queries will be removed before inlining the rules. |
||
828 | * |
||
829 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
830 | * |
||
831 | * @param bool $on |
||
832 | */ |
||
833 | 14 | public function setExcludeMediaQueries($on = true) |
|
837 | |||
838 | /** |
||
839 | * Set exclude charset |
||
840 | * |
||
841 | * @param bool $on |
||
842 | */ |
||
843 | 1 | public function setExcludeCssCharset($on = true) |
|
847 | |||
848 | /** |
||
849 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
850 | * |
||
851 | * @param bool $on |
||
852 | */ |
||
853 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
857 | } |
||
858 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.