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

OsDetector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 53.45 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 31
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 6 1
A initResultObject() 9 9 2
A setupResultObject() 12 12 3
A detectByFamily() 0 13 4
A detectByDeviceType() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}