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.

AbstractProvider::parse()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use DateTime;
5
use PackageInfo\Exception\PackageNotInstalledException;
6
use PackageInfo\Package;
7
use UserAgentParser\Exception;
8
use UserAgentParser\Exception\PackageNotLoadedException;
9
use UserAgentParser\Model;
10
11
/**
12
 * Abstraction for all providers
13
 *
14
 * @author Martin Keckeis <[email protected]>
15
 * @license MIT
16
 */
17
abstract class AbstractProvider
18
{
19
    /**
20
     * Name of the provider
21
     *
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * Homepage of the provider
28
     *
29
     * @var string
30
     */
31
    protected $homepage;
32
33
    /**
34
     * Composer package name
35
     *
36
     * @var string
37
     */
38
    protected $packageName;
39
40
    /**
41
     * Per default the provider cannot detect anything
42
     * Activate them in $detectionCapabilities
43
     *
44
     * @var array
45
     */
46
    protected $allDetectionCapabilities = [
47
        'browser' => [
48
            'name'    => false,
49
            'version' => false,
50
        ],
51
52
        'renderingEngine' => [
53
            'name'    => false,
54
            'version' => false,
55
        ],
56
57
        'operatingSystem' => [
58
            'name'    => false,
59
            'version' => false,
60
        ],
61
62
        'device' => [
63
            'model'    => false,
64
            'brand'    => false,
65
            'type'     => false,
66
            'isMobile' => false,
67
            'isTouch'  => false,
68
        ],
69
70
        'bot' => [
71
            'isBot' => false,
72
            'name'  => false,
73
            'type'  => false,
74
        ],
75
    ];
76
77
    /**
78
     * Set this in each Provider implementation
79
     *
80
     * @var array
81
     */
82
    protected $detectionCapabilities = [];
83
84
    protected $defaultValues = [
85
        'general' => [],
86
    ];
87
88
    /**
89
     * Return the name of the provider
90
     *
91
     * @return string
92
     */
93 1
    public function getName()
94
    {
95 1
        return $this->name;
96
    }
97
98
    /**
99
     * Get the homepage
100
     *
101
     * @return string
102
     */
103 1
    public function getHomepage()
104
    {
105 1
        return $this->homepage;
106
    }
107
108
    /**
109
     * Get the package name
110
     *
111
     * @return string null
112
     */
113 7
    public function getPackageName()
114
    {
115 7
        return $this->packageName;
116
    }
117
118
    /**
119
     * Return the version of the provider
120
     *
121
     * @return string null
122
     */
123 2
    public function getVersion()
124
    {
125
        try {
126 2
            $package = new Package($this->getPackageName());
127
128 1
            return $package->getVersion();
0 ignored issues
show
Bug introduced by
The method getVersion() does not seem to exist on object<PackageInfo\Package>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129 1
        } catch (PackageNotInstalledException $ex) {
130 1
            return;
131
        }
132
    }
133
134
    /**
135
     * Get the last change date of the provider
136
     *
137
     * @return DateTime null
138
     */
139 2
    public function getUpdateDate()
140
    {
141
        try {
142 2
            $package = new Package($this->getPackageName());
143
144 1
            return $package->getVersionReleaseDate();
0 ignored issues
show
Bug introduced by
The method getVersionReleaseDate() does not seem to exist on object<PackageInfo\Package>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145 1
        } catch (PackageNotInstalledException $ex) {
146 1
            return;
147
        }
148
    }
149
150
    /**
151
     * What kind of capabilities this provider can detect
152
     *
153
     * @return array
154
     */
155 1
    public function getDetectionCapabilities()
156
    {
157 1
        return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities);
158
    }
159
160
    /**
161
     *
162
     * @throws PackageNotLoadedException
163
     */
164 2
    protected function checkIfInstalled()
165
    {
166 2
        if (! Package::isInstalled($this->getPackageName())) {
167 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
168
        }
169 1
    }
170
171
    /**
172
     *
173
     * @param  mixed   $value
174
     * @param  string  $group
175
     * @param  string  $part
176
     * @return boolean
177
     */
178 4
    protected function isRealResult($value, $group = null, $part = null)
179
    {
180 4
        $value = (string) $value;
181 4
        $value = trim($value);
182
183 4
        if ($value === '') {
184 2
            return false;
185
        }
186
187 4
        $regexes = $this->defaultValues['general'];
188
189 4
        if ($group !== null && $part !== null && isset($this->defaultValues[$group][$part])) {
190 2
            $regexes = array_merge($regexes, $this->defaultValues[$group][$part]);
191
        }
192
193 4
        foreach ($regexes as $regex) {
194 2
            if (preg_match($regex, $value) === 1) {
195 2
                return false;
196
            }
197
        }
198
199 4
        return true;
200
    }
201
202 2
    protected function getRealResult($value, $group = null, $part = null)
203
    {
204 2
        if ($this->isRealResult($value, $group, $part) === true) {
205 2
            return $value;
206
        }
207
208 2
        return;
209
    }
210
211
    /**
212
     * Parse the given user agent and return a result if possible
213
     *
214
     * @param string $userAgent
215
     * @param array  $headers
216
     *
217
     * @throws Exception\NoResultFoundException
218
     *
219
     * @return Model\UserAgent
220
     */
221
    abstract public function parse($userAgent, array $headers = []);
222
}
223