Explorer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 85
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A discover() 0 19 3
A extractFeeds() 0 15 3
A isFeedLink() 0 5 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo;
12
13
use FeedIo\Adapter\ClientInterface;
14
use Psr\Log\LoggerInterface;
15
16
class Explorer
17
{
18
19
    /**
20
     * @var \FeedIo\Adapter\ClientInterface;
21
     */
22
    protected $client;
23
24
    /**
25
     * @var \Psr\Log\LoggerInterface
26
     */
27
    protected $logger;
28
29
    const VALID_TYPES = [
30
        'application/atom+xml',
31
        'application/rss+xml'
32
    ];
33
34
    /**
35
     * @param \FeedIo\Adapter\ClientInterface $client
36
     * @param \Psr\Log\LoggerInterface        $logger
37
     */
38 1
    public function __construct(ClientInterface $client, LoggerInterface $logger)
39
    {
40 1
        $this->client = $client;
41 1
        $this->logger = $logger;
42 1
    }
43
44
    /**
45
     * Discover feeds from the webpage's headers
46
     * @param  string $url
47
     * @return array
48
     */
49 1
    public function discover(string $url) : array
50
    {
51 1
        $this->logger->info("discover feeds from {$url}");
52 1
        $stream = $this->client->getResponse($url, new \DateTime('@0'));
53
54 1
        $internalErrors = libxml_use_internal_errors(true);
55 1
        if (LIBXML_VERSION < 20900) {
56
            $entityLoaderDisabled = libxml_disable_entity_loader(true);
57
        }
58
59 1
        $feeds = $this->extractFeeds($stream->getBody());
60
61 1
        libxml_use_internal_errors($internalErrors);
62 1
        if (LIBXML_VERSION < 20900) {
63
            libxml_disable_entity_loader($entityLoaderDisabled);
0 ignored issues
show
Bug introduced by
The variable $entityLoaderDisabled does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
64
        }
65
66 1
        return $feeds;
67
    }
68
69
    /**
70
     * Extract feeds Urls from HTML stream
71
     * @param  string $html
72
     * @return array
73
     */
74 1
    protected function extractFeeds(string $html) : array
75
    {
76 1
        $dom = new \DOMDocument();
77 1
        $dom->loadHTML($html);
78
79 1
        $links = $dom->getElementsByTagName('link');
80 1
        $feeds = [];
81 1
        foreach ($links as $link) {
82 1
            if ($this->isFeedLink($link)) {
83 1
                $feeds[] = $link->getAttribute('href');
84
            }
85
        }
86
87 1
        return $feeds;
88
    }
89
90
    /**
91
     * Tells if the given Element contains a valid Feed Url
92
     * @param  DomElement $element
93
     * @return bool
94
     */
95 1
    protected function isFeedLink(\DomElement $element) : bool
96
    {
97 1
        return $element->hasAttribute('type')
98 1
                && in_array($element->getAttribute('type'), self::VALID_TYPES);
99
    }
100
}
101