Completed
Push — master ( 27d2f2...3153a3 )
by Changwan
04:10
created

Router::domain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Router;
3
4
use IteratorAggregate;
5
use Wandu\Router\Contracts\Route as RouteInterface;
6
use Wandu\Router\Contracts\Router as RouterInterface;
7
8
class Router implements IteratorAggregate, RouterInterface 
9
{
10
    /** @var \Wandu\Router\Route[] */
11
    protected $routes = [];
12
13
    /** @var string */
14
    protected $domains = [];
15
    
16
    /** @var string */
17
    protected $prefix = '';
18
19
    /** @var array */
20
    protected $middlewares = [];
21
22
    /**
23
     * @param string $prefix
24
     * @param callable $handler
25
     */
26 4
    public function prefix(string $prefix, callable $handler)
27
    {
28 4
        $beforePrefix = $this->prefix;
29 4
        $this->prefix = "{$beforePrefix}/" . ($prefix ?? '');
30
31 4
        call_user_func($handler, $this);
32
33 4
        $this->prefix = $beforePrefix;
34 4
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function domain($domain, callable $handler)
40
    {
41 1
        $domains = array_filter((array) $domain);
42 1
        $beforeDomains = $this->domains;
43 1
        $this->domains = array_merge($beforeDomains, $domains);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($beforeDomains, $domains) of type array is incompatible with the declared type string of property $domains.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44 1
        call_user_func($handler, $this);
45 1
        $this->domains = $beforeDomains;
46 1
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 26
    public function middleware($middleware, callable $handler)
52
    {
53 26
        $middlewares = array_filter((array) $middleware);
54 26
        $beforeMiddlewares = $this->middlewares;
55 26
        $this->middlewares = array_merge($beforeMiddlewares, $middlewares);
56 26
        call_user_func($handler, $this);
57 26
        $this->middlewares = $beforeMiddlewares;
58 26
    }
59
60
    /**
61
     * @param array $attributes
62
     * @param callable $handler
63
     */
64
    public function group(array $attributes, callable $handler)
65
    {
66
        $this->prefix($attributes['prefix'] ?? '', function () use ($attributes, $handler) {
67 2
            $this->middleware($attributes['middleware'] ?? [], function () use ($attributes, $handler) {
68 2
                call_user_func($handler, $this);
69 2
            });
70 2
        });
71 2
    }
72
73
    /**
74
     * @param callable $handler
75
     */
76
    public function append(callable $handler)
77
    {
78
        call_user_func($handler, $this);
79
    }
80
81
    public function resource($className, $except = [], $only = [])
0 ignored issues
show
Unused Code introduced by
The parameter $except is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $only is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $this->get('', $className, 'index');
84
        $this->get('create', $className, 'create');
85
        $this->post('', $className, 'store');
86
        $this->get('{id}', $className, 'show');
87
        $this->put('{id}', $className, 'update');
88
        $this->delete('{id}', $className, 'destroy');
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 7
    public function get(string $path, string $className, string $methodName = 'index'): RouteInterface
95
    {
96 7
        return $this->createRoute(['GET', 'HEAD'], $path, $className, $methodName);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 2
    public function post(string $path, string $className, string $methodName = 'index'): RouteInterface
103
    {
104 2
        return $this->createRoute(['POST'], $path, $className, $methodName);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    public function put(string $path, string $className, string $methodName = 'index'): RouteInterface
111
    {
112 1
        return $this->createRoute(['PUT'], $path, $className, $methodName);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function delete(string $path, string $className, string $methodName = 'index'): RouteInterface
119
    {
120 1
        return $this->createRoute(['DELETE'], $path, $className, $methodName);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function options(string $path, string $className, string $methodName = 'index'): RouteInterface
127
    {
128 1
        return $this->createRoute(['OPTIONS'], $path, $className, $methodName);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function patch(string $path, string $className, string $methodName = 'index'): RouteInterface
135
    {
136 1
        return $this->createRoute(['PATCH'], $path, $className, $methodName);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function any(string $path, string $className, string $methodName = 'index'): RouteInterface
143
    {
144 1
        return $this->createRoute([
145 1
            'GET',
146
            'HEAD',
147
            'POST',
148
            'PUT',
149
            'DELETE',
150
            'OPTIONS',
151
            'PATCH'
152 1
        ], $path, $className, $methodName);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 27
    public function createRoute(array $methods, string $path, string $className, string $methodName = 'index'): RouteInterface
159
    {
160 27
        $path = trim("{$this->prefix}/{$path}", '/');
161 27
        while(strpos($path, '//') !== false) {
162 3
            $path = str_replace('//', '/', $path);
163
        }
164 27
        $path = '/' . $path;
165 27
        $route = new Route($className, $methodName, $this->middlewares, $this->domains);
0 ignored issues
show
Documentation introduced by
$this->domains is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166 27
        $this->routes[] = [$methods, $path, $route];
167 27
        return $route;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 27
    public function getIterator()
174
    {
175 27
        foreach ($this->routes as $route) {
176 27
            yield $route;
177
        }
178 27
    }
179
}
180