Completed
Pull Request — master (#3)
by Бабичев
03:59
created

RegExp::quote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Router;
4
5
class RegExp
6
{
7
8
    /**
9
     * @var null|string
10
     */
11
    protected $value;
12
13
    /**
14
     * @var string
15
     */
16
    protected $raw;
17
18
    /**
19
     * RegExp constructor.
20
     *
21
     * @param string $raw
22
     * @param null|string $value
23
     */
24
    public function __construct(string $raw, ?string $value = null)
25
    {
26
        $this->raw = $raw;
27
        $this->value = $value;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function raw(): string
34
    {
35
        return $this->raw;
36
    }
37
38
    /**
39
     * @return null|string
40
     */
41
    public function value(): ?string
42
    {
43
        return $this->value;
44
    }
45
46
//    public function regularExpression()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
//    {
48
//        \preg_replace_callback(
49
//            '~\<(?<key>[\w-]+)\>~',
50
//            function ($matches) {
51
//                return '(?<' . $matches['key'] . '>' . ($this->regex[$matches['key']] ?? $this->defaultRegex) . ')';
52
//            },
53
//            $path
54
//        );
55
//    }
56
57
    /**
58
     * @param string $value
59
     *
60
     * @return RegExp
61
     */
62
    public static function quote(string $value): self
63
    {
64
        $path = \preg_quote($value, '()');
65
        $path = \strtr($path, [
66
            '\\(' => '(',
67
            '\\)' => ')',
68
            '\\<' => '<',
69
            '\\>' => '>',
70
        ]);
71
72
        return new static(
73
            \str_replace(')', ')?', $path),
74
            $value
75
        );
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function __toString(): string
82
    {
83
        return (string)($this->raw ?: $this->value);
84
    }
85
86
}
87