Passed
Pull Request — master (#36)
by Philippe
02:37
created

Scope::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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\Exceptions\NotDetectedException;
7
use Thinktomorrow\Locale\Values\Locale;
8
use Thinktomorrow\Locale\Values\Root;
9
10
class Scope
11
{
12
    /**
13
     * @var array
14
     */
15
    private $locales;
16
17
    /**
18
     * The active locale.
19
     *
20
     * @var Locale
21
     */
22
    private static $activeLocale;
23
24
    /**
25
     * The default locale. In the request path
26
     * it's the hidden segment, e.g. /.
27
     *
28
     * @var Locale
29
     */
30
    private $default;
31
32
    /**
33
     * When the canonical scope has a root set to be
34
     * other than the current, that specific root is defined here
35
     * By default the current request root is of use (NULL).
36
     *
37
     * @var null|Root
38
     */
39
    private $customRoot = null;
40
41 83
    public function __construct(array $locales)
42
    {
43 83
        if (!isset($locales['/'])) {
44 1
            throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.');
45
        }
46 82
        $this->locales = $locales;
47 82
        $this->default = Locale::from($this->locales['/']);
48 82
    }
49
50 12
    public function setCustomRoot(Root $customRoot)
51
    {
52 12
        $this->customRoot = $customRoot;
53
54 12
        return $this;
55
    }
56
57 10
    public function customRoot(): ?Root
58
    {
59 10
        return $this->customRoot;
60
    }
61
62
    /**
63
     * Get the locale by segment identifier.
64
     *
65
     * @param $segment
66
     *
67
     * @return null|Locale
68
     */
69 68
    public function findLocale($segment): ?Locale
70
    {
71 68
        return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null;
72
    }
73
74 32
    public function locales(): array
75
    {
76 32
        return $this->locales;
77
    }
78
79 61
    public function defaultLocale(): Locale
80
    {
81 61
        return $this->default;
82
    }
83
84 19
    public static function activeLocale(): ?Locale
85
    {
86 19
        if(is_null(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...
87 1
            throw new NotDetectedException('Make sure you detect the locale by using the routeprefix helper.');
88
        } 
89
90 18
        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...
91
    }
92
93 68
    public static function setActiveLocale(Locale $locale)
94
    {
95 68
        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...
96 68
    }
97
98
    /**
99
     * Get the url segment which corresponds with the passed locale.
100
     *
101
     * @param null $locale
102
     *
103
     * @return null|string
104
     */
105 35
    public function segment($locale = null): ?string
106
    {
107 35
        if (is_null($locale)) {
108 2
            return $this->activeSegment();
109
        }
110
111 35
        return ($key = array_search($locale, $this->locales)) ? $key : null;
112
    }
113
114 18
    public function activeSegment(): ?string
115
    {
116 18
        return $this->segment($this->activeLocale());
117
    }
118
119 27
    public function validateLocale(string $locale = null): bool
120
    {
121 27
        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...
122 5
            return false;
123
        }
124
125 25
        return in_array($locale, $this->locales);
126
    }
127
128 24
    public function validateSegment(string $segment = null): bool
129
    {
130 24
        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...
131 5
            return false;
132
        }
133
134 22
        return isset($this->locales[$segment]);
135
    }
136
137 1
    public function refresh()
138
    {
139 1
        static::$activeLocale = null;
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...
140 1
    }
141
}
142