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 ( 9617f8...91a95d )
by Martin
07:40 queued 01:09
created

AbstractProvider::getRealResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 6
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use DateTime;
5
use DateTimeZone;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for all providers
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 */
15
abstract class AbstractProvider
16
{
17
    /**
18
     * Name of the provider
19
     *
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * Homepage of the provider
26
     *
27
     * @var string
28
     */
29
    protected $homepage;
30
31
    /**
32
     * Composer package name
33
     *
34
     * @var string
35
     */
36
    protected $packageName;
37
38
    /**
39
     * Version string for caching
40
     *
41
     * @var string
42
     */
43
    private $version;
44
45
    /**
46
     * Last update date
47
     *
48
     * @var DateTime
49
     */
50
    private $updateDate;
51
52
    /**
53
     * Per default the provider cannot detect anything
54
     * Activate them in $detectionCapabilities
55
     *
56
     * @var array
57
     */
58
    protected $allDetectionCapabilities = [
59
        'browser' => [
60
            'name'    => false,
61
            'version' => false,
62
        ],
63
64
        'renderingEngine' => [
65
            'name'    => false,
66
            'version' => false,
67
        ],
68
69
        'operatingSystem' => [
70
            'name'    => false,
71
            'version' => false,
72
        ],
73
74
        'device' => [
75
            'model'    => false,
76
            'brand'    => false,
77
            'type'     => false,
78
            'isMobile' => false,
79
            'isTouch'  => false,
80
        ],
81
82
        'bot' => [
83
            'isBot' => false,
84
            'name'  => false,
85
            'type'  => false,
86
        ],
87
    ];
88
89
    /**
90
     * Set this in each Provider implementation
91
     *
92
     * @var array
93
     */
94
    protected $detectionCapabilities = [];
95
96
    protected $defaultValues = [
97
        'general' => [],
98
    ];
99
100
    /**
101
     * Return the name of the provider
102
     *
103
     * @return string
104
     */
105 1
    public function getName()
106
    {
107 1
        return $this->name;
108
    }
109
110
    /**
111
     * Get the homepage
112
     *
113
     * @return string
114
     */
115 1
    public function getHomepage()
116
    {
117 1
        return $this->homepage;
118
    }
119
120
    /**
121
     * Get the package name
122
     *
123
     * @return string null
124
     */
125 5
    public function getPackageName()
126
    {
127 5
        return $this->packageName;
128
    }
129
130
    /**
131
     * Return the version of the provider
132
     *
133
     * @return string null
134
     */
135 2
    public function getVersion()
136
    {
137 2
        if ($this->version !== null) {
138 1
            return $this->version;
139
        }
140
141 2
        if ($this->getPackageName() === null) {
142 1
            return;
143
        }
144
145 2
        $packages = $this->getPackages();
146
147 2
        if ($packages === null) {
148 1
            return;
149
        }
150
151 2
        foreach ($packages as $package) {
152 2
            if ($package->name === $this->getPackageName()) {
153 1
                $this->version = $package->version;
154
155 2
                break;
156
            }
157
        }
158
159 2
        return $this->version;
160
    }
161
162
    /**
163
     * Get the last change date of the provider
164
     *
165
     * @return DateTime null
166
     */
167 2
    public function getUpdateDate()
168
    {
169 2
        if ($this->updateDate !== null) {
170 1
            return $this->updateDate;
171
        }
172
173 2
        if ($this->getPackageName() === null) {
174 1
            return;
175
        }
176
177 2
        $packages = $this->getPackages();
178
179 2
        if ($packages === null) {
180 1
            return;
181
        }
182
183 2
        foreach ($packages as $package) {
184 2
            if ($package->name === $this->getPackageName()) {
185 1
                $updateDate = new DateTime($package->time, new DateTimeZone('UTC'));
186
187 1
                $this->updateDate = $updateDate;
188
189 2
                break;
190
            }
191
        }
192
193 2
        return $this->updateDate;
194
    }
195
196
    /**
197
     *
198
     * @return array null
199
     */
200 4
    private function getPackages()
201
    {
202 4
        if (! file_exists('composer.lock')) {
203 2
            return;
204
        }
205
206 4
        $content = file_get_contents('composer.lock');
207 4
        if ($content === false || $content === '') {
208
            return;
209
        }
210
211 4
        $content = json_decode($content);
212
213 4
        return array_merge($content->packages, $content->{'packages-dev'});
214
    }
215
216
    /**
217
     * What kind of capabilities this provider can detect
218
     *
219
     * @return array
220
     */
221 1
    public function getDetectionCapabilities()
222
    {
223 1
        return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities);
224
    }
225
226
    /**
227
     *
228
     * @param  mixed   $value
229
     * @param  string  $group
230
     * @param  string  $part
231
     * @return boolean
232
     */
233 2
    protected function isRealResult($value, $group = null, $part = null)
234
    {
235 2
        $value = (string) $value;
236 2
        $value = trim($value);
237
238 2
        if ($value === '') {
239 1
            return false;
240
        }
241
242 2
        $regexes = $this->defaultValues['general'];
243
244 2
        if ($group !== null && $part !== null && isset($this->defaultValues[$group][$part])) {
245 1
            $regexes = array_merge($regexes, $this->defaultValues[$group][$part]);
246
        }
247
248 2
        foreach ($regexes as $regex) {
249 1
            if (preg_match($regex, $value) === 1) {
250 1
                return false;
251
            }
252
        }
253
254 2
        return true;
255
    }
256
257
    protected function getRealResult($value, $group = null, $part = null)
258
    {
259
        if ($this->isRealResult($value, $group, $part) === true) {
260
            return $value;
261
        }
262
263
        return;
264
    }
265
266
    /**
267
     * Parse the given user agent and return a result if possible
268
     *
269
     * @param string $userAgent
270
     * @param array  $headers
271
     *
272
     * @throws Exception\NoResultFoundException
273
     *
274
     * @return Model\UserAgent
275
     */
276
    abstract public function parse($userAgent, array $headers = []);
277
}
278