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

HostClient   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 12
c 5
b 1
f 0
lcom 1
cbo 1
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isListed() 0 21 4
A export() 0 7 3
A isPreferred() 0 4 2
A get() 0 4 2
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