ModelDetector::setupResultObject()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 12
rs 9.6111
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2020
6
 * @version 4.0.2
7
 * @project endorphin-studio/browser-detector-detection
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);
0 ignored issues
show
Bug introduced by
The method setModel() does not exist on EndorphinStudio\Detector\Data\AbstractData. It seems like you code against a sub-type of EndorphinStudio\Detector\Data\AbstractData such as EndorphinStudio\Detector\Data\Device. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            $this->resultObject->/** @scrutinizer ignore-call */ 
47
                                 setModel($model);
Loading history...
47
            $this->resultObject->setSeries($series);
0 ignored issues
show
Bug introduced by
The method setSeries() does not exist on EndorphinStudio\Detector\Data\AbstractData. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $this->resultObject->/** @scrutinizer ignore-call */ 
48
                                 setSeries($series);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new EndorphinStudio\Detector\Data\Model() of type EndorphinStudio\Detector\Data\Model is incompatible with the declared type EndorphinStudio\Detector\Data\AbstractData of property $resultObject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
        }
77
        $this->setupResultObject();
78
        if ($this->resultObject->getModel()) {
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on EndorphinStudio\Detector\Data\AbstractData. It seems like you code against a sub-type of EndorphinStudio\Detector\Data\AbstractData such as EndorphinStudio\Detector\Data\Device. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        if ($this->resultObject->/** @scrutinizer ignore-call */ getModel()) {
Loading history...
79
            $this->detector->getResultObject()->getDevice()->setModel($this->resultObject);
80
            $this->detector->getResultObject()->getDevice()->setHasModel(true);
81
        }
82
    }
83
}