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

CollectionsOperationsTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 8
cts 16
cp 0.5
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoutes() 0 4 1
A setRoutes() 0 4 1
A has() 0 6 2
A get() 0 5 2
A addRoute() 0 9 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
     * @var Route[]
15
     */
16
    protected $routes = [];
17
18
    /**
19
     * @inheritdoc
20
     * @deprecated Use all()
21
     */
22
    public function getRoutes()
23
    {
24
        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...
25
    }
26
27
    /**
28
     * @param array $routes
29
     */
30
    public function setRoutes($routes)
31
    {
32
        $this->routes = $routes;
33
    }
34
35
    /**
36
     * @param $route
37
     * @return bool
38
     */
39
    public function has($route)
40
    {
41
        $name = $route instanceof Route ? $route->getName() : $route;
42
43
        return $this->get($name) instanceof Route;
44
    }
45
46
    /**
47
     * @param $route
48
     * @return Route|\Symfony\Component\Routing\Route|null
49
     */
50 5
    public function get($route)
51
    {
52 5
        $name = $route instanceof Route ? $route->getName() : $route;
53 5
        return parent::get($name);
54
    }
55
56
57
    /**
58
     * @param Route $route
59
     * @param null $name
60
     * @return
61
     */
62 11
    public function addRoute($route, $name = null)
63
    {
64 11
        if ($name) {
65 8
            $route->setName($name);
66
        } else {
67 3
            $name = $route->getName();
68
        }
69 11
        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...
70
    }
71
}