Completed
Push — master ( b3b57c...1d1c50 )
by Jan-Petter
02:44 queued 58s
created

DisAllow::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ParserException;
5
use vipnytt\RobotsTxtParser\ObjectTools;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class DisAllow
10
 *
11
 * @package vipnytt\RobotsTxtParser\Directives
12
 */
13
class DisAllow implements DirectiveInterface, RobotsTxtInterface
14
{
15
    use ObjectTools;
16
17
    /**
18
     * Directive alternatives
19
     */
20
    const DIRECTIVE = [
21
        self::DIRECTIVE_ALLOW,
22
        self::DIRECTIVE_DISALLOW,
23
    ];
24
25
    /**
26
     * Sub directives white list
27
     */
28
    const SUB_DIRECTIVES = [
29
        self::DIRECTIVE_CLEAN_PARAM,
30
        self::DIRECTIVE_HOST,
31
    ];
32
33
    /**
34
     * Directive
35
     */
36
    protected $directive;
37
38
    /**
39
     * Rule array
40
     * @var array
41
     */
42
    protected $array = [];
43
44
    /**
45
     * Sub-directive Clean-param
46
     * @var CleanParam
47
     */
48
    protected $cleanParam;
49
50
    /**
51
     * Sub-directive Host
52
     * @var Host
53
     */
54
    protected $host;
55
56
    /**
57
     * DisAllow constructor
58
     *
59
     * @param string $directive
60
     * @throws ParserException
61
     */
62
    public function __construct($directive)
63
    {
64 View Code Duplication
        if (!in_array($directive, self::DIRECTIVE, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            throw new ParserException('Directive not allowed here, has to be `' . self::DIRECTIVE_ALLOW . '` or `' . self::DIRECTIVE_DISALLOW . '`');
66
        }
67
        $this->directive = mb_strtolower($directive);
68
        $this->cleanParam = new CleanParam();
69
        $this->host = new Host();
70
    }
71
72
    /**
73
     * Add
74
     *
75
     * @param string $line
76
     * @return bool
77
     */
78
    public function add($line)
79
    {
80
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
81
        switch ($pair['directive']) {
82
            case self::DIRECTIVE_CLEAN_PARAM:
83
                return $this->cleanParam->add($pair['value']);
84
            case self::DIRECTIVE_HOST:
85
                return $this->host->add($pair['value']);
86
        }
87
        return $this->addPath($line);
88
    }
89
90
    /**
91
     * Add plain path to allow/disallow
92
     *
93
     * @param string $rule
94
     * @return bool
95
     */
96
    protected function addPath($rule)
97
    {
98
        // Return an array of paths
99
        if (isset($this->array['path']) && in_array($rule, $this->array['path'])) {
100
            return false;
101
        }
102
        $this->array['path'][] = $rule;
103
        return true;
104
    }
105
106
    /**
107
     * Check
108
     *
109
     * @param  string $url
110
     * @return bool
111
     */
112
    public function check($url)
113
    {
114
        $path = $this->getPath($url);
115
        return (
116
            $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) ||
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->getPath($url) on line 114 can also be of type false; however, vipnytt\RobotsTxtParser\ObjectTools::checkPath() 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...
117
            $this->cleanParam->check($path) ||
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->getPath($url) on line 114 can also be of type false; however, vipnytt\RobotsTxtParser\...ves\CleanParam::check() 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...
118
            $this->host->check($url)
119
        );
120
    }
121
122
    /**
123
     * Export
124
     *
125
     * @return array
126
     */
127
    public function export()
128
    {
129
        $result = $this->array
130
            + $this->cleanParam->export()
131
            + $this->host->export();
132
        return empty($result) ? [] : [$this->directive => $result];
133
    }
134
}
135