Completed
Branch master (df0330)
by Gabriel
08:50 queued 06:43
created

HasDefaultRouteTrait::initDefaultRoute()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 2
dl 0
loc 21
rs 8.6506
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Generator\Traits;
4
5
use Nip\Router\Route\Route;
6
7
/**
8
 * Trait HasDefaultRouteTrait
9
 * @package Nip\Router\Generator\Traits
10
 */
11
trait HasDefaultRouteTrait
12
{
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
18
    {
19
        $name = $this->initDefaultRoute($name, $parameters);
20
        return parent::generate($name, $parameters, $referenceType);
21
    }
22
23
    /**
24
     * @param $name
25
     * @param array $params
26
     * @return null|Route
27
     */
28
    protected function initDefaultRoute($name, &$params = [])
29
    {
30
        $route = $this->hasRoute($name);
0 ignored issues
show
Bug introduced by
It seems like hasRoute() 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...
31
        if ($route) {
32
            return $name;
33
        }
34
        $parts = explode(".", $name);
35
        $count = count($parts);
36
        if ($count <= 3) {
37
            if (in_array(reset($parts), app('mvc.modules')->getNames())) {
38
                $module = array_shift($parts);
39
                $params['controller'] = isset($parts[0]) ? $parts[0] : null;
40
                $params['action'] = isset($parts[1]) ? $parts[1] : null;
41
                $defaultRoute = $module . '.default';
42
                if ($this->hasRoute($defaultRoute)) {
0 ignored issues
show
Bug introduced by
It seems like hasRoute() 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...
43
                    return $defaultRoute;
44
                }
45
            }
46
        }
47
        return $name;
48
    }
49
}
50