Test Setup Failed
Push — master ( ecdc34...0769f8 )
by Gabriel
02:46 queued 11s
created

CollectionsOperationsTrait::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\RouteCollections\Traits;
4
5
use Nip\Router\Route\Route;
6
7
/**
8
 * Trait CollectionsOperationsTrait
9
 * @package Nip\Router\RouteCollections\Traits
10
 */
11
trait CollectionsOperationsTrait
12
{
13
    /**
14
     * @inheritdoc
15
     * @deprecated Use all()
16
     */
17
    public function getRoutes()
18
    {
19
        return $this->all();
0 ignored issues
show
Bug introduced by
It seems like all() 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...
20
    }
21
22
    /**
23
     * @param array $routes
24
     */
25 1
    public function setRoutes($routes)
26
    {
27 1
        foreach ($routes as $route) {
28 1
            $this->addRoute($route);
29
        }
30 1
    }
31
32
    /**
33
     * @param Route $route
34
     */
35 1
    public function prependRoute($route)
36
    {
37 1
        $routes = $this->all();
0 ignored issues
show
Bug introduced by
It seems like all() 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...
38 1
        $this->addRoute($route);
39 1
        $this->setRoutes($routes);
40 1
    }
41
42
    /**
43
     * @param $route
44
     * @return bool
45
     */
46
    public function has($route)
47
    {
48
        $name = $route instanceof Route ? $route->getName() : $route;
49
50
        return $this->get($name) instanceof Route;
51
    }
52
53
    /**
54
     * @param $route
55
     * @return Route|\Symfony\Component\Routing\Route|null
56
     */
57 26
    public function get($route)
58
    {
59 26
        $name = $route instanceof Route ? $route->getName() : $route;
60 26
        return parent::get($name);
61
    }
62
63
64
    /**
65
     * @param Route $route
66
     * @param null $name
67
     * @return
68
     */
69 29
    public function addRoute($route, $name = null)
70
    {
71 29
        if ($name) {
72 25
            $route->setName($name);
73
        } else {
74 4
            $name = $route->getName();
75
        }
76 29
        return $this->add($name, $route);
0 ignored issues
show
Bug introduced by
It seems like add() 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...
77
    }
78
}
79