Test Failed
Push — master ( d3660e...c7a4a9 )
by Divine Niiquaye
10:08
created

ExpressionCollection::getRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Matchers;
19
20
/**
21
 * Prefix tree of routes preserving routes order.
22
 *
23
 * @author Frank de Jonge <[email protected]>
24
 * @author Nicolas Grekas <[email protected]>
25
 *
26
 * @internal
27
 */
28
class ExpressionCollection
29
{
30
    /** @var string */
31
    private $prefix;
32
33
    /** @var string[] */
34
    private $staticPrefixes = [];
35
36
    /** @var string[] */
37
    private $prefixes = [];
38
39
    /** @var array[]|self[] */
40
    private $items = [];
41
42
    public function __construct(string $prefix = '/')
43
    {
44
        $this->prefix = $prefix;
45
    }
46
47
    public function getPrefix(): string
48
    {
49
        return $this->prefix;
50
    }
51
52
    /**
53
     * @return array[]|self[]
54
     */
55
    public function getRoutes(): array
56
    {
57
        return $this->items;
58
    }
59
60
    /**
61
     * Adds a route to a group.
62
     *
63
     * @param array|self $route
64
     */
65
    public function addRoute(string $prefix, $route): void
66
    {
67
        [$prefix, $staticPrefix] = $this->getCommonPrefix($prefix, $prefix);
68
69
        for ($i = \count($this->items) - 1; 0 <= $i; --$i) {
70
            $item = $this->items[$i];
71
72
            [$commonPrefix, $commonStaticPrefix] = $this->getCommonPrefix($prefix, $this->prefixes[$i]);
73
74
            if ($this->prefix === $commonPrefix) {
75
                // the new route and a previous one have no common prefix, let's see if they are exclusive to each others
76
77
                if ($this->prefix !== $staticPrefix && $this->prefix !== $this->staticPrefixes[$i]) {
78
                    // the new route and the previous one have exclusive static prefixes
79
                    continue;
80
                }
81
82
                if ($this->prefix === $staticPrefix && $this->prefix === $this->staticPrefixes[$i]) {
83
                    // the new route and the previous one have no static prefix
84
                    break;
85
                }
86
87
                if ($this->prefixes[$i] !== $this->staticPrefixes[$i] && $this->prefix === $this->staticPrefixes[$i]) {
88
                    // the previous route is non-static and has no static prefix
89
                    break;
90
                }
91
92
                if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) {
93
                    // the new route is non-static and has no static prefix
94
                    break;
95
                }
96
97
                continue;
98
            }
99
100
            if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) {
101
                // the new route is a child of a previous one, let's nest it
102
                $item->addRoute($prefix, $route);
103
            } else {
104
                // the new route and a previous one have a common prefix, let's merge them
105
                $child                                           = new self($commonPrefix);
106
                [$child->prefixes[0], $child->staticPrefixes[0]] = $child->getCommonPrefix($this->prefixes[$i], $this->prefixes[$i]);
107
                [$child->prefixes[1], $child->staticPrefixes[1]] = $child->getCommonPrefix($prefix, $prefix);
108
                $child->items                                    = [$this->items[$i], $route];
109
110
                $this->staticPrefixes[$i] = $commonStaticPrefix;
111
                $this->prefixes[$i]       = $commonPrefix;
112
                $this->items[$i]          = $child;
113
            }
114
115
            return;
116
        }
117
118
        // No optimised case was found, in this case we simple add the route for possible
119
        // grouping when new routes are added.
120
        $this->staticPrefixes[] = $staticPrefix;
121
        $this->prefixes[]       = $prefix;
122
        $this->items[]          = $route;
123
    }
124
125
    /**
126
     * @param int $type
127
     * @param string $msg
128
     *
129
     * @return bool
130
     */
131
    public static function handleError($type, $msg)
132
    {
133
        return false !== \strpos($msg, 'Compilation failed: lookbehind assertion is not fixed length');
134
    }
135
136
    /**
137
     * Gets the full and static common prefixes between two route patterns.
138
     *
139
     * The static prefix stops at last at the first opening bracket.
140
     */
141
    private function getCommonPrefix(string $prefix, string $anotherPrefix): array
142
    {
143
        $baseLength   = \strlen($this->prefix);
144
        $end          = \min(\strlen($prefix), \strlen($anotherPrefix));
145
        $staticLength = null;
146
        \set_error_handler([__CLASS__, 'handleError']);
147
148
        for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) {
149
            if ('(' === $prefix[$i]) {
150
                $staticLength = $staticLength ?? $i;
151
152
                for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) {
153
                    if ($prefix[$j] !== $anotherPrefix[$j]) {
154
                        break 2;
155
                    }
156
157
                    if ('(' === $prefix[$j]) {
158
                        ++$n;
159
                    } elseif (')' === $prefix[$j]) {
160
                        --$n;
161
                    } elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) {
162
                        --$j;
163
164
                        break;
165
                    }
166
                }
167
168
                if (0 < $n) {
169
                    break;
170
                }
171
172
                if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) {
173
                    break;
174
                }
175
                $subPattern = \substr($prefix, $i, $j - $i);
176
177
                if ($prefix !== $anotherPrefix && !\preg_match('/^\(\[[^\]]++\]\+\+\)$/', $subPattern) && !\preg_match('{(?<!' . $subPattern . ')}', '')) {
178
                    // sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching
179
                    break;
180
                }
181
                $i = $j - 1;
182
            } elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) {
183
                --$i;
184
185
                break;
186
            }
187
        }
188
        \restore_error_handler();
189
190
        if ($i < $end && 0b10 === (\ord($prefix[$i]) >> 6) && \preg_match('//u', $prefix . ' ' . $anotherPrefix)) {
191
            do {
192
                // Prevent cutting in the middle of an UTF-8 characters
193
                --$i;
194
            } while (0b10 === (\ord($prefix[$i]) >> 6));
195
        }
196
197
        return [\substr($prefix, 0, $i), \substr($prefix, 0, $staticLength ?? $i)];
198
    }
199
}
200