Completed
Push — gh-27-android-devices ( 34c0bd )
by Serhii
02:00
created

ModelDetector::setupResultObject()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
namespace EndorphinStudio\Detector\Detection;
4
5
use EndorphinStudio\Detector\Data\Model;
6
7
class ModelDetector extends AbstractDetection
8
{
9
    /**
10
     * @var string Key in config (os, device, etc.)
11
     */
12
    protected $configKey = 'model';
13
14
    /**
15
     * Setup result of object
16
     */
17
    protected function setupResultObject()
18
    {
19
        $name = $this->additionalInfo['name'];
20
        if(!\array_key_exists($name, $this->config)) {
21
            return;
22
        }
23
        $patternList = $this->config[$name];
24
        foreach ($patternList as $series => $pattern) {
25
            $pattern = sprintf('/%s/', $pattern);
26
            $result = \preg_match($pattern, $this->detector->getUserAgent(), $model);
27
            if($result) {
28
                $this->resultObject->setModel(sprintf('%s%s', $series, $model[1]));
29
                $this->resultObject->setSeries($series);
30
            }
31
        }
32
    }
33
34
    /**
35
     * Detect method
36
     * @param array $additional Additional Info
37
     */
38
    public function detect(array $additional = [])
39
    {
40
        $this->additionalInfo = $additional;
41
        if ($this->configKey !== 'none') {
42
            $this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), $this->configKey);
43
            $this->resultObject = new Model();
44
        }
45
        $this->setupResultObject();
46
        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...
47
            $this->detector->getResultObject()->getDevice()->setModel($this->resultObject);
48
            $this->detector->getResultObject()->getDevice()->setHasModel(true);
49
        }
50
    }
51
}