Completed
Push — master ( b5f967...f3d3e2 )
by Jan-Petter
01:55
created

Parser::optimizeURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\UserAgentParser;
5
6
class Parser extends Core
7
{
8
    /**
9
     * HTTP status code parser
10
     * @var StatusCodeParser
11
     */
12
    protected $statusCodeParser;
13
14
    protected $origin;
15
    protected $statusCode;
16
17
    /**
18
     * Parser constructor.
19
     *
20
     * @param string $robotsTxtURL
21
     * @param int|null $statusCode
22
     * @param string|null $content
23
     * @param string $encoding
24
     * @param int $byteLimit
25
     */
26
    public function __construct($robotsTxtURL, $statusCode, $content, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT)
27
    {
28
        parent::__construct($content, $encoding, $byteLimit);
29
        $this->origin = $robotsTxtURL;
30
        $this->statusCode = $statusCode;
31
    }
32
33
    /**
34
     * Get sitemaps
35
     *
36
     * @return array
37
     */
38
    public function getSitemaps()
39
    {
40
        return $this->sitemap->export();
41
    }
42
43
    /**
44
     * Get host
45
     *
46
     * @return string|null
47
     */
48
    public function getHost()
49
    {
50
        return $this->host->export();
51
    }
52
53
    /**
54
     * Get Clean-param
55
     *
56
     * @return array
57
     */
58
    public function getCleanParam()
59
    {
60
        return $this->cleanParam->export();
61
    }
62
63
    public function userAgent($string = self::USER_AGENT)
64
    {
65
        $uaParser = new UserAgentParser($string);
66
        $userAgent = $uaParser->match($this->userAgent->userAgents, self::USER_AGENT);
67
        return new UserAgentClient([
68
            self::DIRECTIVE_DISALLOW => $this->userAgent->{self::DIRECTIVE_DISALLOW}[$userAgent],
69
            self::DIRECTIVE_ALLOW => $this->userAgent->{self::DIRECTIVE_ALLOW}[$userAgent],
70
        ], $userAgent, $this->origin, $this->statusCode);
0 ignored issues
show
Security Bug introduced by
It seems like $userAgent defined by $uaParser->match($this->...ents, self::USER_AGENT) on line 66 can also be of type false; however, vipnytt\RobotsTxtParser\...ntClient::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
71
    }
72
}
73