Passed
Pull Request — master (#19)
by Ryosuke
62:49
created

Consumer::extractOpenGraphData()   F

Complexity

Conditions 20
Paths 2166

Size

Total Lines 94

Duplication

Lines 40
Ratio 42.55 %

Code Coverage

Tests 49
CRAP Score 29.7455

Importance

Changes 0
Metric Value
dl 40
loc 94
ccs 49
cts 69
cp 0.71
rs 0
c 0
b 0
f 0
cc 20
nc 2166
nop 1
crap 29.7455

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Fusonic\OpenGraph;
4
5
use Fusonic\Linq\Linq;
6
use Fusonic\OpenGraph\Objects\ObjectBase;
7
use Fusonic\OpenGraph\Objects\Website;
8
use GuzzleHttp\Adapter\AdapterInterface;
9
use GuzzleHttp\Client;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
/**
13
 * Consumer that extracts Open Graph data from either a URL or a HTML string.
14
 */
15
class Consumer
16
{
17
    private $client;
18
19
    /**
20
     * When enabled, crawler will read content of title and meta description if no
21
     * Open Graph data is provided by target page.
22
     *
23
     * @var bool
24
     */
25
    public $useFallbackMode = false;
26
27
    /**
28
     * When enabled, crawler will throw exceptions for some crawling errors like unexpected
29
     * Open Graph elements.
30
     *
31
     * @var bool
32
     */
33
    public $debug = false;
34
35
    /**
36
     * @param   AdapterInterface $adapter Guzzle adapter to use for making HTTP requests.
37
     * @param   array            $config  Optional Guzzle config overrides.
38
     */
39 16
    public function __construct(AdapterInterface $adapter = null, array $config = [])
40
    {
41 16
        $config = array_replace_recursive(['adapter' => $adapter], $config);
42
43 16
        $this->client = new Client($config);
44 16
    }
45
46
    /**
47
     * Fetches HTML content from the given URL and then crawls it for Open Graph data.
48
     *
49
     * @param   string  $url            URL to be crawled.
50
     *
51
     * @return  Website
52
     */
53
    public function loadUrl($url)
54
    {
55
        // Fetch HTTP content using Guzzle
56
        $response = $this->client->get($url);
57
58
        return $this->loadHtml($response->getBody()->__toString(), $url);
59
    }
60
61
    /**
62
     * Crawls the given HTML string for OpenGraph data.
63
     *
64
     * @param   string  $html           HTML string, usually whole content of crawled web resource.
65
     * @param   string  $fallbackUrl    URL to use when fallback mode is enabled.
66
     *
67
     * @return  ObjectBase
68
     */
69 16
    public function loadHtml($html, $fallbackUrl = null)
70
    {
71
        // Extract all data that can be found
72 16
        $page = $this->extractOpenGraphData($html);
73
74
        // Use the user's URL as fallback
75 13
        if ($this->useFallbackMode && $page->url === null) {
76 1
            $page->url = $fallbackUrl;
77 1
        }
78
79
        // Return result
80 13
        return $page;
81
    }
82
83 16
    private function extractOpenGraphData($content)
84
    {
85 16
        $crawler = new Crawler;
86 16
        $crawler->addHTMLContent($content, 'UTF-8');
87
88 16
        $properties = [];
89 16
        foreach(['name', 'property'] as $t)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
90
        {
91
            // Get all meta-tags starting with "og:"
92 16
            $ogMetaTags = $crawler->filter("meta[{$t}^='og:']");
93
            // Create clean property array
94 16
            $props = Linq::from($ogMetaTags)
95 16
                ->select(
96
                    function (\DOMElement $tag) use ($t) {
97 13
                        $name = strtolower(trim($tag->getAttribute($t)));
98 13
                        $value = trim($tag->getAttribute("content"));
99 13
                        return new Property($name, $value);
100
                    }
101 16
                )
102 16
                ->toArray();
103 16
            $properties = array_merge($properties, $props);
104
          
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
105 16
        }
106
            
107
        // Create new object of the correct type
108 16
        $typeProperty = Linq::from($properties)
109 16
            ->firstOrNull(
110 13
                function (Property $property) {
111 13
                    return $property->key === Property::TYPE;
112
                }
113 16
            );
114 16
        switch ($typeProperty !== null ? $typeProperty->value : null) {
115 16
            default:
0 ignored issues
show
Unused Code introduced by
default: $object = n...s\Website(); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
116 16
                $object = new Website();
117 16
                break;
118 16
        }
119
120
        // Assign all properties to the object
121 16
        $object->assignProperties($properties, $this->debug);
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
122
123
        // Fallback for url
124 13
        if ($this->useFallbackMode && !$object->url) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
125 2
            $urlElement = $crawler->filter("link[rel='canonical']")->first();
126 2
            if ($urlElement->count()) {
127 1
                $object->url = trim($urlElement->attr("href"));
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
128 1
            }
129 2
        }
130
131
        // Fallback for title
132 13 View Code Duplication
        if ($this->useFallbackMode && !$object->title) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133 2
            $titleElement = $crawler->filter("title")->first();
134 2
            if ($titleElement->count()) {
135 2
                $object->title = trim($titleElement->text());
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
136 2
            }
137
138 2
            if (!$object->title) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
139
                $titleElement = $crawler->filter("h1")->first();
140
                if ($titleElement->count()) {
141
                    $object->title = trim($titleElement->text());
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
142
                }
143
            }
144
145 2
            if (!$object->title) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
146
                $titleElement = $crawler->filter("h2")->first();
147
                if ($titleElement->count()) {
148
                    $object->title = trim($titleElement->text());
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
149
                }
150
            }
151 2
        }
152
153
        // Fallback for description
154 13 View Code Duplication
        if ($this->useFallbackMode && !$object->description) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155 2
            $descriptionElement = $crawler->filter("meta[property='description']")->first();
156 2
            if ($descriptionElement->count()) {
157 2
                $object->description = trim($descriptionElement->attr("content"));
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
158 2
            }
159
160 2
            if (!$object->description) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
161
                $descriptionElement = $crawler->filter("meta[name='description']")->first();
162
                if ($descriptionElement->count()) {
163
                    $object->description = trim($descriptionElement->attr("content"));
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
164
                }
165
            }
166
167 2
            if (!$object->description) {
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
168
                $descriptionElement = $crawler->filter("p")->first();
169
                if ($descriptionElement->count()) {
170
                    $object->description = trim($descriptionElement->text());
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
171
                }
172
            }
173 2
        }
174
175 13
        return $object;
0 ignored issues
show
Bug introduced by
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
176
    }
177
}
178