PathValidator::isValid()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 1
dl 0
loc 32
ccs 19
cts 19
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Validators;
6
7
use Gacela\Router\Entities\RouteParams;
8
9
final class PathValidator
10
{
11 109
    public static function isValid(string $path): bool
12
    {
13 109
        if ($path === '/') {
14 2
            return true;
15
        }
16
17 109
        if ($path === '') {
18 1
            return false;
19
        }
20
21 108
        if (str_starts_with($path, '/')) {
22 1
            return false;
23
        }
24
25 107
        if (str_ends_with($path, '/')) {
26 1
            return false;
27
        }
28
29 106
        $parts = explode('/', $path);
30 106
        $optionalParamFound = false;
31
32 106
        foreach ($parts as $part) {
33 106
            if (!$part) { // Empty part found
34 1
                return false;
35 106
            } elseif (preg_match(RouteParams::OPTIONAL_PARAM_PATTERN, $part)) { // Optional argument found
36 10
                $optionalParamFound = true;
37 106
            } elseif ($optionalParamFound) { // Mandatory argument or static part found after an optional argument
38 2
                return false;
39
            }
40
        }
41
42 103
        return true;
43
    }
44
}
45