Completed
Push — 4.0 ( c6bc6b...71d650 )
by Serhii
02:10
created

AbstractDetection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 70
Duplicated Lines 18.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 13
loc 70
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A initResultObject() 0 7 2
detect() 0 1 ?
A detectByPattern() 0 11 3
A getPattern() 0 7 4
A setAttributes() 0 9 3
A detectByType() 13 13 4

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
7
use EndorphinStudio\Detector\Data\AbstractData;
8
use EndorphinStudio\Detector\Detector;
9
use EndorphinStudio\Detector\Tools;
10
11
abstract class AbstractDetection implements DetectionInterface
12
{
13
    /** @var array */
14
    protected $config;
15
    /** @var Detector */
16
    protected $detector;
17
18
    /** @var AbstractData */
19
    protected $resultObject;
20
21
    public function init(Detector $detector)
22
    {
23
        $this->detector = $detector;
24
        echo static::class . PHP_EOL;
25
    }
26
27
    protected function initResultObject()
28
    {
29
        // init default value from data
30
        foreach ($this->config['default'] as $defaultKey => $defaultValue) {
31
            Tools::runSetter($this->resultObject, $defaultKey, $defaultValue);
32
        }
33
    }
34
35
    public abstract function detect(string $ua);
36
37
    protected function detectByPattern(array $patternList)
38
    {
39
        foreach ($patternList as $patternId => $patternData) {
40
            $pattern = $this->getPattern($patternId, $patternData);
41
42
            if (preg_match($pattern['pattern'], $this->detector->getUserAgent())) {
43
                return ['name' => $patternId, 'version' => Tools::getVersion($pattern['version'], $this->detector->getUserAgent()), 'originalInfo' => $patternData];
44
            }
45
        }
46
        return null;
47
    }
48
49
    private function getPattern(string $patternId, array $patternData): array
50
    {
51
        if (array_key_exists('default', $patternData) && $patternData['default'] === true) {
52
            return ['pattern' => sprintf('/%s/', $patternId), 'version' => $patternId];
53
        }
54
        return ['pattern' => sprintf('/%s/', $patternData['pattern']), 'version' => array_key_exists('version', $patternData) ? $patternData['version'] : $patternId];
55
    }
56
57
    protected function setAttributes($info)
58
    {
59
        $result = $this->detector->getResultObject();
60
        if (array_key_exists('attributes', $info)) {
61
            foreach ($info['attributes'] as $attributeKey => $attributeValue) {
62
                Tools::runSetter($result, $attributeKey, $attributeValue);
63
            }
64
        }
65
    }
66
67 View Code Duplication
    protected function detectByType(): 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...
68
    {
69
        foreach ($this->config as $type => $patternList) {
70
            if ($type === 'default') {
71
                continue;
72
            }
73
            $browser = $this->detectByPattern($patternList);
74
            if ($browser) {
75
                return array_merge($browser, ['type' => $type]);
76
            }
77
        }
78
        return [];
79
    }
80
}