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