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 | * regular expression: conditional inline style tags |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private static $excludeConditionalInlineStylesBlockRegEx = '/<!--.*<style.*-->/isU'; |
||
35 | |||
36 | /** |
||
37 | * regular expression: inline style tags |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private static $styleTagRegEx = '|<style(.*)>(.*)</style>|isU'; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private static $domLinkReplaceHelper = array( |
||
47 | 'orig' => array('[', ']', '{', '}',), |
||
48 | 'tmp' => array( |
||
49 | '!!!!SQUARE_BRACKET_LEFT!!!!', |
||
50 | '!!!!SQUARE_BRACKET_RIGHT!!!!', |
||
51 | '!!!!BRACKET_LEFT!!!!', |
||
52 | '!!!!BRACKET_RIGHT!!!!', |
||
53 | ), |
||
54 | ); |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected static $domReplaceHelper = array( |
||
60 | 'orig' => array('&'), |
||
61 | 'tmp' => array('!!!!AMP!!!!'), |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * regular expression: css-comments |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private static $styleCommentRegEx = '/\\/\\*.*\\*\\//sU'; |
||
70 | |||
71 | /** |
||
72 | * The CSS to use |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $css; |
||
77 | |||
78 | /** |
||
79 | * Should the generated HTML be cleaned |
||
80 | * |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $cleanup = false; |
||
84 | |||
85 | /** |
||
86 | * The encoding to use. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $encoding = 'UTF-8'; |
||
91 | |||
92 | /** |
||
93 | * The HTML to process |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $html; |
||
98 | |||
99 | /** |
||
100 | * Use inline-styles block as CSS |
||
101 | * |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $useInlineStylesBlock = false; |
||
105 | |||
106 | /** |
||
107 | * Use link block reference as CSS |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | private $loadCSSFromHTML = false; |
||
112 | |||
113 | /** |
||
114 | * Strip original style tags |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $stripOriginalStyleTags = false; |
||
119 | |||
120 | /** |
||
121 | * Exclude conditional inline-style blocks |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | private $excludeConditionalInlineStylesBlock = true; |
||
126 | |||
127 | /** |
||
128 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
129 | * |
||
130 | * @var bool |
||
131 | */ |
||
132 | private $excludeMediaQueries = true; |
||
133 | |||
134 | /** |
||
135 | * Exclude media queries from "$this->css" and keep media queries for inline-styles blocks |
||
136 | * |
||
137 | * @var bool |
||
138 | */ |
||
139 | private $excludeCssCharset = true; |
||
140 | |||
141 | /** |
||
142 | * Creates an instance, you could set the HTML and CSS here, or load it |
||
143 | * later. |
||
144 | * |
||
145 | * @param null|string $html The HTML to process. |
||
146 | * @param null|string $css The CSS to use. |
||
147 | */ |
||
148 | 48 | public function __construct($html = null, $css = null) |
|
158 | |||
159 | /** |
||
160 | * Set HTML to process |
||
161 | * |
||
162 | * @param string $html The HTML to process. |
||
163 | */ |
||
164 | 46 | public function setHTML($html) |
|
169 | |||
170 | /** |
||
171 | * Set CSS to use |
||
172 | * |
||
173 | * @param string $css The CSS to use. |
||
174 | */ |
||
175 | 44 | public function setCSS($css) |
|
179 | |||
180 | /** |
||
181 | * Sort an array on the specificity element |
||
182 | * |
||
183 | * @return int |
||
184 | * |
||
185 | * @param Specificity[] $e1 The first element. |
||
186 | * @param Specificity[] $e2 The second element. |
||
187 | */ |
||
188 | 17 | private static function sortOnSpecificity($e1, $e2) |
|
200 | |||
201 | /** |
||
202 | * Converts the loaded HTML into an HTML-string with inline styles based on the loaded CSS |
||
203 | * |
||
204 | * @param bool $outputXHTML [optional] Should we output valid XHTML? |
||
205 | * @param int $libXMLOptions [optional] $libXMLOptions Since PHP 5.4.0 and Libxml 2.6.0, |
||
206 | * you may also use the options parameter to specify additional |
||
207 | * Libxml parameters. Recommend these options: |
||
208 | * LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD |
||
209 | * @param bool $path [optional] Set the path to your external css-files. |
||
210 | * |
||
211 | * @return string |
||
212 | * |
||
213 | * @throws Exception |
||
214 | */ |
||
215 | 46 | public function convert($outputXHTML = false, $libXMLOptions = 0, $path = false) |
|
292 | |||
293 | /** |
||
294 | * get css from inline-html style-block |
||
295 | * |
||
296 | * @param string $html |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 29 | public function getCssFromInlineHtmlStyleBlock($html) |
|
319 | |||
320 | /** |
||
321 | * Process the loaded CSS |
||
322 | * |
||
323 | * @param $css |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | 45 | private function processCSS($css) |
|
398 | |||
399 | /** |
||
400 | * @param string $css |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | 45 | private function doCleanup($css) |
|
431 | |||
432 | /** |
||
433 | * remove css media queries from the string |
||
434 | * |
||
435 | * @param string $css |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | 44 | private function stripeMediaQueries($css) |
|
446 | |||
447 | /** |
||
448 | * remove charset from the string |
||
449 | * |
||
450 | * @param $css |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | 45 | private function stripeCharsetInCss($css) |
|
458 | |||
459 | /** |
||
460 | * Process the CSS-properties |
||
461 | * |
||
462 | * @return array |
||
463 | * |
||
464 | * @param string $propertyString The CSS-properties. |
||
465 | */ |
||
466 | 34 | private function processCSSProperties($propertyString) |
|
504 | |||
505 | /** |
||
506 | * Split a style string into an array of properties. |
||
507 | * The returned array can contain empty strings. |
||
508 | * |
||
509 | * @param string $styles ex: 'color:blue;font-size:12px;' |
||
510 | * |
||
511 | * @return array an array of strings containing css property ex: array('color:blue','font-size:12px') |
||
512 | */ |
||
513 | 34 | private function splitIntoProperties($styles) |
|
535 | |||
536 | /** |
||
537 | * create DOMDocument from HTML |
||
538 | * |
||
539 | * @param string $html |
||
540 | * @param int $libXMLOptions |
||
541 | * |
||
542 | * @return \DOMDocument |
||
543 | */ |
||
544 | 45 | private function createDOMDocument($html, $libXMLOptions = 0) |
|
590 | |||
591 | /** |
||
592 | * Get the encoding to use |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | 45 | private function getEncoding() |
|
600 | |||
601 | /** |
||
602 | * create XPath |
||
603 | * |
||
604 | * @param \DOMDocument $document |
||
605 | * @param array $cssRules |
||
606 | * |
||
607 | * @return \DOMXPath |
||
608 | */ |
||
609 | 45 | private function createXPath(\DOMDocument $document, array $cssRules) |
|
726 | |||
727 | /** |
||
728 | * @param \DOMElement $element |
||
729 | * @param array $ruleProperties |
||
730 | * |
||
731 | * @return array |
||
732 | */ |
||
733 | 32 | private function createPropertyChunks(\DOMElement $element, array $ruleProperties) |
|
780 | |||
781 | /** |
||
782 | * @param array $definedProperties |
||
783 | * |
||
784 | * @return array |
||
785 | */ |
||
786 | 32 | private function splitStyleIntoChunks(array $definedProperties) |
|
816 | |||
817 | /** |
||
818 | * Strip style tags into the generated HTML |
||
819 | * |
||
820 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
821 | * |
||
822 | * @return string |
||
823 | */ |
||
824 | 13 | private function stripOriginalStyleTags(\DOMXPath $xPath) |
|
846 | |||
847 | /** |
||
848 | * Remove id and class attributes. |
||
849 | * |
||
850 | * @param \DOMXPath $xPath The DOMXPath for the entire document. |
||
851 | * |
||
852 | * @return string |
||
853 | */ |
||
854 | 3 | private function cleanupHTML(\DOMXPath $xPath) |
|
861 | |||
862 | /** |
||
863 | * Should the IDs and classes be removed? |
||
864 | * |
||
865 | * @param bool $on Should we enable cleanup? |
||
866 | */ |
||
867 | 3 | public function setCleanup($on = true) |
|
871 | |||
872 | /** |
||
873 | * Set the encoding to use with the DOMDocument |
||
874 | * |
||
875 | * @param string $encoding The encoding to use. |
||
876 | * |
||
877 | * @deprecated Doesn't have any effect |
||
878 | */ |
||
879 | public function setEncoding($encoding) |
||
883 | |||
884 | /** |
||
885 | * Set use of inline styles block |
||
886 | * If this is enabled the class will use the style-block in the HTML. |
||
887 | * |
||
888 | * @param bool $on Should we process inline styles? |
||
889 | */ |
||
890 | 27 | public function setUseInlineStylesBlock($on = true) |
|
894 | |||
895 | /** |
||
896 | * Set use of inline link block |
||
897 | * If this is enabled the class will use the links reference in the HTML. |
||
898 | * |
||
899 | * @return void |
||
900 | * |
||
901 | * @param bool [optional] $on Should we process link styles? |
||
902 | */ |
||
903 | 2 | public function setLoadCSSFromHTML($on = true) |
|
907 | |||
908 | /** |
||
909 | * Set strip original style tags |
||
910 | * If this is enabled the class will remove all style tags in the HTML. |
||
911 | * |
||
912 | * @param bool $on Should we process inline styles? |
||
913 | */ |
||
914 | 17 | public function setStripOriginalStyleTags($on = true) |
|
918 | |||
919 | /** |
||
920 | * Set exclude media queries |
||
921 | * |
||
922 | * If this is enabled the media queries will be removed before inlining the rules. |
||
923 | * |
||
924 | * WARNING: If you use inline styles block "<style>" the this option will keep the media queries. |
||
925 | * |
||
926 | * @param bool $on |
||
927 | */ |
||
928 | 14 | public function setExcludeMediaQueries($on = true) |
|
932 | |||
933 | /** |
||
934 | * Set exclude charset |
||
935 | * |
||
936 | * @param bool $on |
||
937 | */ |
||
938 | 1 | public function setExcludeCssCharset($on = true) |
|
942 | |||
943 | /** |
||
944 | * Set exclude conditional inline-style blocks e.g.: <!--[if gte mso 9]><style>.foo { bar } </style><![endif]--> |
||
945 | * |
||
946 | * @param bool $on |
||
947 | */ |
||
948 | 6 | public function setExcludeConditionalInlineStylesBlock($on = true) |
|
952 | |||
953 | /** |
||
954 | * @param string $html |
||
955 | * |
||
956 | * @return string |
||
957 | */ |
||
958 | 45 | private function replaceToPreserveHtmlEntities($html) |
|
985 | |||
986 | /** |
||
987 | * @param string $html |
||
988 | * |
||
989 | * @return string |
||
990 | */ |
||
991 | 45 | private function putReplacedBackToPreserveHtmlEntities($html) |
|
999 | } |
||
1000 |