Test Setup Failed
Push — master ( 415317...7450ca )
by Gabriel
02:29 queued 10s
created

UrlGenerator::initDefaultRoute()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 26.3215

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 2
dl 0
loc 21
ccs 4
cts 15
cp 0.2667
crap 26.3215
rs 8.6506
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Generator;
4
5
use Nip\Request;
6
use Nip\Router\Generator\Traits\FormattingTrait;
7
use Nip\Router\Generator\Traits\HasPreviousUrlTrait;
8
use Nip\Router\Generator\Traits\UrlFunctionTrait;
9
use Nip\Router\RequestContext;
10
use Nip\Router\Route\Route;
11
12
/**
13
 * Class UrlGenerator
14
 * @package Nip\Router\Generator
15
 * @method RequestContext getContext()
16
 */
17
class UrlGenerator extends \Symfony\Component\Routing\Generator\UrlGenerator
18
{
19
    use FormattingTrait;
20
    use UrlFunctionTrait;
21
    use HasPreviousUrlTrait;
22
23
    /**
24
     * Set the current request instance.
25
     *
26
     * @param  Request $request
27
     */
28
    public function setRequest(Request $request)
29
    {
30
        $context = (new RequestContext())->fromRequest($request);
31
32
        $this->cachedRoot = null;
33
        $this->cachedSchema = null;
34
        return $this->setContext($context);
35
    }
36
37 2
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
38
    {
39 2
        $name = $this->initDefaultRoute($name, $parameters);
40 2
        return parent::generate($name, $parameters, $referenceType);
0 ignored issues
show
Documentation introduced by
$name is of type null|object<Nip\Router\Route\Route>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
    }
42
43
44
    /**
45
     * @param $name
46
     * @param array $params
47
     * @return null|Route
48
     */
49 2
    protected function initDefaultRoute($name, &$params = [])
50
    {
51 2
        $route = $this->routes->get($name);
52 2
        if ($route) {
53 2
            return $name;
54
        }
55
        $parts = explode(".", $name);
56
        $count = count($parts);
57
        if ($count <= 3) {
58
            if (in_array(reset($parts), app('mvc.modules')->getNames())) {
59
                $module = array_shift($parts);
60
                $params['controller'] = isset($parts[0]) ? $parts[0] : null;
61
                $params['action'] = isset($parts[1]) ? $parts[1] : null;
62
                $route = $this->routes->get($module . '.default');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $route is correct as $this->routes->get($module . '.default') (which targets Symfony\Component\Routing\RouteCollection::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
                if ($route) {
64
                    return $route->getName();
65
                }
66
            }
67
        }
68
        return $name;
69
    }
70
}
71