| Conditions | 14 |
| Paths | 146 |
| Total Lines | 45 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 15 | ||
| Bugs | 2 | Features | 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 |
||
| 10 | { |
||
| 11 | |||
| 12 | private const string DEFAULT_USER_AGENT = 'Curl'; |
||
|
|
|||
| 13 | |||
| 14 | private string $userAgent = self::DEFAULT_USER_AGENT; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @throws FetchException |
||
| 18 | */ |
||
| 19 | public function fetch($url, $allMeta = null, $lang = null, $options = LIBXML_NOWARNING | LIBXML_NOERROR): array |
||
| 20 | { |
||
| 21 | $html = $this->curl_get_contents($url, $lang, $this->userAgent); |
||
| 22 | /** |
||
| 23 | * parsing starts here:. |
||
| 24 | */ |
||
| 25 | $doc = new DOMDocument(); |
||
| 26 | |||
| 27 | $libxml_previous_state = libxml_use_internal_errors(true); |
||
| 28 | $doc->loadHTML('<?xml encoding="utf-8" ?>'.$html, $options); |
||
| 29 | //catch possible errors due to empty or malformed HTML |
||
| 30 | if ($options > 0 && ($options & (LIBXML_NOWARNING | LIBXML_NOERROR)) == 0) { |
||
| 31 | Log::warning(libxml_get_errors()); |
||
| 32 | } |
||
| 33 | libxml_clear_errors(); |
||
| 34 | // restore previous state |
||
| 35 | libxml_use_internal_errors($libxml_previous_state); |
||
| 36 | |||
| 37 | $tags = $doc->getElementsByTagName('meta'); |
||
| 38 | $metadata = []; |
||
| 39 | foreach ($tags as $tag) { |
||
| 40 | $metaProperty = ($tag->hasAttribute('property')) ? $tag->getAttribute('property') : $tag->getAttribute('name'); |
||
| 41 | if (!$allMeta && $metaProperty && str_starts_with($tag->getAttribute('property'), 'og:')) { |
||
| 42 | $key = strtr(substr($metaProperty, 3), '-', '_'); |
||
| 43 | $value = $this->get_meta_value($tag); |
||
| 44 | } |
||
| 45 | if ($allMeta && $metaProperty) { |
||
| 46 | $key = (str_starts_with($metaProperty, 'og:')) ? strtr(substr($metaProperty, 3), '-', '_') : $metaProperty; |
||
| 47 | $value = $this->get_meta_value($tag); |
||
| 48 | } |
||
| 49 | if (!empty($key)) { |
||
| 50 | $metadata[$key] = $value; |
||
| 51 | } |
||
| 52 | /* |
||
| 53 | * Verify image url |
||
| 54 | */ |
||
| 55 | if (isset($metadata['image'])) { |
||
| 159 |