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

UserAgent   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B add() 0 15 5
A check() 0 10 3
A export() 0 9 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\ObjectTools;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class UserAgent
9
 *
10
 * @package vipnytt\RobotsTxtParser\Directives
11
 */
12
class UserAgent implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use ObjectTools;
15
16
    const SUB_DIRECTIVES = [
17
        self::DIRECTIVE_ALLOW,
18
        self::DIRECTIVE_CACHE_DELAY,
19
        self::DIRECTIVE_CRAWL_DELAY,
20
        self::DIRECTIVE_DISALLOW,
21
    ];
22
23
    /**
24
     * Directive
25
     */
26
    const DIRECTIVE = 'User-agent';
27
28
    protected $parent;
29
    protected $array = [];
30
31
    protected $allow;
32
    protected $cacheDelay;
33
    protected $crawlDelay;
34
    protected $disallow;
35
36
    public function __construct($array, $parent = null)
37
    {
38
        $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...
39
        $this->allow = new Allow([], self::DIRECTIVE);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
        $this->cacheDelay = new CacheDelay([], self::DIRECTIVE);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
        $this->crawlDelay = new CrawlDelay([], self::DIRECTIVE);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
        $this->disallow = new Disallow([], self::DIRECTIVE);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44
45
    /**
46
     * Add
47
     *
48
     * @param string $line
49
     * @return bool
50
     */
51
    public function add($line)
52
    {
53
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
54
        switch ($pair['directive']) {
55
            case self::DIRECTIVE_ALLOW:
56
                return $this->allow->add($pair['value']);
57
            case self::DIRECTIVE_CACHE_DELAY:
58
                return $this->cacheDelay->add($pair['value']);
59
            case self::DIRECTIVE_CRAWL_DELAY:
60
                return $this->crawlDelay->add($pair['value']);
61
            case self::DIRECTIVE_DISALLOW:
62
                return $this->disallow->add($pair['value']);
63
        }
64
        return false;
65
    }
66
67
    /**
68
     * Check rules
69
     *
70
     * @param  string $url - URL to check
71
     * @param  string $type - directive to check
72
     * @return bool
73
     */
74
    public function check($url, $type)
75
    {
76
        $result = ($type === self::DIRECTIVE_ALLOW);
77
        foreach ([self::DIRECTIVE_DISALLOW, self::DIRECTIVE_ALLOW] as $directive) {
78
            if ($this->$directive->check($url)) {
79
                $result = ($type === $directive);
80
            }
81
        }
82
        return $result;
83
    }
84
85
    public function export()
86
    {
87
        $result = $this->array
88
            + $this->allow->export()
89
            + $this->cacheDelay->export()
90
            + $this->crawlDelay->export()
91
            + $this->disallow->export();
92
        return empty($result) ? [] : [self::DIRECTIVE => $result];
93
    }
94
}
95