Completed
Push — develop ( a92196...990136 )
by Stéphane
02:11
created

Expression::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Bee4\RobotsTxt;
4
5
/**
6
 * Class Expression
7
 * Represent a matching expression rule
8
 *
9
 * @copyright Bee4 2016
10
 * @author    Stephane HULARD <[email protected]>
11
 */
12
class Expression
13
{
14
    /**
15
     * Raw definition
16
     * @var string
17
     */
18
    private $raw;
19
20
    /**
21
     * Rule pattern
22
     * @var string
23
     */
24
    private $pattern;
25
26
    /**
27
     * Initialize expression
28
     * @param string $rule
29
     */
30
    public function __construct($rule, $operator = self::ALLOW)
0 ignored issues
show
Unused Code introduced by
The parameter $operator 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...
31
    {
32
        $this->raw = $rule;
33
    }
34
35
    /**
36
     * Retrieve the raw rule definition
37
     * @return string
38
     */
39
    public function getRaw()
40
    {
41
        return $this->raw;
42
    }
43
44
    /**
45
     * Transform current pattern to be used for matching
46
     * @param string $raw
0 ignored issues
show
Bug introduced by
There is no parameter named $raw. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     * @return string
48
     */
49
    private function build()
50
    {
51
        $raw = $this->raw;
52
53
        $ended = substr($raw, -1) === '$';
54
        $raw = rtrim($raw, '*');
55
        $raw = rtrim($raw, '$');
56
57
        $parts = explode('*', $raw);
58
        array_walk($parts, function (&$part) {
59
            $part = preg_quote($part, '/');
60
        });
61
        return implode('.*', $parts).($ended?'':'.*');
62
    }
63
64
    /**
65
     * Check if current expression is contained in another
66
     * @param  Expression $exp
67
     * @return boolean
68
     */
69
    public function contained(Expression $exp)
70
    {
71
        return $exp->contains($this);
72
    }
73
74
    /**
75
     * Check if current expression contains another
76
     * @param  Expression $exp
77
     * @return boolean
78
     */
79
    public function contains(Expression $exp)
80
    {
81
        return preg_match('/^'.(string)$this.'$/', $exp->getRaw()) === 1;
82
    }
83
84
    /**
85
     * Retrieve the regex pattern corresponding to the Expression
86
     * @return string
87
     */
88
    public function getPattern()
89
    {
90
        $this->pattern = $this->pattern ?: $this->build();
91
        return $this->pattern;
92
    }
93
94
    /**
95
     * Transform expression to string
96
     * @return string
97
     */
98
    public function __toString()
99
    {
100
        return $this->getPattern();
101
    }
102
}
103