Passed
Pull Request — master (#225)
by Rustam
02:46
created

MatchingResult::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use RuntimeException;
8
use Yiisoft\Http\Method;
9
10
final class MatchingResult
11
{
12
    /**
13
     * @var string[]
14
     * @psalm-var array<string,string>
15
     */
16
    private array $arguments = [];
17
18
    /**
19
     * @var string[]
20
     */
21
    private array $methods = [];
22
23 18
    private function __construct(private ?Route $route)
24
    {
25 18
    }
26
27
    /**
28
     * @param string[] $arguments
29
     * @psalm-param array<string,string> $arguments
30
     */
31 11
    public static function fromSuccess(Route $route, array $arguments): self
32
    {
33 11
        $new = new self($route);
34 11
        $new->arguments = $arguments;
35 11
        return $new;
36
    }
37
38
    /**
39
     * @param string[] $methods
40
     */
41 7
    public static function fromFailure(array $methods): self
42
    {
43 7
        $new = new self(null);
44 7
        $new->methods = $methods;
45 7
        return $new;
46
    }
47
48
    /**
49
     * @psalm-assert-if-true !null $this->route
50
     */
51 14
    public function isSuccess(): bool
52
    {
53 14
        return $this->route !== null;
54
    }
55
56 16
    public function isMethodFailure(): bool
57
    {
58 16
        return $this->route === null && $this->methods !== Method::ALL;
59
    }
60
61
    /**
62
     * @return string[]
63
     * @psalm-return array<string,string>
64
     */
65 11
    public function arguments(): array
66
    {
67 11
        return $this->arguments;
68
    }
69
70
    /**
71
     * @return string[]
72
     */
73 4
    public function methods(): array
74
    {
75 4
        return $this->methods;
76
    }
77
78 11
    public function route(): Route
79
    {
80 11
        if ($this->route === null) {
81 1
            throw new RuntimeException('There is no route in the matching result.');
82
        }
83
84 10
        return $this->route;
85
    }
86
}
87