Completed
Push — master ( 3e8ce0...72d2fd )
by Andrew
11:12
created

Router   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 7
c 3
b 2
f 0
lcom 1
cbo 6
dl 0
loc 56
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A match() 0 18 4
1
<?php
2
3
namespace SimpleRoute;
4
5
use FastRoute;
6
use FastRoute\Dispatcher;
7
use FastRoute\RouteCollector;
8
use SimpleRoute\Exception\MethodNotAllowedException;
9
use SimpleRoute\Exception\NotFoundException;
10
11
final class Router implements RouterInterface
12
{
13
    /**
14
     * @var Dispatcher
15
     */
16
    private $dispatcher;
17
18
    /**
19
     * @var RouteInterface[]
20
     */
21
    private $routes;
22
23
    /**
24
     * @param RouteInterface[] A collection of routes
25
     */
26 15
    public function __construct(array $routes)
27
    {
28 15
        $this->routes = array();
29
30 15
        $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $collector) use ($routes) {
31 15
            foreach ($routes as $route) {
32 12
                if (!$route instanceof RouteInterface) {
33 3
                    throw new \InvalidArgumentException('Routes array must contain only RouteInterface objects');
34
                }
35
36 9
                $key = spl_object_hash($route);
37
38 9
                $this->routes[$key] = $route;
39
40 9
                $collector->addRoute($route->getMethods(), $route->getPattern(), $key);
41 12
            }
42 15
        });
43 12
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 12
    public function match($method, $uri)
49
    {
50 12
        $routeInfo = $this->dispatcher->dispatch($method, $uri);
51
52 12
        switch ($routeInfo[0]) {
53 12
            case Dispatcher::FOUND:
54 6
                $result = new Result($this->routes[$routeInfo[1]], $routeInfo[2]);
55 6
                break;
56
57 6
            case Dispatcher::METHOD_NOT_ALLOWED:
58 3
                throw new MethodNotAllowedException($method, $routeInfo[1]);
59
60 3
            case Dispatcher::NOT_FOUND:
61 3
                throw new NotFoundException($uri);
62 6
        }
63
64 6
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
65
    }
66
}
67