RouteSimplifier::simplifyRoute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Helper;
15
16
use InvalidArgumentException;
17
18
use function str_replace;
19
20
/**
21
 * @since 3.0.0
22
 */
23
final class RouteSimplifier
24
{
25
    /**
26
     * @throws InvalidArgumentException
27
     */
28 10
    public static function simplifyRoute(string $route): string
29
    {
30 10
        $variables = RouteParser::parseRoute($route);
31
32 10
        $search = [];
33 10
        $replace = [];
34 10
        foreach ($variables as $variable) {
35 3
            $search[] = $variable['statement'];
36 3
            $replace[] = '{' . $variable['name'] . '}';
37
        }
38
39
        // will be replaced by an empty string:
40
        // https://github.com/php/php-src/blob/a04577fb4ab5e1ebc7779608523b95ddf01e6c7f/ext/standard/string.c#L4406-L4408
41 10
        $search[] = '(';
42 10
        $search[] = ')';
43
44 10
        return str_replace($search, $replace, $route);
45
    }
46
}
47