Completed
Push — master ( a7ec5f...7812c9 )
by Jan-Petter
02:03
created

Allow::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
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\ObjectTools;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
use vipnytt\RobotsTxtParser\UrlToolbox;
7
8
/**
9
 * Class Allow
10
 *
11
 * @package vipnytt\RobotsTxtParser\Directives
12
 */
13 View Code Duplication
class Allow implements DirectiveInterface, RobotsTxtInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
14
{
15
    use UrlToolbox;
16
    use ObjectTools;
17
18
    const SUB_DIRECTIVES = [
19
        self::DIRECTIVE_CLEAN_PARAM,
20
        self::DIRECTIVE_HOST,
21
    ];
22
23
    /**
24
     * Directive
25
     */
26
    const DIRECTIVE = 'Allow';
27
28
    protected $array = [];
29
    protected $parent;
30
31
    protected $cleanParam;
32
    protected $host;
33
34
35
    public function __construct($array, $parent = null)
36
    {
37
        $this->array = $array;
0 ignored issues
show
Documentation Bug introduced by
It seems like $array of type string is incompatible with the declared type array of property $array.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->cleanParam = new CleanParam([], self::DIRECTIVE);
39
        $this->host = new Host([], self::DIRECTIVE);
40
    }
41
42
    /**
43
     * Add
44
     *
45
     * @param string $line
46
     * @return bool
47
     */
48
    public function add($line)
49
    {
50
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
51
        switch ($pair['directive']) {
52
            case self::DIRECTIVE_CLEAN_PARAM:
53
                return $this->cleanParam->add($pair['value']);
54
            case self::DIRECTIVE_HOST:
55
                return $this->host->add($pair['value']);
56
        }
57
        return $this->addPath($line);
58
    }
59
60
    protected function addPath($rule)
61
    {
62
        // Return an array of paths
63
        if (isset($this->array['path']) && in_array($rule, $this->array['path'])) {
64
            return false;
65
        }
66
        $this->array['path'][] = $rule;
67
        return true;
68
    }
69
70
    /**
71
     * Check
72
     *
73
     * @param  string $url
74
     * @return bool
75
     */
76
    public function check($url)
77
    {
78
        $path = $this->getPath($url);
79
        return (
80
            $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) ||
81
            $this->cleanParam->check($path) ||
82
            $this->host->check($url)
83
        );
84
    }
85
86
    public function export()
87
    {
88
        $result = $this->array
89
            + $this->cleanParam->export()
90
            + $this->host->export();
91
        return empty($result) ? [] : [self::DIRECTIVE => $result];
92
    }
93
}
94