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
Pull Request — master (#99)
by Martin
07:44
created

AbstractProvider::isRealResult()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 6.7272
cc 7
eloc 12
nc 7
nop 3
crap 7
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
     *
152
     * @return array null
153
     */
154
    private function getPackages()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
155
    {
156
        if (! file_exists('composer.lock')) {
157
            return;
158
        }
159
160
        $content = file_get_contents('composer.lock');
161
        if ($content === false || $content === '') {
162
            return;
163
        }
164
165
        $content = json_decode($content);
166
167
        return array_merge($content->packages, $content->{'packages-dev'});
168
    }
169
170
    /**
171
     * What kind of capabilities this provider can detect
172
     *
173
     * @return array
174
     */
175 1
    public function getDetectionCapabilities()
176
    {
177 1
        return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities);
178
    }
179
180
    /**
181
     *
182
     * @throws PackageNotLoadedException
183
     */
184 2
    protected function checkIfInstalled()
185
    {
186 2
        if (! Package::isInstalled($this->getPackageName())) {
187 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
188
        }
189 1
    }
190
191
    /**
192
     *
193
     * @param  mixed   $value
194
     * @param  string  $group
195
     * @param  string  $part
196
     * @return boolean
197
     */
198 4
    protected function isRealResult($value, $group = null, $part = null)
199
    {
200 4
        $value = (string) $value;
201 4
        $value = trim($value);
202
203 4
        if ($value === '') {
204 2
            return false;
205
        }
206
207 4
        $regexes = $this->defaultValues['general'];
208
209 4
        if ($group !== null && $part !== null && isset($this->defaultValues[$group][$part])) {
210 2
            $regexes = array_merge($regexes, $this->defaultValues[$group][$part]);
211
        }
212
213 4
        foreach ($regexes as $regex) {
214 2
            if (preg_match($regex, $value) === 1) {
215 2
                return false;
216
            }
217
        }
218
219 4
        return true;
220
    }
221
222 2
    protected function getRealResult($value, $group = null, $part = null)
223
    {
224 2
        if ($this->isRealResult($value, $group, $part) === true) {
225 2
            return $value;
226
        }
227
228 2
        return;
229
    }
230
231
    /**
232
     * Parse the given user agent and return a result if possible
233
     *
234
     * @param string $userAgent
235
     * @param array  $headers
236
     *
237
     * @throws Exception\NoResultFoundException
238
     *
239
     * @return Model\UserAgent
240
     */
241
    abstract public function parse($userAgent, array $headers = []);
242
}
243