Test Setup Failed
Push — master ( 0c8b57...df0330 )
by Gabriel
08:06
created

HasRouteCollectionTrait::initRouteCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 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 21
    public function getRouteCollection()
20
    {
21 21
//        if (null === $this->collection) {
22 21
//            $this->collection = $this->newRoutesCollection();
23
//        }
24 21
        return parent::getRouteCollection();
25
    }
26
27
    /**
28
     * @param $name
29
     * @return null|Route\Route
30
     */
31 13
    public function getRoute($name)
32
    {
33 13
        return $this->getRouteCollection()->get($name);
34
    }
35
36
    /**
37
     * @return RouteCollection|Route[]
38
     */
39 16
    public function getRoutes()
40
    {
41 16
        return $this->getRouteCollection();
42
    }
43
44
    /**
45
     * @param Route $route
46
     */
47
    public function addRoute($route)
48
    {
49
        $this->getRouteCollection()->addRoute($route);
50
    }
51
52
    /**
53
     * @param $name
54
     * @return bool
55
     */
56 1
    public function connected($name)
57
    {
58 1
        return $this->hasRoute($name);
59 1
    }
60
61
    /**
62
     * @param string $name
63
     * @return bool
64
     */
65
    public function hasRoute($name)
66
    {
67
        return $this->getGenerator()->hasRoute($name);
0 ignored issues
show
Bug introduced by
It seems like getGenerator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
    }
69
70
    public function initRouteCollection()
71
    {
72
        $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...
73 21
    }
74
75 21
    /**
76
     * @return RouteCollection
77
     */
78
    protected function newRoutesCollection()
79
    {
80
        return new RouteCollection();
81
    }
82
}
83