Completed
Push — master ( 0b6473...8155cd )
by Edson
01:55
created

Route::match()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Router;
4
5
/**
6
 * Class Route
7
 * @package Router
8
 */
9
class Route
10
{
11
    /**
12
     * @var BaseRouter
13
     */
14
    private $router;
15
16
    /**
17
     * @var
18
     */
19
    private static $route;
20
    /**
21
     * @var null
22
     */
23
    private static $match = null;
24
25
    /**
26
     * Route constructor.
27
     */
28
    private function __construct()
29
    {
30
        $this->router = new BaseRouter();
31
    }
32
33
    /**
34
     * @return Route
35
     */
36
    private static function route(): Route
37
    {
38
        if (!isset(self::$route) || is_null(self::$route)) {
39
            self::$route = new Route;
40
        }
41
42
        return self::$route;
43
    }
44
45
    /**
46
     * @return null|BaseRoute
47
     */
48
    private static function match(): ?BaseRoute
49
    {
50
        if (!isset(self::$match) || is_null(self::$match)) {
51
            self::$match = self::route()->router->handle();
0 ignored issues
show
Documentation Bug introduced by
It seems like self::route()->router->handle() can also be of type Router\BaseRoute. However, the property $match is declared as type null. 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...
52
        }
53
54
        return self::$match;
55
    }
56
57
    /**
58
     * @param string $method
59
     * @param string $uri
60
     * @param $callback
61
     * @return void
62
     */
63
    private function handle(string $method, string $uri, $callback): void
64
    {
65
        $this->router->add([
66
            'uri' => $uri,
67
            'name' => '',
68
            'method' => $method,
69
            'callback' => $callback
70
        ]);
71
72
        $match = $this->match();
73
74
        call_user_func_array($match->getCallback(), $match->getArgs());
75
76
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
77
    }
78
79
    /**
80
     * @param string $uri
81
     * @param $callback
82
     */
83
    public static function get(string $uri, $callback): void
84
    {
85
        self::route()->handle('get', $uri, $callback);
86
    }
87
88
    /**
89
     * @param string $uri
90
     * @param $callback
91
     */
92
    public static function post(string $uri, $callback): void
93
    {
94
        self::route()->handle('post', $uri, $callback);
95
    }
96
97
    /**
98
     * @param string $uri
99
     * @param $callback
100
     */
101
    public static function put(string $uri, $callback): void
102
    {
103
        self::route()->handle('put', $uri, $callback);
104
    }
105
106
    /**
107
     * @param string $uri
108
     * @param $callback
109
     */
110
    public static function patch(string $uri, $callback): void
111
    {
112
        self::route()->handle('patch', $uri, $callback);
113
    }
114
115
    /**
116
     * @param string $uri
117
     * @param $callback
118
     */
119
    public static function delete(string $uri, $callback): void
120
    {
121
        self::route()->handle('delete', $uri, $callback);
122
    }
123
124
    /**
125
     * @param string $uri
126
     * @param $callback
127
     */
128
    public static function options(string $uri, $callback): void
129
    {
130
        self::route()->handle('options', $uri, $callback);
131
    }
132
}
133