GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fee33a...ddc175 )
by Jan-Petter
02:52
created

UserAgentParser::stripVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace vipnytt;
3
4
/**
5
 * Class UserAgentParser
6
 *
7
 * @package vipnytt
8
 */
9
class UserAgentParser
10
{
11
    private $userAgent;
12
    private $groups = [];
13
14
    /**
15
     * Constructor
16
     *
17
     * @param string $userAgent
18
     */
19
    public function __construct($userAgent)
20
    {
21
        $this->userAgent = mb_strtolower(trim($userAgent));
22
        $this->explode();
23
    }
24
25
    /**
26
     * Parses all possible User-Agent groups to an array
27
     *
28
     * @return array
29
     */
30
    private function explode()
31
    {
32
        $this->groups = [$this->userAgent];
33
        $this->groups[] = $this->stripVersion();
34
        while (strpos(end($this->groups), '-') !== false) {
35
            $current = end($this->groups);
36
            $this->groups[] = substr($current, 0, strrpos($current, '-'));
37
        }
38
        $this->groups = array_unique($this->groups);
39
    }
40
41
    /**
42
     * Strip version number
43
     *
44
     * @return string
45
     */
46
    public function stripVersion()
47
    {
48
        if (strpos($this->userAgent, '/') !== false) {
49
            return explode('/', $this->userAgent, 2)[0];
50
        }
51
        return $this->userAgent;
52
    }
53
54
    /**
55
     * Find matching User-Agent
56
     * Selects the best matching from an array, or $fallback if none matches
57
     *
58
     * @param array $array
59
     * @param string|null $fallback
60
     * @return string|false
61
     */
62
    public function match($array, $fallback = null)
63
    {
64
        foreach ($this->groups as $userAgent) {
65
            if (in_array($userAgent, array_map('strtolower', $array))) {
66
                return $userAgent;
67
            }
68
        }
69
        return isset($fallback) ? $fallback : false;
70
    }
71
72
    /**
73
     * Export all User-Agents as an array
74
     *
75
     * @return array
76
     */
77
    public function export()
78
    {
79
        return $this->groups;
80
    }
81
}
82