Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

HasRouteCollectionTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteCollection() 0 7 2
A getRoute() 0 4 1
A getRoutes() 0 4 1
A connected() 0 4 1
A addRoute() 0 4 1
A hasRoute() 0 4 1
A newRoutesCollection() 0 4 1
1
<?php
2
3
namespace Nip\Router\Router\Traits;
4
5
use Nip\Router\Route\Route;
6
use Nip\Router\RouteCollection;
7
8
/**
9
 * Class HasRouteCollectionTrait
10
 * @package Nip\Router\Router\Traits
11
 */
12
trait HasRouteCollectionTrait
13
{
14
15
    /**
16
     * @inheritdoc
17
     * @return RouteCollection
18
     */
19 9
    public function getRouteCollection()
20
    {
21 9
        if (null === $this->collection) {
22 9
            $this->collection = $this->newRoutesCollection();
0 ignored issues
show
Bug introduced by
The property collection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        }
24 9
        return parent::getRouteCollection();
25
    }
26
27
    /**
28
     * @param $name
29
     * @return null|Route\Route
30
     */
31 2
    public function getRoute($name)
32
    {
33 2
        return $this->getRouteCollection()->get($name);
34
    }
35
36
    /**
37
     * @return RouteCollection|Route[]
38
     */
39 8
    public function getRoutes()
40
    {
41 8
        return $this->getRouteCollection();
42
    }
43
44
    /**
45
     * @param $name
46
     * @return bool
47
     */
48
    public function connected($name)
49
    {
50
        return ($this->getRoute($name) instanceof Route);
51
    }
52
53
    /**
54
     * @param Route $route
55
     */
56 1
    public function addRoute($route)
57
    {
58 1
        $this->getRouteCollection()->addRoute($route);
59 1
    }
60
61
    /**
62
     * @param string $name
63
     * @return bool
64
     */
65
    public function hasRoute($name)
66
    {
67
        return $this->getRoutes()->has($name);
68
    }
69
70
    /**
71
     * @return RouteCollection
72
     */
73 9
    protected function newRoutesCollection()
74
    {
75 9
        return new RouteCollection();
76
    }
77
}
78