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 | private $cssConverter; |
||
| 17 | |||
| 18 | /** @var HTML5|null */ |
||
| 19 | private $html5Parser; |
||
| 20 | |||
| 21 | /** @var bool */ |
||
| 22 | private $isHtml5Document = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param bool|null $useHtml5Parser Whether to use a HTML5 parser or the native DOM parser |
||
| 26 | */ |
||
| 27 | public function __construct($useHtml5Parser = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Will inline the $css into the given $html |
||
| 44 | * |
||
| 45 | * Remark: if the html contains <style>-tags those will be used, the rules |
||
| 46 | * in $css will be appended. |
||
| 47 | * |
||
| 48 | * @param string $html |
||
| 49 | * @param string $css |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function convert($html, $css = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Inline the given properties on an given DOMElement |
||
| 74 | * |
||
| 75 | * @param \DOMElement $element |
||
| 76 | * @param Css\Property\Property[] $properties |
||
| 77 | * |
||
| 78 | * @return \DOMElement |
||
| 79 | */ |
||
| 80 | public function inlineCssOnElement(\DOMElement $element, array $properties) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the current inline styles for a given DOMElement |
||
| 110 | * |
||
| 111 | * @param \DOMElement $element |
||
| 112 | * |
||
| 113 | * @return Css\Property\Property[] |
||
| 114 | */ |
||
| 115 | public function getInlineStyles(\DOMElement $element) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $html |
||
| 128 | * |
||
| 129 | * @return \DOMDocument |
||
| 130 | */ |
||
| 131 | protected function createDomDocumentFromHtml($html) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $html |
||
| 144 | * @return \DOMDocument |
||
| 145 | */ |
||
| 146 | protected function parseHtml5($html) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $html |
||
| 155 | * @return \DOMDocument |
||
| 156 | */ |
||
| 157 | protected function parseXhtml($html) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $content |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | protected function canParseHtml5String($content) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $heading |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | protected function isValidHtml5Heading($heading) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $html |
||
| 198 | * @return array|false|string |
||
| 199 | */ |
||
| 200 | protected function convertToHtmlEntities($html) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param \DOMDocument $document |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | protected function getHtmlFromDocument(\DOMDocument $document) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param \DOMDocument $document |
||
| 238 | * @param Css\Rule\Rule[] $rules |
||
| 239 | * |
||
| 240 | * @return \DOMDocument |
||
| 241 | */ |
||
| 242 | protected function inline(\DOMDocument $document, array $rules) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Merge the CSS rules to determine the applied properties. |
||
| 289 | * |
||
| 290 | * @param Css\Property\Property[] $properties |
||
| 291 | * @param Css\Property\Property[] $cssProperties existing applied properties indexed by name |
||
| 292 | * |
||
| 293 | * @return Css\Property\Property[] updated properties, indexed by name |
||
| 294 | */ |
||
| 295 | private function calculatePropertiesToBeApplied(array $properties, array $cssProperties) |
||
| 327 | } |
||
| 328 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: