RegexParser   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 42
dl 0
loc 91
rs 10
c 0
b 0
f 0
ccs 39
cts 39
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 28 8
B parseStaticPrefix() 0 44 8
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Security\Http;
12
13
/**
14
 * @author Yaroslav Honcharuk <[email protected]>
15
 */
16
class RegexParser
17
{
18
    private static $metaCharacters = '\\.+*?|^$[](){}';
19
20
    private static $removePreviousMetaCharacters = '+*?{|';
21
22
    /**
23
     * @param string $expression
24
     *
25
     * @return array
26
     */
27 40
    public function parse($expression)
28
    {
29 40
        $hasStringStartAssert = false;
30 40
        $hasStringEndAssert = false;
31 40
        $dynamicPartIsWildcard = false;
32
33 40
        if ('^' === $expression[0]) {
34 18
            $hasStringStartAssert = true;
35 18
            $expression = substr($expression, 1);
36
        }
37
38 40
        $lastIndex = strlen($expression) - 1;
39 40
        if ('$' === $expression[$lastIndex] && (!isset($expression[$lastIndex - 1]) || '\\' !== $expression[$lastIndex - 1])) {
40 4
            $hasStringEndAssert = true;
41 4
            $expression = substr($expression, 0, -1);
42
        }
43
44 40
        list($staticPrefix, $dynamicPart) = $this->parseStaticPrefix($expression);
45
46 40
        if (('' === $dynamicPart && !$hasStringEndAssert) || '.*' === $dynamicPart) {
47 24
            $dynamicPartIsWildcard = true;
48
        }
49
50
        return [
51 40
            'hasStringStartAssert' => $hasStringStartAssert,
52 40
            'hasStringEndAssert' => $hasStringEndAssert,
53 40
            'staticPrefix' => $staticPrefix,
54 40
            'dynamicPartIsWildcard' => $dynamicPartIsWildcard,
55
        ];
56
    }
57
58
    /**
59
     * @param string $expression
60
     *
61
     * @return array
62
     */
63 40
    private function parseStaticPrefix($expression)
64
    {
65 40
        $prefix = '';
66
67 40
        for ($i = 0; $i < strlen($expression); $i++) {
68 40
            $symbol = $expression[$i];
69
70 40
            if (isset($expression[$i + 1])) {
71 40
                $nextSymbol = $expression[$i + 1];
72
73 40
                if ('\\' === $symbol) {
74 10
                    if (false !== strpos(self::$metaCharacters, $nextSymbol)) {
75
                        // Escaped meta character symbol, i.e. "."
76 6
                        $prefix .= $nextSymbol;
77 6
                        $i++;
78 6
                        continue;
79
                    }
80
81
                    // Some special structure, like non-printing character / character type / assertion
82 5
                    break;
83
                }
84
85 36
                if (false !== strpos(self::$removePreviousMetaCharacters, $nextSymbol)) {
86
                    // Quantifier or alternative branch
87 8
                    break;
88
                }
89
            }
90
91 36
            if (false !== strpos(self::$metaCharacters, $symbol)) {
92 5
                break;
93
            }
94
95 36
            $prefix .= $symbol;
96
        }
97
98 40
        if (strlen($expression) === $i) {
99
            // Before PHP 7.0, if string is equal to start characters long, FALSE was returned.
100
            // @see http://php.net/manual/en/function.substr.php
101 22
            $dynamicPart = '';
102
        } else {
103 18
            $dynamicPart = substr($expression, $i);
104
        }
105
106 40
        return [$prefix, $dynamicPart];
107
    }
108
}
109