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); |
|
|
|
|
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
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: