Completed
Push — 4.0 ( c710b5...5960ab )
by Serhii
02:22
created

OsDetector::detectByFamily()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
4
namespace EndorphinStudio\Detector\Detection;
5
6
7
use EndorphinStudio\Detector\Data\AbstractData;
8
use EndorphinStudio\Detector\Data\Os;
9
use EndorphinStudio\Detector\Tools;
10
11
class OsDetector extends AbstractDetection
12
{
13
    public function detect(string $ua)
14
    {
15
        $this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), 'os');
16
        $result = $this->detector->getResultObject()->getOs();
17
18
        // init default value from data
19
        foreach ($this->config['default'] as $defaultKey => $defaultValue) {
20
            $methodName = sprintf('set%s', ucfirst($defaultKey));
21
            if (!method_exists($result, $methodName)) {
22
                continue;
23
            }
24
            $result->$methodName($defaultValue);
25
        }
26
        $this->detectByFamily();
27
    }
28
29
    private function detectByFamily(): array
30
    {
31
        $result = [];
32
        foreach ($this->config as $family => $data) {
33
            if($family === 'default') {
34
                continue;
35
            }
36
            $this->detectByDeviceType($family);
37
        }
38
        return $result;
39
    }
40
41
    private function detectByDeviceType($family): array
42
    {
43
        $result = [];
44
        foreach ($this->config[$family] as $deviceType => $patternList) {
45
            $this->detectByPattern($patternList);
46
        }
47
        return $result;
48
    }
49
50
    private function detectByPattern(array $deviceList)
51
    {
52
        $result = null;
53
        foreach ($deviceList as $patternId => $patternData) {
54
            $useDefault = false;
55
            $pattern = '/%s/';
56
            $version = '%s';
57
            if(array_key_exists('default', $patternData) && $patternData['default'] === true) {
58
                $useDefault = true;
59
                $pattern = sprintf($pattern, $patternId);
60
                $version = Tools::getVersionPattern(sprintf($version, $patternId));
61
            }
62
63
            if(!$useDefault) {
64
                $pattern = sprintf($pattern, $patternData['pattern']);
65
                $version = Tools::getVersionPattern(sprintf($version, $patternData['version']));
0 ignored issues
show
Unused Code introduced by
$version is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
            }
67
68
            if(preg_match($pattern, $this->detector->getUserAgent())) {
69
                echo 'BINGO';
70
            }
71
        }
72
        return $result;
73
    }
74
75
}