Completed
Push — develop ( 990136...183c36 )
by Stéphane
02:08
created

Expression   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 91
ccs 20
cts 24
cp 0.8333
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A contained() 0 4 1
A contains() 0 4 1
A __construct() 0 4 1
A getRaw() 0 4 1
A build() 0 14 2
A getPattern() 0 5 2
A __toString() 0 4 1
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 3
    public function __construct($rule)
31
    {
32 3
        $this->raw = $rule;
33 3
    }
34
35
    /**
36
     * Retrieve the raw rule definition
37
     * @return string
38
     */
39 2
    public function getRaw()
40
    {
41 2
        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 2
    private function build()
50
    {
51 2
        $raw = $this->raw;
52
53 2
        $ended = substr($raw, -1) === '$';
54 2
        $raw = rtrim($raw, '*');
55 2
        $raw = rtrim($raw, '$');
56
57 2
        $parts = explode('*', $raw);
58 2
        array_walk($parts, function (&$part) {
59 2
            $part = preg_quote($part, '/');
60 2
        });
61 2
        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 2
    public function getPattern()
89
    {
90 2
        $this->pattern = $this->pattern ?: $this->build();
91 2
        return $this->pattern;
92
    }
93
94
    /**
95
     * Transform expression to string
96
     * @return string
97
     */
98 2
    public function __toString()
99
    {
100 2
        return $this->getPattern();
101
    }
102
}
103