Completed
Branch 4.0 (c710b5)
by Serhii
02:04
created

Detector::getResultObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2018
6
 * @version 4.0.0
7
 * @project browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
use EndorphinStudio\Detector\Data\Result;
13
use EndorphinStudio\Detector\Exception\StorageException;
14
use EndorphinStudio\Detector\Storage\StorageInterface;
15
use Twig\Parser;
16
17
class Detector
18
{
19
    /**
20
     * @var StorageInterface
21
     */
22
    private $dataProvider;
23
24
    /**
25
     * @return StorageInterface
26
     */
27
    public function getDataProvider(): StorageInterface
28
    {
29
        return $this->dataProvider;
30
    }
31
32
    /**
33
     * @return Result
34
     */
35
    public function getResultObject(): Result
36
    {
37
        return $this->resultObject;
38
    }
39
40
    /**
41
     * @var Result
42
     */
43
    private $resultObject;
44
45
    /**
46
     * @param StorageInterface $dataProvider
47
     */
48
    public function setDataProvider(StorageInterface $dataProvider)
49
    {
50
        $this->dataProvider = $dataProvider;
51
    }
52
53
    private $ua;
54
55
    /**
56
     * Detector constructor.
57
     * @param string $dataProvider
58
     * @param string $format
59
     * @throws StorageException
60
     */
61
    public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml')
62
    {
63
        $dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format);
64
        $dataProvider = new $dataProvider();
65
        $dataProvider->setDataDirectory($dataDirectory);
66
        $this->setDataProvider($dataProvider);
67
    }
68
69
    public function analyze(string $ua = 'ua')
70
    {
71
        $this->ua = $ua === 'ua' ? $_SERVER['HTTP_USER_AGENT'] : $ua;
72
        $config = $this->getDataProvider()->getConfig();
73
        $check = ['device', 'os', 'browser', 'robots'];
74
        $this->resultObject = new Result();
75
        foreach ($check as $type) {
76
            $this->detect($config, $type);
0 ignored issues
show
Unused Code introduced by
The call to the method EndorphinStudio\Detector\Detector::detect() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
77
        }
78
        var_dump($this->resultObject);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->resultObject); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
79
    }
80
81
    private function detect($config, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
84
    }
85
86
    private function getPatternList($list, $type)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
87
    {
88
        return array_key_exists($type, $list) ? $list[$type] : [];
89
    }
90
}