UrlGenerator::toRoute()   B
last analyzed

Complexity

Conditions 9
Paths 15

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.0555
cc 9
nc 15
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Overrides\Illuminate\Routing;
6
7
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
8
9
class UrlGenerator extends BaseUrlGenerator
10
{
11
    /**
12
     * Generate an absolute URL to the given path.
13
     *
14
     * @param string    $path
15
     * @param mixed     $extra
16
     * @param bool|null $secure
17
     *
18
     * @return string
19
     */
20
    public function to($path, $extra = [], $secure = null): string
21
    {
22
        if (! config('cortex.foundation.route.trailing_slash')) {
23
            return parent::to($path, $extra, $secure);
24
        }
25
26
        // First we will check if the URL is already a valid URL. If it is we will not
27
        // try to generate a new one but will simply return the URL as is, which is
28
        // convenient since developers do not always have to check if it's valid.
29
        if ($this->isValidUrl($path)) {
30
            return $path;
31
        }
32
33
        $tail = implode('/', array_map(
34
                'rawurlencode', (array) $this->formatParameters($extra))
35
        );
36
37
        // Once we have the scheme we will compile the "tail" by collapsing the values
38
        // into a single string delimited by slashes. This just makes it convenient
39
        // for passing the array of parameters to this URL as a list of segments.
40
        $root = $this->formatRoot($this->formatScheme($secure));
41
42
        [$path, $query] = $this->extractQueryString($path);
0 ignored issues
show
Bug introduced by
The variable $query does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
44
        return $this->format(
45
                $root, '/'.trim($path.'/'.$tail, '/')
46
            ).'/'.$query;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function routeUrl()
53
    {
54
        if (config('cortex.foundation.route.trailing_slash') && ! $this->routeGenerator) {
55
            $this->routeGenerator = new RouteUrlGenerator($this, $this->request);
56
        }
57
58
        return parent::routeUrl();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function previous($fallback = false)
65
    {
66
        return ($previousUrl = $this->request->input('previous_url')) ? $this->to($previousUrl) : parent::previous($fallback);
0 ignored issues
show
Bug introduced by
It seems like $previousUrl defined by $this->request->input('previous_url') on line 66 can also be of type array; however, Cortex\Foundation\Overri...ting\UrlGenerator::to() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function toRoute($route, $parameters, $absolute)
73
    {
74
        // Bind {locale} route parameter
75
        if (config('cortex.foundation.route.locale_prefix') && in_array('locale', $route->parameterNames()) && ! isset($parameters['locale'])) {
76
            $urlLocale = $this->request->segment(1);
77
            $sessionLocale = session('locale', $defaultLocale = app('laravellocalization')->getCurrentLocale());
78
            $parameters['locale'] = app('laravellocalization')->checkLocaleInSupportedLocales($urlLocale) ? $urlLocale
79
                : (app('laravellocalization')->checkLocaleInSupportedLocales($sessionLocale) ? $sessionLocale : $defaultLocale);
80
        }
81
82
        // Bind {subdomain} route parameter
83
        if (in_array('subdomain', $route->parameterNames()) && ! isset($parameters['subdomain'])) {
84
            $parameters['subdomain'] = $route->hasParameter('subdomain') ? $route->parameter('subdomain') : explode('.', $this->request->getHost())[0];
85
        }
86
87
        return $this->routeUrl()->to(
88
            $route, $this->formatParameters($parameters), $absolute
89
        );
90
    }
91
}
92