Passed
Push — main ( deaffd...dea3f5 )
by Chema
55s queued 13s
created

PathValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 34
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B isValid() 0 32 9
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