Completed
Push — develop ( 157cd5...6a478d )
by Abdelrahman
01:38
created

UrlGenerator::toRoute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
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
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
9
10
class UrlGenerator extends BaseUrlGenerator
11
{
12
    /**
13
     * Generate an absolute URL to the given path.
14
     *
15
     * @param  string  $path
16
     * @param  mixed  $extra
17
     * @param  bool|null  $secure
18
     * @return string
19
     */
20
    public function to($path, $extra = [], $secure = null)
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
        list($path, $query) = $this->extractQueryString($path);
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 $this->routeGenerator;
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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function toRoute($route, $parameters, $absolute)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        // Bind {locale} route parameter
75
        if (config('cortex.foundation.route.locale_prefix') && ! isset($parameters['locale'])) {
76
            $urlLocale = $this->request->segment(1);
77
            $sessionLocale = session('locale', $defaultLocale = app('laravellocalization')->getCurrentLocale());
78
79
            $parameters['locale'] = app('laravellocalization')->checkLocaleInSupportedLocales($urlLocale) ? $urlLocale
80
                : (app('laravellocalization')->checkLocaleInSupportedLocales($sessionLocale) ? $sessionLocale : $defaultLocale);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
81
        }
82
83
        return $this->routeUrl()->to(
84
            $route, $this->formatParameters($parameters), $absolute
85
        );
86
    }
87
}
88