Passed
Pull Request — master (#196)
by Rustam
04:40 queued 01:50
created

MatchingResult::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 array<string,string>
14
     */
15
    private array $arguments = [];
16
17
    /**
18
     * @var string[]
19
     */
20
    private array $methods = [];
21
22 18
    private function __construct(private ?Route $route)
23
    {
24 18
    }
25
26
    /**
27
     * @param array<string,string> $arguments
28
     */
29 11
    public static function fromSuccess(Route $route, array $arguments): self
30
    {
31 11
        $new = new self($route);
32 11
        $new->arguments = $arguments;
33 11
        return $new;
34
    }
35
36
    /**
37
     * @param string[] $methods
38
     */
39 7
    public static function fromFailure(array $methods): self
40
    {
41 7
        $new = new self(null);
42 7
        $new->methods = $methods;
43 7
        return $new;
44
    }
45
46
    /**
47
     * @psalm-assert-if-true !null $this->route
48
     */
49 14
    public function isSuccess(): bool
50
    {
51 14
        return $this->route !== null;
52
    }
53
54 16
    public function isMethodFailure(): bool
55
    {
56 16
        return $this->route === null && $this->methods !== Method::ALL;
57
    }
58
59
    /**
60
     * @return array<string,string>
61
     */
62 11
    public function arguments(): array
63
    {
64 11
        return $this->arguments;
65
    }
66
67
    /**
68
     * @return string[]
69
     */
70 4
    public function methods(): array
71
    {
72 4
        return $this->methods;
73
    }
74
75
    /**
76
     * @psalm-assert-if-true !null $this->route
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