Passed
Pull Request — master (#60)
by
unknown
10:40
created

Router::addRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yiisoft\Router\Dispatcher\DispatcherInterface;
10
use Yiisoft\Router\Handler\HandlerAwareTrait;
11
use Yiisoft\Router\Route\RouteCollectionInterface;
12
use Yiisoft\Router\Route\RouteInterface;
13
14
use function array_merge;
15
16
final class Router implements RouterInterface, RouteCollectionInterface
17
{
18
    use HandlerAwareTrait;
19
20
    private RouteCollectionInterface $routeCollection;
21
    private MatcherInterface $matcher;
22
    private DispatcherInterface $dispatcher;
23
24
    public function __construct(RouteCollectionInterface $routeCollection, MatcherInterface $matcher, DispatcherInterface $dispatcher)
25
    {
26
        $this->routeCollection = clone $routeCollection;
27
        $this->matcher = $matcher;
28
        $this->dispatcher = $dispatcher;
29
    }
30
31
    public function getDispatcher(): DispatcherInterface
32
    {
33
        return $this->dispatcher;
34
    }
35
36
    public function withDispatcher(DispatcherInterface $dispatcher): self
37
    {
38
        $new = clone $this;
39
        $new->dispatcher = $dispatcher;
40
        return $new;
41
    }
42
43
    public function handle(ServerRequestInterface $request): ResponseInterface
44
    {
45
        $matchingResult = $this->match($request);
46
        if ($matchingResult->isSuccess()) {
47
            $route = $matchingResult->getRoute();
48
            $routeDispatcher = $route->getDispatcher();
49
            $handlers = array_merge($route->getHandlers(), $this->getHandlers());
0 ignored issues
show
Bug introduced by
$route->getHandlers() of type iterable is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $handlers = array_merge(/** @scrutinizer ignore-type */ $route->getHandlers(), $this->getHandlers());
Loading history...
50
51
            if ($routeDispatcher !== null) {
52
                return $routeDispatcher->handlers($handlers)->handle($request);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on Yiisoft\Router\Handler\HandlerAwareInterface. Did you maybe mean handler()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                return $routeDispatcher->handlers($handlers)->/** @scrutinizer ignore-call */ handle($request);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            }
54
55
            $this->dispatcher = $this->dispatcher->handlers($handlers);
56
        }
57
58
        return $this->dispatcher->handle($request);
59
    }
60
61
    public function match(ServerRequestInterface $request): MatchingResult
62
    {
63
        return $this->matchForRoutes($this->routeCollection->getRoutes(), $request);
64
    }
65
66
    public function matchForRoutes(iterable $routes, ServerRequestInterface $request): MatchingResult
67
    {
68
        return $this->matcher->matchForRoutes($routes, $request);
69
    }
70
71
    public function addRoute(RouteInterface $route): self
72
    {
73
        $this->routeCollection = $this->routeCollection->addRoute($route);
74
75
        return $this;
76
    }
77
78
    public function addRoutes(array $routes): self
79
    {
80
        // TODO: Implement addRoutes() method.
81
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Yiisoft\Router\Router. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
82
83
    public function addCollection(RouteCollectionInterface $collection): self
84
    {
85
        // TODO: Implement addCollection() method.
86
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Yiisoft\Router\Router. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
87
88
    public function getRoutes(): iterable
89
    {
90
        return $this->routeCollection->getRoutes();
91
    }
92
}
93