Test Failed
Push — master ( 0ee32e...41c938 )
by Dan
07:10
created

Route::withNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the PSR Http Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Ds\Router;
11
12
use Ds\Router\Interfaces\RouteInterface;
13
14
/**
15
 * Route Class
16
 *
17
 * @package Rs\Router
18
 * @author  Dan Smith    <[email protected]>
19
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link    https://github.com/djsmithme/Router
21
 *
22
 * @see RouteInterface
23
 * @see RouteCollectionInterface
24
 */
25
class Route implements RouteInterface
26
{
27
    /**
28
     * Route Http Method
29
     *
30
     * @var string
31
     */
32
    protected $method;
33
34
    /**
35
     * Route Path Pattern
36
     *
37
     * @var string
38
     */
39
    protected $pattern;
40
41
    /**
42
     * Route Handler
43
     *
44
     * @var string|\Closure
45
     */
46
    protected $handler;
47
48
    /**
49
     * Route Names
50
     *
51
     * @var array $routeNames
52
     */
53
    protected $names;
54
55
    /**
56
     * Create a new route.
57
     *
58
     * @param string|array $method Route Http Method
59
     * @param string $pattern Route Path / Pattern
60
     * @param string|\Closure $handler Route Handler.
61
     * @param array $names Route Names
62
     */
63
    public function __construct($method, $pattern, $handler, array $names = [])
64
    {
65
        $this->method = $method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $method can also be of type array. However, the property $method is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66
        $this->pattern = $pattern;
67
        $this->handler = $handler;
68
        $this->names = $names;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function withMethod($method = '')
75
    {
76
        $new = clone $this;
77
        $new->method = (string)$method;
78
        return $new;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function withHandler($handler)
85
    {
86
        $new = clone $this;
87
        $new->handler = $handler;
88
        return $new;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function withPattern($pattern = '')
95
    {
96
        $new = clone $this;
97
        $new->pattern = (string)$pattern;
98
        return $new;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function withNames(array $name = [])
105
    {
106
        $new = clone $this;
107
        $new->names = $name;
108
        return $new;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function getMethod()
115
    {
116
        return (string)$this->method;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function getHandler()
123
    {
124
        return $this->handler;
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function getHandlerType()
131
    {
132
        return \gettype($this->handler);
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public function getPattern()
139
    {
140
        return (string)$this->pattern;
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146
    public function getNames()
147
    {
148
        return (array)$this->names;
149
    }
150
}
151