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

CollectionsOperationsTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 66
ccs 16
cts 22
cp 0.7272
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A prependRoute() 0 5 1
A get() 0 4 2
A getRoutes() 0 3 1
A setRoutes() 0 4 2
A has() 0 5 2
A addRoute() 0 8 2
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