Completed
Branch master (b52e58)
by Pierre
03:02 queued 37s
created

Routes::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Http;
4
5
use App\Http\Interfaces\IRoutes;
6
7
class Routes implements IRoutes
8
{
9
    /**
10
     * route list as array
11
     *
12
     * @var array
13
     */
14
    private $routes = [];
15
16
    /**
17
     * __construct
18
     *
19
     * @param array $routes
20
     * @return Routes
21
     */
22
    public function __construct(array $routes = [])
23
    {
24
        if (!empty($routes)) {
25
            $this->set($routes);
26
        }
27
        return $this;
28
    }
29
30
    /**
31
     * returns routes as array
32
     *
33
     * @return array
34
     */
35
    public function get(): array
36
    {
37
        return $this->routes;
38
    }
39
40
    /**
41
     * set routes as array and returns Routes
42
     *
43
     * @param array $routes
44
     * @return Routes
45
     */
46
    public function set(array $routes): Routes
47
    {
48
        $this->routes = $routes;
49
        $this->validate();
50
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Http\Routes which is incompatible with the return type mandated by App\Http\Interfaces\IRoutes::set() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
51
    }
52
53
    /**
54
     * validate routes to be an array of regexp string
55
     *
56
     * @throws Exception
57
     * @return void
58
     */
59
    protected function validate()
60
    {
61
        $count = count($this->routes);
62
        for ($c = 0; $c < $count; $c++) {
63
            $route = $this->routes[$c];
64
            if (@preg_match($route, null) === false) {
65
                throw new \Exception('Route invalid ' . $route);
66
            }
67
        }
68
    }
69
}
70