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

Disallow   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 80
loc 80
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A add() 11 11 3
A addPath() 9 9 3
A check() 9 9 4
A export() 7 7 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
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\ObjectTools;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
use vipnytt\RobotsTxtParser\UrlToolbox;
7
8
/**
9
 * Class Disallow
10
 *
11
 * @package vipnytt\RobotsTxtParser\Directives
12
 */
13 View Code Duplication
class Disallow 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 = 'Disallow';
27
28
    protected $array = [];
29
    protected $parent;
30
31
    protected $cleanParam;
32
    protected $host;
33
34
    public function __construct($array, $parent = null)
35
    {
36
        $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...
37
        $this->cleanParam = new CleanParam([], self::DIRECTIVE);
38
        $this->host = new Host([], self::DIRECTIVE);
39
    }
40
41
    /**
42
     * Add
43
     *
44
     * @param string $line
45
     * @return bool
46
     */
47
    public function add($line)
48
    {
49
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
50
        switch ($pair['directive']) {
51
            case self::DIRECTIVE_CLEAN_PARAM:
52
                return $this->cleanParam->add($pair['value']);
53
            case self::DIRECTIVE_HOST:
54
                return $this->host->add($pair['value']);
55
        }
56
        return $this->addPath($line);
57
    }
58
59
    protected function addPath($rule)
60
    {
61
        // Return an array of paths
62
        if (isset($this->array['path']) && in_array($rule, $this->array['path'])) {
63
            return false;
64
        }
65
        $this->array['path'][] = $rule;
66
        return true;
67
    }
68
69
    /**
70
     * Check
71
     *
72
     * @param  string $url
73
     * @return bool
74
     */
75
    public function check($url)
76
    {
77
        $path = $this->getPath($url);
78
        return (
79
            $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) ||
80
            $this->cleanParam->check($path) ||
81
            $this->host->check($url)
82
        );
83
    }
84
85
    public function export()
86
    {
87
        $result = $this->array
88
            + $this->cleanParam->export()
89
            + $this->host->export();
90
        return empty($result) ? [] : [self::DIRECTIVE => $result];
91
    }
92
}
93