Test Failed
Pull Request — master (#122)
by
unknown
30:03 queued 23:05
created

Scope::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Thinktomorrow\Locale\Exceptions\InvalidScope;
6
use Thinktomorrow\Locale\Values\Locale;
7
use Thinktomorrow\Locale\Values\Root;
8
9
class Scope
10
{
11
    /**
12
     * @var array
13
     */
14
    private $locales;
15
16
    /**
17
     * The active locale.
18
     *
19
     * @var Locale
20
     */
21
    private static $activeLocale;
22
23
    /**
24
     * The default locale. In the request path
25
     * it's the hidden segment, e.g. /.
26
     *
27
     * @var Locale
28
     */
29
    private $default;
30
31
    /**
32
     * When the canonical scope has a root set to be
33
     * other than the current, that specific root is defined here
34
     * By default the current request root is of use (NULL).
35
     *
36
     * @var null|Root
37
     */
38
    private $customRoot = null;
39
40 10
    public function __construct(array $locales)
41
    {
42 10
        if (!isset($locales['/'])) {
43
            throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.');
44
        }
45 10
        $this->locales = $locales;
46 10
        $this->default = Locale::from($this->locales['/']);
47 10
    }
48
49
    public function setCustomRoot(Root $customRoot)
50
    {
51
        $this->customRoot = $customRoot;
52
53
        return $this;
54
    }
55
56
    public function customRoot(): ?Root
57
    {
58
        return $this->customRoot;
59
    }
60
61
    /**
62
     * Get the locale by segment identifier.
63
     *
64
     * @param $segment
65
     *
66
     * @return null|Locale
67
     */
68
    public function findLocale($segment): ?Locale
69
    {
70
        return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null;
71
    }
72
73
    public function locales(): array
74
    {
75
        return $this->locales;
76
    }
77
78
    public function defaultLocale(): Locale
79
    {
80
        return $this->default;
81
    }
82
83
    public static function activeLocale(): ?Locale
84
    {
85
        return static::$activeLocale;
0 ignored issues
show
Bug introduced by
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
86
    }
87
88
    public static function setActiveLocale(Locale $locale)
89
    {
90
        static::$activeLocale = $locale;
0 ignored issues
show
Bug introduced by
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
91
    }
92
93
    /**
94
     * Get the url segment which corresponds with the passed locale.
95
     *
96
     * @param null $locale
97
     *
98
     * @return null|string
99
     */
100
    public function segment($locale = null): ?string
101
    {
102
        if (is_null($locale)) {
103
            return $this->activeSegment();
104
        }
105
106
        return ($key = array_search($locale, $this->locales)) ? $key : null;
107
    }
108
109
    public function activeSegment(): ?string
110
    {
111
        return $this->segment($this->activeLocale());
112
    }
113
114
    public function validateLocale(string $locale = null): bool
115
    {
116
        if (!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117
            return false;
118
        }
119
120
        return in_array($locale, $this->locales);
121
    }
122
123
    public function validateSegment(string $segment = null): bool
124
    {
125
        if (!$segment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segment of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126
            return false;
127
        }
128
129
        return isset($this->locales[$segment]);
130
    }
131
}
132