Ruleset   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 12.26 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 13
loc 106
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 4 1
A has() 0 4 1
A remove() 13 13 2
B isUserAgentAllowed() 0 19 6
A getComments() 0 4 1
A getDirectives() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the bisarca/robots-txt package.
5
 *
6
 * (c) Emanuele Minotto <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bisarca\RobotsTxt;
13
14
use Bisarca\RobotsTxt\Directive\DirectiveInterface;
15
use Bisarca\RobotsTxt\Directive\PathDirectiveInterface;
16
use Generator;
17
18
/**
19
 * Set of directives.
20
 */
21
class Ruleset extends AbstractSet
22
{
23
    /**
24
     * Class constructor with optional initialization data.
25
     *
26
     * @param DirectiveInterface[] $directives
27
     */
28
    public function __construct(DirectiveInterface ...$directives)
29
    {
30
        $this->data = $directives;
31
    }
32
33
    /**
34
     * Adds a directive.
35
     *
36
     * @param DirectiveInterface $directive
37
     */
38
    public function add(DirectiveInterface $directive)
39
    {
40
        $this->data[] = $directive;
41
    }
42
43
    /**
44
     * Checks if a directive is contained.
45
     *
46
     * @param DirectiveInterface $directive
47
     *
48
     * @return bool
49
     */
50
    public function has(DirectiveInterface $directive): bool
51
    {
52
        return false !== array_search($directive, $this->data, true);
53
    }
54
55
    /**
56
     * Remove an element.
57
     *
58
     * @param DirectiveInterface $directive
59
     *
60
     * @return bool
61
     */
62 View Code Duplication
    public function remove(DirectiveInterface $directive): bool
0 ignored issues
show
Duplication introduced by
This method 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...
63
    {
64
        $key = array_search($directive, $this->data, true);
65
66
        if (false !== $key) {
67
            unset($this->data[$key]);
68
            $this->data = array_values($this->data);
69
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Checks if a user agent is allowed.
78
     *
79
     * @param string $path
80
     *
81
     * @return bool
82
     */
83
    public function isUserAgentAllowed(
84
        string $path = PathDirectiveInterface::DEFAULT_PATH
85
    ): bool {
86
        $path = trim($path) ?: PathDirectiveInterface::DEFAULT_PATH;
87
88
        foreach ($this->data as $directive) {
89
            if (
90
                $directive instanceof PathDirectiveInterface &&
91
                (
92
                    $directive->getValue() == $path ||
93
                    preg_match('#'.$directive->getRegex().'#', $path)
94
                )
95
            ) {
96
                return $directive instanceof Directive\Allow;
97
            }
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * Gets ruleset comments for the developer.
105
     *
106
     * @return Generator
107
     */
108
    public function getComments(): Generator
109
    {
110
        yield from $this->getDirectives(Directive\Comment::class);
111
    }
112
113
    /**
114
     * Gets directives of a certain type.
115
     *
116
     * @param string|null $class
117
     *
118
     * @return DirectiveInterface[]
119
     */
120
    public function getDirectives(string $class = null): array
121
    {
122
        return array_filter($this->data, function ($element) use ($class) {
123
            return null === $class || $element instanceof $class;
124
        });
125
    }
126
}
127