HasDefaultRouteTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 50
ccs 0
cts 22
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initMvcRoute() 0 19 6
A generate() 0 4 1
A initDefaultRoute() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Nip\Router\Generator\Traits;
5
6
/**
7
 * Trait HasDefaultRouteTrait
8
 * @package Nip\Router\Generator\Traits
9
 */
10
trait HasDefaultRouteTrait
11
{
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
0 ignored issues
show
Bug introduced by
The constant Nip\Router\Generator\Tra...uteTrait::ABSOLUTE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
    {
18
        $name = $this->initDefaultRoute($name, $parameters);
19
        return parent::generate($name, $parameters, $referenceType);
20
    }
21
22
    /**
23
     * @param $name
24
     * @param array $params
25
     * @return string
26
     */
27
    protected function initDefaultRoute($name, &$params = [])
28
    {
29
        $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? ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $route = $this->hasRoute($name);
Loading history...
30
        if ($route) {
31
            return $name;
32
        }
33
        return $this->initMvcRoute($name, $params);
34
    }
35
36
    /**
37
     * @param $name
38
     * @param array $params
39
     * @return string
40
     */
41
    protected function initMvcRoute($name, &$params = [])
42
    {
43
        $parts = explode(".", $name);
44
        $count = count($parts);
45
        if ($count > 3) {
46
            return $name;
47
        }
48
        $firstPart = strtolower(reset($parts));
49
        if (!in_array($firstPart, app('mvc.modules')->getNames())) {
50
            return $name;
51
        }
52
        $module = array_shift($parts);
53
        $params['controller'] = isset($parts[0]) ? $parts[0] : null;
54
        $params['action'] = isset($parts[1]) ? $parts[1] : null;
55
        $defaultRoute = $module . '.default';
56
        if ($this->hasRoute($defaultRoute)) {
57
            return $defaultRoute;
58
        }
59
        return $name;
60
    }
61
}
62