Passed
Push — master ( f908b9...b51215 )
by Alexander
01:21
created

Group::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Yiisoft\Router;
4
5
use InvalidArgumentException;
6
use Psr\Http\Server\MiddlewareInterface;
7
use Yiisoft\Router\Middleware\Callback;
8
9
class Group implements RouteCollectorInterface
10
{
11
    protected array $items = [];
12
    protected ?string $prefix;
13
    protected array $middlewares = [];
14
15 1
    public function __construct(?string $prefix = null, ?callable $callback = null)
16
    {
17 1
        $this->prefix = $prefix;
18
19 1
        if ($callback !== null) {
20
            $callback($this);
21
        }
22
    }
23
24
    final public function addRoute(Route $route): void
25
    {
26
        $this->items[] = $route;
27
    }
28
29
    final public function addGroup(string $prefix, callable $callback): void
30
    {
31
        $this->items[] = new Group($prefix, $callback);
32
    }
33
34
    /**
35
     * @param callable|MiddlewareInterface $middleware
36
     * @return $this
37
     */
38 1
    final public function addMiddleware($middleware): self
39
    {
40 1
        if (is_callable($middleware)) {
41
            $middleware = new Callback($middleware);
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type Psr\Http\Server\MiddlewareInterface; however, parameter $callback of Yiisoft\Router\Middleware\Callback::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

41
            $middleware = new Callback(/** @scrutinizer ignore-type */ $middleware);
Loading history...
42
        }
43
44 1
        if (!$middleware instanceof MiddlewareInterface) {
45
            throw new InvalidArgumentException('Parameter should be either a PSR middleware or a callable.');
46
        }
47
48 1
        $this->middlewares[] = $middleware;
49
50 1
        return $this;
51
    }
52
53
    final public function getItems(): array
54
    {
55
        return $this->items;
56
    }
57
58
    final public function getPrefix(): ?string
59
    {
60
        return $this->prefix;
61
    }
62
63 1
    final public function getMiddlewares(): array
64
    {
65 1
        return $this->middlewares;
66
    }
67
}
68