SetLocale::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace CodeZero\Localizer\Middleware;
4
5
use CodeZero\Localizer\Localizer;
6
use Closure;
7
8
class SetLocale
9
{
10
    /**
11
     * Localizer.
12
     *
13
     * @var \CodeZero\Localizer\Localizer
14
     */
15
    protected $localizer;
16
17
    /**
18
     * Create a new SetLocale instance.
19
     *
20
     * @param \CodeZero\Localizer\Localizer $localizer
21
     */
22 7
    public function __construct(Localizer $localizer)
23
    {
24 7
        $this->localizer = $localizer;
25 7
    }
26
27
    /**
28
     * Handle an incoming request.
29
     *
30
     * @param \Illuminate\Http\Request $request
31
     * @param \Closure $next
32
     *
33
     * @return mixed
34
     */
35 7
    public function handle($request, Closure $next)
36
    {
37 7
        $locale = $this->localizer->detect();
38
39 7
        if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false 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...
40 7
            $this->localizer->store($locale);
41
        }
42
43 7
        return $next($request);
44
    }
45
}
46