Completed
Push — 2.0-dev ( 2c252e...97b412 )
by Jan-Petter
02:13
created

HostClient::isListed()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 15
nc 6
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UrlParser;
5
6
/**
7
 * Class HostClient
8
 *
9
 * @package vipnytt\RobotsTxtParser\Client\Directives
10
 */
11
class HostClient implements ClientInterface
12
{
13
    use UrlParser;
14
15
    /**
16
     * Base Uri
17
     * @var string
18
     */
19
    private $base;
20
21
    /**
22
     * Host
23
     * @var string[]
24
     */
25
    private $host;
26
27
    /**
28
     * Parent directive
29
     * @var string|null
30
     */
31
    private $parent;
32
33
    /**
34
     * HostClient constructor.
35
     *
36
     * @param string $base
37
     * @param array $host
38
     * @param string|null $parentDirective
39
     */
40
    public function __construct($base, array $host, $parentDirective = null)
41
    {
42
        $this->base = $base;
43
        $this->host = $host;
44
        $this->parent = $parentDirective;
45
    }
46
47
    /**
48
     * Check
49
     *
50
     * @param string $url
51
     * @return bool
52
     */
53
    public function isListed($url)
54
    {
55
        $url = mb_strtolower($this->urlEncode($url));
56
        $parts = [
57
            'scheme' => parse_url($url, PHP_URL_SCHEME),
58
            'host' => parse_url($url, PHP_URL_HOST),
59
        ];
60
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
61
        $cases = [
62
            $parts['host'],
63
            $parts['host'] . ':' . $parts['port'],
64
            $parts['scheme'] . '://' . $parts['host'],
65
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
66
        ];
67
        foreach ($this->export() as $host) {
0 ignored issues
show
Bug introduced by
The expression $this->export() of type string|null|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
68
            if (in_array($host, $cases)) {
69
                return true;
70
            }
71
        }
72
        return false;
73
    }
74
75
    /**
76
     * Export
77
     *
78
     * @return string[]|string|null
79
     */
80
    public function export()
81
    {
82
        if ($this->parent === null) {
83
            return isset($this->host[0]) ? $this->host[0] : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression isset($this->host[0]) ? $this->host[0] : null; of type string|null adds the type string to the return on line 83 which is incompatible with the return type declared by the interface vipnytt\RobotsTxtParser\...ClientInterface::export of type array.
Loading history...
84
        }
85
        return $this->host;
86
    }
87
88
    /**
89
     * Preferred host
90
     *
91
     * @return bool
92
     */
93
    public function isPreferred()
94
    {
95
        return empty($this->host) ? true : mb_stripos($this->urlBase($this->urlEncode($this->base)), $this->get()) !== false;
96
    }
97
98
    /**
99
     * Get
100
     *
101
     * @return string|null
102
     */
103
    public function get()
104
    {
105
        return isset($this->host[0]) ? $this->host[0] : null;
106
    }
107
}
108