Completed
Push — gh-27-android-devices ( 0ab8c2...175b3f )
by Serhii
07:11
created

ModelDetector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupResultObject() 0 15 5
A detectModelByPattern() 0 11 3
A detectModelByModelList() 0 13 3
A detect() 0 13 3
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2018
6
 * @version 4.0.2
7
 * @project endorphin-studio/browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector\Detection;
11
12
use EndorphinStudio\Detector\Data\Model;
13
14
class ModelDetector extends AbstractDetection
15
{
16
    /**
17
     * @var string Key in config (os, device, etc.)
18
     */
19
    protected $configKey = 'model';
20
21
    /**
22
     * Setup result of object
23
     */
24
    protected function setupResultObject()
25
    {
26
        $name = $this->additionalInfo['name'];
27
        if (!\array_key_exists($name, $this->config)) {
28
            return;
29
        }
30
        $patternList = $this->config[$name];
31
        foreach ($patternList as $series => $data) {
32
            if(array_key_exists('pattern', $data)) {
33
                $this->detectModelByPattern($series,$data['pattern']);
34
            } elseif (array_key_exists('models', $data)) {
35
                $this->detectModelByModelList($series, $data['models']);
36
            }
37
        }
38
    }
39
40
    private function detectModelByPattern(string $series, string $pattern)
41
    {
42
        $pattern = sprintf('/%s/', $pattern);
43
        $result = \preg_match($pattern, $this->detector->getUserAgent(), $model);
44
        if ($result) {
45
            $model = count($model) > 1 ? sprintf('%s%s', $series, end($model)) : $series;
46
            $this->resultObject->setModel($model);
47
            $this->resultObject->setSeries($series);
48
            return $result;
49
        }
50
    }
51
52
    private function detectModelByModelList(string $series, array $data)
53
    {
54
        foreach ($data as $model => $pattern) {
55
            $pattern = sprintf('/%s/', $pattern);
56
            $result = \preg_match($pattern, $this->detector->getUserAgent());
57
            if ($result) {
58
                $model = sprintf('%s%s', $series, $model);
59
                $this->resultObject->setModel($model);
60
                $this->resultObject->setSeries($series);
61
                return $result;
62
            }
63
        }
64
    }
65
66
    /**
67
     * Detect method
68
     * @param array $additional Additional Info
69
     */
70
    public function detect(array $additional = [])
71
    {
72
        $this->additionalInfo = $additional;
73
        if ($this->configKey !== 'none') {
74
            $this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), $this->configKey);
75
            $this->resultObject = new Model();
76
        }
77
        $this->setupResultObject();
78
        if ($this->resultObject->getModel()) {
0 ignored issues
show
Bug introduced by
The method getModel does only exist in EndorphinStudio\Detector\Data\Model, but not in EndorphinStudio\Detector\Data\AbstractData.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
            $this->detector->getResultObject()->getDevice()->setModel($this->resultObject);
80
            $this->detector->getResultObject()->getDevice()->setHasModel(true);
81
        }
82
    }
83
}