Completed
Push — 4.0 ( 604d5a...33061c )
by Serhii
02:27
created

OsDetector::detect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace EndorphinStudio\Detector\Detection;
5
6
use EndorphinStudio\Detector\Tools;
7
8
class OsDetector extends AbstractDetection
9
{
10
    public function detect(string $ua)
11
    {
12
        $this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), 'os');
13
        $this->initResultObject();
14
        $this->setupResultObject();
15
    }
16
17 View Code Duplication
    private function initResultObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19
        $result = $this->detector->getResultObject()->getOs();
20
21
        // init default value from data
22
        foreach ($this->config['default'] as $defaultKey => $defaultValue) {
23
            Tools::runSetter($result, $defaultKey, $defaultValue);
24
        }
25
    }
26
27 View Code Duplication
    private function setupResultObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $result = $this->detector->getResultObject()->getOs();
30
        $osData = $this->detectByFamily();
31
        foreach ($osData as $key => $value) {
32
            if ($key === 'originalInfo') {
33
                $this->setAttributes($value);
34
                continue;
35
            }
36
            Tools::runSetter($result, $key, $value);
37
        }
38
    }
39
40
    private function detectByFamily(): array
41
    {
42
        foreach ($this->config as $family => $data) {
43
            if ($family === 'default') {
44
                continue;
45
            }
46
            $os = $this->detectByDeviceType($family);
47
            if ($os) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $os of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
                return array_merge($os, ['family' => $family]);
49
            }
50
        }
51
        return [];
52
    }
53
54 View Code Duplication
    private function detectByDeviceType($family): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        foreach ($this->config[$family] as $deviceType => $patternList) {
57
            $os = $this->detectByPattern($patternList);
58
            if ($os) {
59
                return array_merge($os, ['type' => $deviceType]);
60
            }
61
        }
62
        return [];
63
    }
64
65
}