Completed
Push — master ( 922528...99faf5 )
by Ben
04:00
created

LocaleUrl::extractLocaleFromParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 3
rs 9.2
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\RouteParser;
7
use Thinktomorrow\Locale\Parsers\UrlParser;
8
9
class LocaleUrl
10
{
11
    /**
12
     * @var Locale
13
     */
14
    private $locale;
15
16
    /**
17
     * @var UrlParser
18
     */
19
    private $urlparser;
20
21
    /**
22
     * @var null|string
23
     */
24
    private $placeholder;
25
26
    /**
27
     * @var RouteParser
28
     */
29
    private $routeparser;
30
31 141
    public function __construct(Locale $locale, UrlParser $urlparser, RouteParser $routeparser, $config = [])
32
    {
33 141
        $this->locale = $locale;
34 141
        $this->urlparser = $urlparser;
35 141
        $this->routeparser = $routeparser;
36
37 141
        $this->placeholder = isset($config['placeholder']) ? $config['placeholder'] : 'locale_slug';
38 141
    }
39
40
    /**
41
     * Generate a localized url
42
     *
43
     * @param $url
44
     * @param null $locale
45
     * @param array $parameters
46
     * @param null $secure
47
     * @return mixed
48
     */
49 33
    public function to($url, $locale = null, $parameters = [], $secure = null)
50
    {
51 33
        return $this->urlparser->set($url)
52 33
                            ->localize($locale)
53 33
                            ->parameters($parameters)
54 33
                            ->secure($secure)
0 ignored issues
show
Documentation introduced by
$secure is of type null, but the function expects a boolean.

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...
55 33
                            ->get();
56
    }
57
58
    /**
59
     * Generate a localized route.
60
     * Note that unlike the Illuminate route() no parameter for 'absolute' path is available
61
     * since urls will always be rendered as absolute ones.
62
     *
63
     * @param $name
64
     * @param null $locale
65
     * @param array $parameters
66
     * @return mixed
67
     */
68 87
    public function route($name, $locale = null, $parameters = [])
69
    {
70
        // Locale should be passed as second parameter but in case it is passed as array
71
        // alongside other parameters, we will try to extract it
72 87
        if(!is_array($locale)) $locale = [$this->placeholder => $locale];
73
74 87
        $parameters = array_merge($locale,(array)$parameters);
75
76 87
        $locale = $this->extractLocaleFromParameters($parameters);
77
78 87
        return $this->routeparser->set($name)
79 87
                            ->localize($locale)
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->extractLocaleFromParameters($parameters) on line 76 can also be of type string; however, Thinktomorrow\Locale\Par...RouteParser::localize() 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...
80 87
                            ->parameters($parameters)
81 87
                            ->get();
82
    }
83
84
    /**
85
     * Isolate locale value from parameters
86
     *
87
     * @param array $parameters
88
     * @return null|string
89
     */
90 87
    private function extractLocaleFromParameters(array &$parameters = [])
91
    {
92 87
        $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...
93
94 87
        if(!array_key_exists($this->placeholder,$parameters))
95 87
        {
96 27
            return $this->locale->get();
97
        }
98
99 69
        $locale = $this->locale->get($parameters[$this->placeholder]);
100
101
        // If locale parameter is not a 'real' parameter, we ignore this value and use the current locale instead
102
        // The 'wrong' parameter will be used without key
103 69
        if($locale != $parameters[$this->placeholder])
104 69
        {
105 24
            $parameters[] = $parameters[$this->placeholder];
106 24
        }
107
108 69
        unset($parameters[$this->placeholder]);
109
110 69
        return $locale;
111
    }
112
}