| Conditions | 38 |
| Paths | 76 |
| Total Lines | 265 |
| Lines | 54 |
| Ratio | 20.38 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 202 | public function convert($outputXHTML = false) |
||
| 203 | { |
||
| 204 | // redefine |
||
| 205 | $outputXHTML = (bool) $outputXHTML; |
||
| 206 | |||
| 207 | // validate |
||
| 208 | if($this->html == null) throw new Exception('No HTML provided.'); |
||
| 209 | |||
| 210 | // should we use inline style-block |
||
| 211 | if ($this->useInlineStylesBlock) { |
||
| 212 | // init var |
||
| 213 | $matches = array(); |
||
| 214 | |||
| 215 | // match the style blocks |
||
| 216 | preg_match_all('|<style(.*)>(.*)</style>|isU', $this->html, $matches); |
||
| 217 | |||
| 218 | // any style-blocks found? |
||
| 219 | if (!empty($matches[2])) { |
||
| 220 | // add |
||
| 221 | foreach($matches[2] as $match) $this->css .= trim($match) ."\n"; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | // process css |
||
| 226 | $this->processCSS(); |
||
| 227 | |||
| 228 | // create new DOMDocument |
||
| 229 | $document = new \DOMDocument('1.0', $this->getEncoding()); |
||
| 230 | |||
| 231 | // set error level |
||
| 232 | libxml_use_internal_errors(true); |
||
| 233 | |||
| 234 | // load HTML |
||
| 235 | // $document->loadHTML($this->html); |
||
| 236 | $document->loadHTML('<'.'?xml version="1.0" encoding="'.$this->getEncoding().'"?'.'><head><meta http-equiv="Content-Type" content="text/html; charset='.$this->getEncoding().'"></head>'.$this->html); |
||
| 237 | |||
| 238 | // create new XPath |
||
| 239 | $xPath = new \DOMXPath($document); |
||
| 240 | |||
| 241 | // any rules? |
||
| 242 | if (!empty($this->cssRules)) { |
||
| 243 | // loop rules |
||
| 244 | foreach ($this->cssRules as $rule) { |
||
| 245 | // init var |
||
| 246 | $query = $this->buildXPathQuery($rule['selector']); |
||
| 247 | |||
| 248 | // validate query |
||
| 249 | if($query === false) continue; |
||
| 250 | |||
| 251 | // search elements |
||
| 252 | $elements = $xPath->query($query); |
||
| 253 | |||
| 254 | // validate elements |
||
| 255 | if($elements === false) continue; |
||
| 256 | |||
| 257 | // loop found elements |
||
| 258 | foreach ($elements as $element) { |
||
| 259 | // no styles stored? |
||
| 260 | if ($element->attributes->getNamedItem( |
||
| 261 | 'data-css-to-inline-styles-original-styles' |
||
| 262 | ) == null) { |
||
| 263 | // init var |
||
| 264 | $originalStyle = ''; |
||
| 265 | if ($element->attributes->getNamedItem('style') !== null) { |
||
| 266 | $originalStyle = $element->attributes->getNamedItem('style')->value; |
||
| 267 | } |
||
| 268 | |||
| 269 | // store original styles |
||
| 270 | $element->setAttribute( |
||
| 271 | 'data-css-to-inline-styles-original-styles', |
||
| 272 | $originalStyle |
||
| 273 | ); |
||
| 274 | |||
| 275 | // clear the styles |
||
| 276 | $element->setAttribute('style', ''); |
||
| 277 | } |
||
| 278 | |||
| 279 | // init var |
||
| 280 | $properties = array(); |
||
| 281 | |||
| 282 | // get current styles |
||
| 283 | $stylesAttribute = $element->attributes->getNamedItem('style'); |
||
| 284 | |||
| 285 | // any styles defined before? |
||
| 286 | View Code Duplication | if ($stylesAttribute !== null) { |
|
| 287 | // get value for the styles attribute |
||
| 288 | $definedStyles = (string) $stylesAttribute->value; |
||
| 289 | |||
| 290 | // split into properties |
||
| 291 | $definedProperties = (array) explode(';', $definedStyles); |
||
| 292 | |||
| 293 | // loop properties |
||
| 294 | foreach ($definedProperties as $property) { |
||
| 295 | // validate property |
||
| 296 | if($property == '') continue; |
||
| 297 | |||
| 298 | // split into chunks |
||
| 299 | $chunks = (array) explode(':', trim($property), 2); |
||
| 300 | |||
| 301 | // validate |
||
| 302 | if(!isset($chunks[1])) continue; |
||
| 303 | |||
| 304 | // loop chunks |
||
| 305 | $properties[$chunks[0]] = trim($chunks[1]); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | // add new properties into the list |
||
| 310 | foreach ($rule['properties'] as $key => $value) { |
||
| 311 | $properties[$key] = $value; |
||
| 312 | } |
||
| 313 | |||
| 314 | // build string |
||
| 315 | $propertyChunks = array(); |
||
| 316 | |||
| 317 | // build chunks |
||
| 318 | View Code Duplication | foreach ($properties as $key => $values) { |
|
| 319 | foreach ((array) $values as $value) { |
||
| 320 | $propertyChunks[] = $key . ': ' . $value . ';'; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | // build properties string |
||
| 325 | $propertiesString = implode(' ', $propertyChunks); |
||
| 326 | |||
| 327 | // set attribute |
||
| 328 | if ($propertiesString != '') { |
||
| 329 | $element->setAttribute('style', $propertiesString); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | // reapply original styles |
||
| 335 | $query = $this->buildXPathQuery( |
||
| 336 | '*[@data-css-to-inline-styles-original-styles]' |
||
| 337 | ); |
||
| 338 | |||
| 339 | // validate query |
||
| 340 | if($query === false) return; |
||
| 341 | |||
| 342 | // search elements |
||
| 343 | $elements = $xPath->query($query); |
||
| 344 | |||
| 345 | // loop found elements |
||
| 346 | foreach ($elements as $element) { |
||
| 347 | // get the original styles |
||
| 348 | $originalStyle = $element->attributes->getNamedItem( |
||
| 349 | 'data-css-to-inline-styles-original-styles' |
||
| 350 | )->value; |
||
| 351 | |||
| 352 | if ($originalStyle != '') { |
||
| 353 | $originalProperties = array(); |
||
| 354 | $originalStyles = (array) explode(';', $originalStyle); |
||
| 355 | |||
| 356 | foreach ($originalStyles as $property) { |
||
| 357 | // validate property |
||
| 358 | if($property == '') continue; |
||
| 359 | |||
| 360 | // split into chunks |
||
| 361 | $chunks = (array) explode(':', trim($property), 2); |
||
| 362 | |||
| 363 | // validate |
||
| 364 | if(!isset($chunks[1])) continue; |
||
| 365 | |||
| 366 | // loop chunks |
||
| 367 | $originalProperties[$chunks[0]] = trim($chunks[1]); |
||
| 368 | } |
||
| 369 | |||
| 370 | // get current styles |
||
| 371 | $stylesAttribute = $element->attributes->getNamedItem('style'); |
||
| 372 | $properties = array(); |
||
| 373 | |||
| 374 | // any styles defined before? |
||
| 375 | View Code Duplication | if ($stylesAttribute !== null) { |
|
| 376 | // get value for the styles attribute |
||
| 377 | $definedStyles = (string) $stylesAttribute->value; |
||
| 378 | |||
| 379 | // split into properties |
||
| 380 | $definedProperties = (array) explode(';', $definedStyles); |
||
| 381 | |||
| 382 | // loop properties |
||
| 383 | foreach ($definedProperties as $property) { |
||
| 384 | // validate property |
||
| 385 | if($property == '') continue; |
||
| 386 | |||
| 387 | // split into chunks |
||
| 388 | $chunks = (array) explode(':', trim($property), 2); |
||
| 389 | |||
| 390 | // validate |
||
| 391 | if(!isset($chunks[1])) continue; |
||
| 392 | |||
| 393 | // loop chunks |
||
| 394 | $properties[$chunks[0]] = trim($chunks[1]); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | // add new properties into the list |
||
| 399 | foreach ($originalProperties as $key => $value) { |
||
| 400 | $properties[$key] = $value; |
||
| 401 | } |
||
| 402 | |||
| 403 | // build string |
||
| 404 | $propertyChunks = array(); |
||
| 405 | |||
| 406 | // build chunks |
||
| 407 | View Code Duplication | foreach ($properties as $key => $values) { |
|
| 408 | foreach ((array) $values as $value) { |
||
| 409 | $propertyChunks[] = $key . ': ' . $value . ';'; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | // build properties string |
||
| 414 | $propertiesString = implode(' ', $propertyChunks); |
||
| 415 | |||
| 416 | // set attribute |
||
| 417 | if($propertiesString != '') $element->setAttribute( |
||
| 418 | 'style', $propertiesString |
||
| 419 | ); |
||
| 420 | } |
||
| 421 | |||
| 422 | // remove placeholder |
||
| 423 | $element->removeAttribute( |
||
| 424 | 'data-css-to-inline-styles-original-styles' |
||
| 425 | ); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | // should we output XHTML? |
||
| 430 | if ($outputXHTML) { |
||
| 431 | // set formating |
||
| 432 | $document->formatOutput = true; |
||
| 433 | |||
| 434 | // get the HTML as XML |
||
| 435 | $html = $document->saveXML(null, LIBXML_NOEMPTYTAG); |
||
| 436 | |||
| 437 | // get start of the XML-declaration |
||
| 438 | $startPosition = strpos($html, '<?xml'); |
||
| 439 | |||
| 440 | // valid start position? |
||
| 441 | if ($startPosition !== false) { |
||
| 442 | // get end of the xml-declaration |
||
| 443 | $endPosition = strpos($html, '?>', $startPosition); |
||
| 444 | |||
| 445 | // remove the XML-header |
||
| 446 | $html = ltrim(substr($html, $endPosition + 1)); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | // just regular HTML 4.01 as it should be used in newsletters |
||
| 451 | else { |
||
| 452 | // get the HTML |
||
| 453 | $html = $document->saveHTML(); |
||
| 454 | } |
||
| 455 | |||
| 456 | // cleanup the HTML if we need to |
||
| 457 | if($this->cleanup) $html = $this->cleanupHTML($html); |
||
| 458 | |||
| 459 | // strip original style tags if we need to |
||
| 460 | if ($this->stripOriginalStyleTags) { |
||
| 461 | $html = $this->stripOriginalStyleTags($html); |
||
| 462 | } |
||
| 463 | |||
| 464 | // return |
||
| 465 | return $html; |
||
| 466 | } |
||
| 467 | |||
| 701 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.