Conditions | 17 |
Paths | 6 |
Total Lines | 69 |
Code Lines | 35 |
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 |
||
124 | private function getIcons($url) { |
||
125 | |||
126 | if (empty($url)) { |
||
127 | return []; |
||
128 | } |
||
129 | |||
130 | $html = $this->dataAccess->retrieveUrl("{$url}/"); |
||
131 | preg_match('!<head.*?>.*</head>!ims', $html, $match); |
||
132 | |||
133 | if (empty($match) || count($match) == 0) { |
||
134 | return []; |
||
135 | } |
||
136 | |||
137 | $head = $match[0]; |
||
138 | |||
139 | $icons = []; |
||
140 | |||
141 | $dom = new \DOMDocument(); |
||
142 | |||
143 | // Use error supression, because the HTML might be too malformed. |
||
144 | if (@$dom->loadHTML($head)) { |
||
145 | $links = $dom->getElementsByTagName('link'); |
||
146 | |||
147 | foreach ($links as $link) { |
||
148 | |||
149 | if ($link->hasAttribute('rel') && $href = $link->getAttribute('href')) { |
||
150 | |||
151 | $attribute = $link->getAttribute('rel'); |
||
152 | |||
153 | // Make sure the href is an absolute URL. |
||
154 | if ($href && filter_var($href, FILTER_VALIDATE_URL) === false) { |
||
155 | $href = $url . '/' . $href; //Todo: Improve this |
||
156 | } |
||
157 | |||
158 | $size = $link->hasAttribute('sizes') ? $link->getAttribute('sizes') : []; |
||
159 | $size = !is_array($size) ? explode('x', $size) : $size; |
||
160 | |||
161 | $type = false; |
||
162 | |||
163 | switch(strtolower($attribute)) { |
||
164 | case Icon::APPLE_TOUCH: |
||
165 | $type = Icon::APPLE_TOUCH; |
||
166 | break; |
||
167 | default: |
||
168 | if(strpos(strtolower($attribute), 'icon') !== FALSE) { |
||
169 | $type = Icon::FAVICON; |
||
170 | $size = []; |
||
171 | } |
||
172 | }; |
||
173 | |||
174 | if(!empty($type) && filter_var($href, FILTER_VALIDATE_URL)) { |
||
175 | $icons[] = new Icon($type, $href, $size); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | //Sort the icons by width |
||
182 | usort($icons, function($a, $b) { |
||
183 | return $a->getWidth() - $b->getWidth(); |
||
184 | }); |
||
185 | |||
186 | //If it is empty, try and get one from the root |
||
187 | if (empty($icons)) { |
||
188 | $icons = $this->getFavicon($url); |
||
189 | } |
||
190 | |||
191 | return $icons; |
||
192 | } |
||
193 | |||
240 |