| Conditions | 12 | 
| Paths | 73 | 
| Total Lines | 37 | 
| Code Lines | 20 | 
| 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 | ||
| 9 | public function fetch($url, $allMeta = null) | ||
| 10 |     { | ||
| 11 | $html = $this->curl_get_contents($url); | ||
| 12 | |||
| 13 | /** | ||
| 14 | * parsing starts here:. | ||
| 15 | */ | ||
| 16 | $doc = new DOMDocument(); | ||
| 17 |         @$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html); | ||
|  | |||
| 18 | |||
| 19 |         $tags = $doc->getElementsByTagName('meta'); | ||
| 20 | $metadata = []; | ||
| 21 |         foreach ($tags as $tag) { | ||
| 22 |             $metaproperty = ($tag->hasAttribute('property')) ? $tag->getAttribute('property') : $tag->getAttribute('name'); | ||
| 23 |             if (!$allMeta && $metaproperty && strpos($tag->getAttribute('property'), 'og:') === 0) { | ||
| 24 | $key = strtr(substr($metaproperty, 3), '-', '_'); | ||
| 25 |                 $value = $tag->getAttribute('content'); | ||
| 26 | } | ||
| 27 |             if ($allMeta && $metaproperty) { | ||
| 28 | $key = (strpos($metaproperty, 'og:') === 0) ? strtr(substr($metaproperty, 3), '-', '_') : $metaproperty; | ||
| 29 |                 $value = $tag->getAttribute('content'); | ||
| 30 | } | ||
| 31 |             if (!empty($key)) { | ||
| 32 | $metadata[$key] = $value; | ||
| 33 | } | ||
| 34 | /* | ||
| 35 | * Verify image url | ||
| 36 | */ | ||
| 37 |             if (isset($metadata['image'])) { | ||
| 38 | $isValidImageUrl = $this->verify_image_url($metadata['image']); | ||
| 39 |                 if (!$isValidImageUrl) { | ||
| 40 | $metadata['image'] = ''; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | return $metadata; | ||
| 46 | } | ||
| 71 | 
If you suppress an error, we recommend checking for the error condition explicitly: