Passed
Pull Request — master (#22)
by Shashi Prakash
03:27 queued 01:11
created

OpenGraph   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A curl_get_contents() 0 14 1
B fetch() 0 28 10
1
<?php
2
3
namespace shweshi\OpenGraph;
4
5
use DOMDocument;
6
7
class OpenGraph
8
{
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for loadHTML(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

17
        /** @scrutinizer ignore-unhandled */ @$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
33
            }
34
        }
35
36
        return $metadata;
37
    }
38
39
    protected function curl_get_contents($url)
40
    {
41
        $curl = curl_init($url);
42
        curl_setopt($curl, CURLOPT_FAILONERROR, 1);
43
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
44
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
45
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
46
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
47
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
48
        curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
49
        $response = curl_exec($curl);
50
        curl_close($curl);
51
52
        return $response;
53
    }
54
}
55