Completed
Push — master ( 686db0...29755b )
by Mikael
01:55
created

RouterInjectable::getInternal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\Route;
4
5
/**
6
 * A container for routes.
7
 *
8
 */
9
class RouterInjectable
10
{
11
    /**
12
     * Properties
13
     *
14
     */
15
    private $routes         = [];    // All the routes
16
    private $internalRoutes = [];    // All internal routes
17
    private $lastRoute      = null;  // Last route that was callbacked
18
19
20
21
    /**
22
     * Get all routes.
23
     *
24
     * @return array with all routes.
25
     */
26
    public function getAll()
27
    {
28
        return $this->routes;
29
    }
30
31
32
33
    /**
34
     * Get all internal routes.
35
     *
36
     * @return array with internal routes.
37
     */
38
    public function getInternal()
39
    {
40
        return $this->internalRoutes;
41
    }
42
43
44
45
    /**
46
     * Add a route to the router.
47
     *
48
     * @param null|string          $rule   for this route
49
     * @param null|string|callable $action to implement a handler for the route
50
     *
51
     * @return class as new route
52
     */
53 8
    public function add($rule, $action)
54
    {
55 8
        return $this->any(null, $rule, $action);
56
    }
57
58
59
60
    /**
61
     * Add aroute to the router with specific request method.
62
     *
63
     * @param null|string|array    $method as request methods
64
     * @param null|string          $rule   for this route
65
     * @param null|string|callable $action to implement a handler for the route
66
     *
67
     * @return class as new route
68
     */
69 11
    public function any($method, $rule, $action)
70
    {
71 11
        $route = new Route();
72 11
        $route->set($rule, $action, $method);
73 11
        $this->routes[] = $route;
74
75 11
        return $route;
76
    }
77
78
79
80
    /**
81
     * Add a route to the router which will be applied for any route, if the
82
     * method is matching.
83
     *
84
     * @param null|string|array    $method as request methods
85
     * @param null|string|callable $action to implement a handler for the route
86
     *
87
     * @return class as new route
88
     */
89 1
    public function all($method, $action = null)
90
    {
91 1
        return $this->any($method, null, $action);
92
    }
93
94
95
96
    /**
97
     * Add a GET route to the router.
98
     *
99
     * @param null|string|array    $method as request methods
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
     * @param null|string|callable $action to implement a handler for the route
101
     *
102
     * @return class as new route
103
     */
104 1
    public function get($rule, $action)
105
    {
106 1
        return $this->any(["GET"], $rule, $action);
107
    }
108
109
110
111
    /**
112
     * Add a POST route to the router.
113
     *
114
     * @param null|string|array    $method as request methods
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     * @param null|string|callable $action to implement a handler for the route
116
     *
117
     * @return class as new route
118
     */
119 1
    public function post($rule, $action)
120
    {
121 1
        return $this->any(["POST"], $rule, $action);
122
    }
123
124
125
126
    /**
127
     * Add a PUT route to the router.
128
     *
129
     * @param null|string|array    $method as request methods
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
130
     * @param null|string|callable $action to implement a handler for the route
131
     *
132
     * @return class as new route
133
     */
134 1
    public function put($rule, $action)
135
    {
136 1
        return $this->any(["PUT"], $rule, $action);
137
    }
138
139
140
141
    /**
142
     * Add a DELETE route to the router.
143
     *
144
     * @param null|string|array    $method as request methods
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
145
     * @param null|string|callable $action to implement a handler for the route
146
     *
147
     * @return class as new route
148
     */
149 1
    public function delete($rule, $action)
150
    {
151 1
        return $this->any(["DELETE"], $rule, $action);
152
    }
153
154
155
156
    /**
157
     * Add an internal (not exposed to url-matching) route to the router.
158
     *
159
     * @param string               $rule   for this route
160
     * @param null|string|callable $action to implement a handler for the route
161
     *
162
     * @return class as new route
163
     */
164 3
    public function addInternal($rule, $action)
165
    {
166 3
        $route = new Route();
167 3
        $route->set($rule, $action);
168 3
        $this->internalRoutes[$rule] = $route;
169 3
        return $route;
170
    }
171
172
173
174
    /**
175
     * Handle an internal route.
176
     *
177
     * @param string $rule   for this route
178
     *
179
     * @return void
180
     *
181
     * @throws \Anax\Route\NotFoundException
182
     */
183 4
    public function handleInternal($rule)
184
    {
185 4
        if (!isset($this->internalRoutes[$rule])) {
186 1
            throw new NotFoundException("No internal route to handle: " . $rule);
187
        }
188 3
        $route = $this->internalRoutes[$rule];
189 3
        $this->lastRoute = $rule;
190 3
        $route->handle();
191
    }
192
193
194
195
    /**
196
     * Get the route for the last route that was handled.
197
     *
198
     * @return mixed
199
     */
200
    public function getLastRoute()
201
    {
202
        return $this->lastRoute;
203
    }
204
205
206
207
    /**
208
     * Handle the routes and match them towards the request, dispatch them
209
     * when a match is made. Each route handler may throw exceptions that
210
     * may redirect to an internal route for error handling.
211
     * Several routes can match and if the routehandler does not break
212
     * execution flow, the route matching will carry on.
213
     * Only the last routehandler will get its return value returned further.
214
     *
215
     * @param string $query   the query/route to match a handler for.
216
     * @param string $method  the request method to match.
217
     *
218
     * @return mixed content returned from route.
219
     */
220 12
    public function handle($query, $method = null)
221
    {
222
        try {
223 12
            $match = false;
224 12
            foreach ($this->routes as $route) {
225 11
                if ($route->match($query, $method)) {
226 11
                    $this->lastRoute = $route->getRule();
227 11
                    $match = true;
228 11
                    $results = $route->handle();
229 8
                }
230 9
            }
231
232 9
            if ($match) {
233 8
                return $results;
0 ignored issues
show
Bug introduced by
The variable $results does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
234
            }
235
236
            // No route was matched
237 1
            $this->handleInternal("404");
238 4
        } catch (ForbiddenException $e) {
239 1
            $this->handleInternal("403");
240 3
        } catch (NotFoundException $e) {
241 2
            $this->handleInternal("404");
242 1
        } catch (InternalErrorException $e) {
243 1
            $this->handleInternal("500");
244
        }
245
    }
246
}
247