Completed
Pull Request — master (#8)
by ARCANEDEV
08:40 queued 06:05
created

LanguageDetector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 85
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A languages() 0 4 1
A keys() 0 4 1
A handle() 0 11 2
A parse() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Agent\Detectors;
6
7
use Arcanedev\Agent\Contracts\Detector;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class     LanguageDetector
12
 *
13
 * @package  Arcanedev\Agent\Detectors
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class LanguageDetector implements Detector
17
{
18
    /* -----------------------------------------------------------------
19
     |  Properties
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * @var array
25
     */
26
    protected $languages;
27
28
    /* -----------------------------------------------------------------
29
     |  Getters & Setters
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * Get the languages.
35
     *
36
     * @return array
37
     */
38 18
    public function languages(): array
39
    {
40 18
        return $this->languages;
41
    }
42
43
    /**
44
     * Get the languages keys.
45
     *
46
     * @return array
47
     */
48 18
    public function keys(): array
49
    {
50 18
        return array_keys($this->languages());
51
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Main Methods
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Handle the given request.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     *
63
     * @return $this
64
     */
65 372
    public function handle(Request $request): Detector
66
    {
67 372
        $this->languages = [];
68 372
        $acceptLanguage  = $request->server('HTTP_ACCEPT_LANGUAGE');
69
70 372
        if ( ! empty($acceptLanguage)) {
71 372
            $this->parse($acceptLanguage);
0 ignored issues
show
Bug introduced by
It seems like $acceptLanguage defined by $request->server('HTTP_ACCEPT_LANGUAGE') on line 68 can also be of type array; however, Arcanedev\Agent\Detector...nguageDetector::parse() does only seem to accept string, 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...
72
        }
73
74 372
        return $this;
75
    }
76
77
    /* -----------------------------------------------------------------
78
     |  Other Methods
79
     | -----------------------------------------------------------------
80
     */
81
82
    /**
83
     * Parse the accept language.
84
     *
85
     * @param  string  $acceptLanguage
86
     */
87 372
    protected function parse(string $acceptLanguage): void
88
    {
89
        // Parse accept language string.
90 372
        foreach (explode(',', $acceptLanguage) as $piece) {
91 372
            $parts = explode(';', $piece);
92 372
            $language = strtolower($parts[0]);
93 372
            $priority = empty($parts[1]) ? 1. : floatval(str_replace('q=', '', $parts[1]));
94 372
            $this->languages[$language] = $priority;
95
        }
96
97
        // Sort languages by priority.
98 372
        arsort($this->languages);
99 372
    }
100
}
101