Completed
Push — master ( 243903...9a7d92 )
by ARCANEDEV
9s
created

src/Parsers/UserAgentParser.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Arcanedev\LaravelTracker\Parsers;
2
3
use Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser as UserAgentParserContract;
4
5
/**
6
 * Class     UserAgentParser
7
 *
8
 * @package  Arcanedev\LaravelTracker\Parsers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class UserAgentParser implements UserAgentParserContract
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /** @var  \UAParser\Parser */
18
    protected $parser;
19
20
    /** @var string */
21
    public $basePath;
22
23
    /* ------------------------------------------------------------------------------------------------
24
     |  Constructor
25
     | ------------------------------------------------------------------------------------------------
26
     */
27
    /**
28
     * UserAgentParser constructor.
29
     *
30
     * @param  \UAParser\Parser  $parser
31
     * @param  string            $basePath
32
     * @param  null              $userAgent
33
     */
34 30
    public function __construct($parser, $basePath, $userAgent = null)
0 ignored issues
show
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
    {
36 30
        if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) {
37
            $userAgent = $_SERVER['HTTP_USER_AGENT'];
38
        }
39
40 30
        $this->parser   = $parser->parse($userAgent);
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->parse($userAgent) of type object<UAParser\Result\Client> is incompatible with the declared type object<UAParser\Parser> of property $parser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 30
        $this->basePath = $basePath;
42 30
    }
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Main Functions
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    /**
49
     * Get the OS version.
50
     *
51
     * @return string
52
     */
53 18
    public function getOperatingSystemVersion()
54
    {
55 18
        $os = $this->getOperatingSystem();
56
57 18
        return $this->prepareVersion([$os->major, $os->minor, $os->patch]);
58
    }
59
60
    /**
61
     * Get the OS Family.
62
     *
63
     * @return string|null
64
     */
65 18
    public function getOperatingSystemFamily()
66
    {
67
        try {
68 18
            return $this->parser->os->family;
0 ignored issues
show
The property os does not seem to exist in UAParser\Parser.

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...
69
        }
70
        catch (\Exception $e) {
0 ignored issues
show
catch (\Exception $e) { return null; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
71
            return null;
72
        }
73
    }
74
75
    /**
76
     * Get the browser.
77
     *
78
     * @return string
79
     */
80 18
    public function getBrowser()
81
    {
82 18
        return $this->getUserAgent()->family;
83
    }
84
85
    /**
86
     * Get the user agent version.
87
     *
88
     * @return string
89
     */
90 18
    public function getUserAgentVersion()
91
    {
92 18
        $ua = $this->getUserAgent();
93
94 18
        return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]);
95
    }
96
97
    /**
98
     * Get the original user agent.
99
     *
100
     * @return string
101
     */
102 18
    public function getOriginalUserAgent()
103
    {
104 18
        return $this->parser->originalUserAgent;
0 ignored issues
show
The property originalUserAgent does not seem to exist in UAParser\Parser.

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...
105
    }
106
107
    /**
108
     * Get the user agent.
109
     *
110
     * @return \UAParser\Result\UserAgent
111
     */
112 18
    public function getUserAgent()
113
    {
114 18
        return $this->parser->ua;
0 ignored issues
show
The property ua does not seem to exist in UAParser\Parser.

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...
115
    }
116
117
    /**
118
     * Get the operating system.
119
     *
120
     * @return \UAParser\Result\OperatingSystem
121
     */
122 18
    public function getOperatingSystem()
123
    {
124 18
        return $this->parser->os;
0 ignored issues
show
The property os does not seem to exist in UAParser\Parser.

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...
125
    }
126
127
    /**
128
     * Get the device.
129
     *
130
     * @return \UAParser\Result\Device
131
     */
132
    public function getDevice()
133
    {
134
        return $this->parser->device;
0 ignored issues
show
The property device does not seem to exist. Did you mean deviceParser?

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...
135
    }
136
137
    /* ------------------------------------------------------------------------------------------------
138
     |  Other Functions
139
     | ------------------------------------------------------------------------------------------------
140
     */
141
    /**
142
     * Prepare the version.
143
     *
144
     * @param  array  $version
145
     *
146
     * @return string
147
     */
148 24
    protected function prepareVersion(array $version)
149
    {
150 24
        return implode('.', array_filter($version));
151
    }
152
}
153