Completed
Branch master (ed4a79)
by Jan-Petter
01:36
created

AllowParser::removeOverlapping()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 16
rs 8.8571
cc 6
eloc 9
nc 2
nop 0
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\AllowClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class AllowParser
17
 *
18
 * @package vipnytt\RobotsTxtParser\Parser\Directives
19
 */
20
class AllowParser implements ParserInterface, RobotsTxtInterface
21
{
22
    /**
23
     * Directive
24
     * @var string
25
     */
26
    private $directive;
27
28
    /**
29
     * Path
30
     * @var array
31
     */
32
    private $path = [];
33
34
    /**
35
     * Sort result
36
     * @var bool
37
     */
38
    private $sort = false;
39
40
    /**
41
     * AllowParser constructor
42
     *
43
     * @param string $base
44
     * @param string $effective
45
     * @param string $directive
46
     */
47
    public function __construct($base, $effective, $directive)
0 ignored issues
show
Unused Code introduced by
The parameter $base is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $effective is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        $this->directive = $directive;
50
    }
51
52
    /**
53
     * Add
54
     *
55
     * @param string $line
56
     * @return bool
57
     */
58
    public function add($line)
59
    {
60
        $line = rtrim($line, '*');
61
        if (in_array(substr($line, 0, 1), [
62
                '/',
63
                '*',
64
                '?',
65
            ]) &&
66
            !in_array($line, $this->path)
67
        ) {
68
            $this->path[] = $line;
69
            return true;
70
        }
71
        return false;
72
    }
73
74
    /**
75
     * Render
76
     *
77
     * @param RenderHandler $handler
78
     * @return bool
79
     */
80
    public function render(RenderHandler $handler)
81
    {
82
        if ($this->directive === self::DIRECTIVE_DISALLOW &&
83
            count($this->path) === 0 &&
84
            $handler->getLevel() == 2
85
        ) {
86
            $handler->add($this->directive, '');
87
            return true;
88
        }
89
        $this->sort();
90
        foreach ($this->path as $path) {
91
            $handler->add($this->directive, $path);
92
        }
93
        return true;
94
    }
95
96
    /**
97
     * Sort by length
98
     *
99
     * @return bool
100
     */
101
    private function sort()
102
    {
103
        if ($this->sort) {
104
            return $this->sort;
105
        };
106
        return $this->sort = rsort($this->path) && usort($this->path, function ($a, $b) {
107
                // PHP 7: Switch to the <=> "Spaceship" operator
108
                return mb_strlen($a) - mb_strlen($b);
109
            });
110
    }
111
112
    /**
113
     * Client
114
     *
115
     * @return AllowClient
116
     */
117
    public function client()
118
    {
119
        $this->sort();
120
        return new AllowClient($this->path);
121
    }
122
}
123