Completed
Push — master ( 53915c...5989b6 )
by Ben
06:15
created

LocaleUrl::extractLocaleFromParameters()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.1374

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 32
ccs 14
cts 17
cp 0.8235
crap 5.1374
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Thinktomorrow\Locale\Parsers\UrlParser;
7
8
class LocaleUrl
9
{
10
    /**
11
     * @var Locale
12
     */
13
    private $locale;
14
15
    /**
16
     * @var UrlParser
17
     */
18
    private $parser;
19
20
    /**
21
     * @var Illuminate\Contracts\Routing\UrlGenerator
22
     */
23
    private $generator;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $placeholder;
29
30 102
    public function __construct(Locale $locale, UrlParser $parser, UrlGenerator $generator, $config = [])
31
    {
32 102
        $this->locale = $locale;
33 102
        $this->parser = $parser;
34 102
        $this->generator = $generator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $generator of type object<Illuminate\Contracts\Routing\UrlGenerator> is incompatible with the declared type object<Thinktomorrow\Loc...s\Routing\UrlGenerator> of property $generator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36 102
        $this->placeholder = isset($config['placeholder']) ? $config['placeholder'] : 'locale_slug';
37 102
    }
38
39
    /**
40
     * Generate a localized url
41
     *
42
     * @param $url
43
     * @param null $locale
44
     * @param array $extra
45
     * @param null $secure
46
     * @return mixed
47
     */
48 99
    public function to($url, $locale = null, $extra = [], $secure = null)
49
    {
50 99
        $url = $this->parser->set($url)
51 99
                            ->localize($locale)
52 99
                            ->get();
53
54 99
        return $this->resolveUrl($url, $extra, $secure);
55
    }
56
57
    /**
58
     * Generate a localized route
59
     *
60
     * @param $name
61
     * @param null $locale
62
     * @param array $parameters
63
     * @param bool $absolute
64
     * @return mixed
65
     */
66 69
    public function route($name, $locale = null, $parameters = [], $absolute = true)
67
    {
68
        // Locale should be passed as second parameter but in case it is passed as array
69
        // alongside other parameters, we will try to extract it
70 69
        if(!is_array($locale)) $locale = [$this->placeholder => $locale];
71
72 69
        $parameters = array_merge($locale,$parameters);
73
74 69
        $locale = $this->extractLocaleFromParameters($parameters);
75
76 69
        $url = $this->resolveRoute($name,$parameters,$absolute);
77
78 69
        return $this->to($url,$locale);
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->extractLocaleFromParameters($parameters) on line 74 can also be of type string; however, Thinktomorrow\Locale\LocaleUrl::to() does only seem to accept null, 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...
79
    }
80
81
    /**
82
     * Isolate locale value from parameters
83
     *
84
     * @param array $parameters
85
     * @return null|string
86
     */
87 69
    private function extractLocaleFromParameters(&$parameters = [])
88
    {
89 69
        $locale = null;
0 ignored issues
show
Unused Code introduced by
$locale is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
91 69
        if(!is_array($parameters))
92 23
        {
93
            $locale = $this->locale->get($parameters);
94
95
            // If locale is the only parameter, we make sure the 'real' parameters is flushed
96
            if($locale == $parameters) $parameters = [];
97
98
            return $locale;
99
        }
100
101 69
        if(!array_key_exists($this->placeholder,$parameters))
102 23
        {
103 27
            return $this->locale->get();
104
        }
105
106 51
        $locale = $this->locale->get($parameters[$this->placeholder]);
107
108
        // If locale parameter is not a 'real' parameters, we ignore the passed locale and use the active one
109
        // The 'wrong' parameter will be used without key
110 51
        if($locale != $parameters[$this->placeholder])
111 17
        {
112 24
            $parameters[] = $parameters[$this->placeholder];
113 8
        }
114
115 51
        unset($parameters[$this->placeholder]);
116
117 51
        return $locale;
118
    }
119
120
    /**
121
     * Generate url via illuminate
122
     *
123
     * @param $url
124
     * @param array $extra
125
     * @param null $secure
126
     * @return string
127
     */
128 99
    private function resolveUrl($url, $extra = [], $secure = null)
129
    {
130 99
        return $this->generator->to($url, $extra, $secure);
131
    }
132
133
    /**
134
     * Generate route via illuminate
135
     *
136
     * @param $name
137
     * @param array $parameters
138
     * @param bool $absolute
139
     * @return string
140
     */
141 69
    private function resolveRoute($name, $parameters = [], $absolute = true)
142
    {
143 69
        return $this->generator->route($name, $parameters, $absolute);
144
    }
145
}