Disallow   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 63
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A getField() 4 4 1
A getValue() 4 4 1
A __toString() 4 4 1
A getRegex() 14 14 1

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\Directive;
13
14
use Bisarca\RobotsTxt\Exception\InvalidDirectiveException;
15
16
/**
17
 * "Disallow" directive element.
18
 */
19 View Code Duplication
class Disallow implements GroupMemberInterface, PathDirectiveInterface
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...
20
{
21
    /**
22
     * Directive value.
23
     *
24
     * @var string
25
     */
26
    private $value = '';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct(string $raw)
32
    {
33
        if (!preg_match('/^disallow:\s*([^# ]+).*/i', $raw, $matches)) {
34
            throw InvalidDirectiveException::create($raw);
35
        }
36
37
        $this->value = trim($matches[1]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function getField(): string
44
    {
45
        return 'disallow';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getValue(): string
52
    {
53
        return $this->value;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function __toString(): string
60
    {
61
        return sprintf('Disallow: %s', $this->value);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getRegex(): string
68
    {
69
        // "*" matches any sequence of characters (zero or more)
70
        $value = str_replace('*', '.*', $this->value);
71
72
        // "?" matches any character
73
        $value = str_replace('?', '*', $value);
74
75
        // "\" supresses syntactic significance of a special character
76
        $value = str_replace('\*', '\?', $value);
77
        $value = str_replace('\.*', '\*', $value);
78
79
        return $value;
80
    }
81
}
82