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 (#95)
by Martin
11:43 queued 32s
created

Endorphin::hasResult()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use EndorphinStudio\Detector;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for piwik/device-detector
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/endorphin-studio/browser-detector
15
 */
16
class Endorphin extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'Endorphin';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/endorphin-studio/browser-detector';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'endorphin-studio/browser-detector';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => true,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => true,
58
            'brand'    => true,
59
            'type'     => true,
60
            'isMobile' => true,
61
            'isTouch'  => true,
62
        ],
63
64
        'bot' => [
65
            'isBot' => true,
66
            'name'  => true,
67
            'type'  => true,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^N\\\\A$/i',
75
        ],
76
    ];
77
78
    /**
79
     *
80
     * @throws PackageNotLoadedException
81
     */
82
    public function __construct()
83
    {
84
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
85
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
86
        }
87
    }
88
89
    /**
90
     *
91
     * @param Detector\DetectorResult $resultRaw
92
     *
93
     * @return bool
94
     */
95
    private function hasResult(Detector\DetectorResult $resultRaw)
96
    {
97
        if ($resultRaw->OS instanceof Detector\OS) {
98
            return true;
99
        }
100
101
        if ($resultRaw->Browser instanceof Detector\Browser) {
102
            return true;
103
        }
104
105
        if ($resultRaw->Device instanceof Detector\Device) {
106
            return true;
107
        }
108
109
        if ($resultRaw->Robot instanceof Detector\Robot) {
0 ignored issues
show
Bug introduced by
The property Robot does not seem to exist in EndorphinStudio\Detector\DetectorResult.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The class EndorphinStudio\Detector\Robot does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
110
            return true;
111
        }
112
113
        return false;
114
    }
115
116
    /**
117
     *
118
     * @param Model\Bot      $bot
119
     * @param Detector\Robot $resultRaw
120
     */
121
    private function hydrateBot(Model\Bot $bot, Detector\Robot $resultRaw)
122
    {
123
        $bot->setIsBot(true);
124
        $bot->setName($this->getRealResult($resultRaw->getName()));
125
    }
126
127
    /**
128
     *
129
     * @param Model\Browser    $browser
130
     * @param Detector\Browser $resultRaw
131
     */
132
    private function hydrateBrowser(Model\Browser $browser, Detector\Browser $resultRaw)
133
    {
134
        $browser->setName($this->getRealResult($resultRaw->getName()));
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<EndorphinStudio\Detector\Browser>.

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...
135
        $browser->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion()));
0 ignored issues
show
Bug introduced by
The method getVersion() does not seem to exist on object<EndorphinStudio\Detector\Browser>.

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...
136
    }
137
138
    /**
139
     *
140
     * @param Model\OperatingSystem $os
141
     * @param Detector\OS           $resultRaw
142
     */
143
    private function hydrateOperatingSystem(Model\OperatingSystem $os, Detector\OS $resultRaw)
144
    {
145
        $os->setName($this->getRealResult($resultRaw->getName()));
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<EndorphinStudio\Detector\OS>.

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...
146
        $os->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion()));
0 ignored issues
show
Bug introduced by
The method getVersion() does not seem to exist on object<EndorphinStudio\Detector\OS>.

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...
147
    }
148
149
    /**
150
     *
151
     * @param Model\Device    $device
152
     * @param Detector\Device $resultRaw
153
     */
154
    private function hydrateDevice(Model\Device $device, Detector\Device $resultRaw)
155
    {
156
        $device->setModel($this->getRealResult($resultRaw->ModelName));
157
        $device->setType($this->getRealResult($resultRaw->getType()));
0 ignored issues
show
Bug introduced by
The method getType() does not seem to exist on object<EndorphinStudio\Detector\Device>.

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...
158
    }
159
160
    public function parse($userAgent, array $headers = [])
161
    {
162
        $resultRaw = \EndorphinStudio\Detector\Detector::analyse($userAgent);
163
164
        /*
165
         * No result found?
166
         */
167
        if ($this->hasResult($resultRaw) !== true) {
168
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
169
        }
170
171
        /*
172
         * Hydrate the model
173
         */
174
        $result = new Model\UserAgent();
175
        $result->setProviderResultRaw($resultRaw);
176
177
        /*
178
         * Bot detection
179
         */
180
        if ($resultRaw->Robot instanceof Detector\Robot) {
0 ignored issues
show
Bug introduced by
The class EndorphinStudio\Detector\Robot does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
181
            $this->hydrateBot($result->getBot(), $resultRaw->Robot);
0 ignored issues
show
Bug introduced by
The property Robot does not seem to exist in EndorphinStudio\Detector\DetectorResult.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
182
183
            return $result;
184
        }
185
186
        /*
187
         * hydrate the result
188
         */
189
        if ($resultRaw->Browser instanceof Detector\Browser) {
190
            $this->hydrateBrowser($result->getBrowser(), $resultRaw->Browser);
191
        }
192
        if ($resultRaw->OS instanceof Detector\OS) {
193
            $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->OS);
194
        }
195
        if ($resultRaw->Device instanceof Detector\Device) {
196
            $this->hydrateDevice($result->getDevice(), $resultRaw->Device);
197
        }
198
199
        return $result;
200
    }
201
}
202