Passed
Push — master ( 5383f1...af8f48 )
by Gabriel
03:29
created

CollectionsOperationsTrait::addRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        return $this->/** @scrutinizer ignore-call */ all();
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();
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 2
    public function get($route)
58
    {
59 2
        $name = $route instanceof Route ? $route->getName() : $route;
60 2
        return parent::get($name);
61
    }
62
63
64
    /**
65
     * @param Route $route
66
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
67
     * @return
68
     */
69 4
    public function addRoute($route, $name = null)
70
    {
71 4
        if ($name) {
0 ignored issues
show
introduced by
$name is of type null, thus it always evaluated to false.
Loading history...
72
            $route->setName($name);
73
        } else {
74 4
            $name = $route->getName();
75
        }
76 4
        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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        return $this->/** @scrutinizer ignore-call */ add($name, $route);
Loading history...
77
    }
78
}
79