| Conditions | 20 |
| Paths | 25 |
| Total Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 29 | public function convert(string $body): array |
||
| 30 | { |
||
| 31 | $document = new \DOMDocument(); |
||
| 32 | libxml_use_internal_errors(true); |
||
| 33 | $document->loadHTML('<?xml encoding="UTF-8">'.self::stripHtmlTags($body)); |
||
| 34 | $document->encoding = 'UTF-8'; |
||
| 35 | libxml_clear_errors(); |
||
| 36 | |||
| 37 | /** @var \DOMNodeList $body */ |
||
| 38 | if (!($body = $document->getElementsByTagName('body')->item(0))) { |
||
| 39 | throw new \InvalidArgumentException('Invalid HTML was provided'); |
||
| 40 | } |
||
| 41 | |||
| 42 | $components = []; |
||
| 43 | foreach ($body->childNodes as $node) { |
||
| 44 | switch ($node->nodeName) { |
||
| 45 | case 'h1': |
||
| 46 | case 'h2': |
||
| 47 | case 'h3': |
||
| 48 | case 'h4': |
||
| 49 | case 'h5': |
||
| 50 | case 'h6': |
||
| 51 | if ('' !== $node->textContent) { |
||
| 52 | $level = substr($node->nodeName, 1); |
||
| 53 | $components[] = new Heading($node->textContent, (int) $level); |
||
| 54 | } |
||
| 55 | |||
| 56 | break; |
||
| 57 | case 'p': |
||
| 58 | if ('' !== $node->textContent) { |
||
| 59 | $components[] = new Body($node->textContent); |
||
| 60 | } |
||
| 61 | |||
| 62 | break; |
||
| 63 | |||
| 64 | case 'figure': |
||
| 65 | $src = $node->getElementsByTagName('img') |
||
| 66 | ->item(0) |
||
| 67 | ->getAttribute('src'); |
||
| 68 | |||
| 69 | $caption = $node->getElementsByTagName('figcaption') |
||
| 70 | ->item(0) |
||
| 71 | ->textContent; |
||
| 72 | |||
| 73 | $components[] = new Figure($src, $caption); |
||
| 74 | |||
| 75 | break; |
||
| 76 | case 'div': |
||
| 77 | if (!$node->hasAttribute('class')) { |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | |||
| 81 | $iframeElement = $node->getElementsByTagName('iframe') |
||
| 82 | ->item(0); |
||
| 83 | |||
| 84 | if (null !== $iframeElement) { |
||
| 85 | $webVideoUrl = $iframeElement->getAttribute('src'); |
||
| 86 | $url = str_replace('\"', '', $webVideoUrl); |
||
| 87 | if (false !== strpos($url, 'twitter.com')) { |
||
| 88 | $components[] = new Tweet('https:'.$url); |
||
| 89 | } elseif (false !== strpos($url, 'iframe.ly')) { |
||
| 90 | $parsedUrl = parse_url($url); |
||
| 91 | parse_str($parsedUrl['query'], $url); |
||
| 92 | |||
| 93 | $components[] = new FacebookPost($url['url']); |
||
| 94 | } elseif (false !== strpos($url, 'facebook.com')) { |
||
| 95 | $parsedUrl = parse_url($url); |
||
| 96 | parse_str($parsedUrl['query'], $url); |
||
| 97 | |||
| 98 | $components[] = new FacebookPost($url['href']); |
||
| 99 | } else { |
||
| 100 | $components[] = new EmbedWebVideo($url); |
||
| 101 | } |
||
| 102 | |||
| 103 | break; |
||
| 104 | } |
||
| 105 | |||
| 106 | $instagramElement = $node->getElementsByTagName('blockquote') |
||
| 107 | ->item(0); |
||
| 108 | |||
| 109 | if (null !== $instagramElement) { |
||
| 110 | $instagramUrl = $instagramElement->getAttribute('data-instgrm-permalink'); |
||
| 111 | $url = str_replace('\"', '', $instagramUrl); |
||
| 112 | $components[] = new Instagram($url); |
||
| 113 | } |
||
| 114 | |||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return $components; |
||
| 120 | } |
||
| 121 | |||
| 127 |