Completed
Push — 4.0 ( 73f7eb...90ad36 )
by Serhii
02:22
created

RobotDetector::detectByConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
namespace EndorphinStudio\Detector\Detection;
4
5
use EndorphinStudio\Detector\Tools;
6
7
class RobotDetector extends AbstractDetection
8
{
9
    protected $configKey = 'robot';
10
11
    /** @var array */
12
    private $homepages;
13
14
    protected function setupResultObject()
15
    {
16
        $this->homepages = $this->config['homepages'];
17
        $this->config = $this->config['types'];
18
        $object = $this->detectByConfig();
19
        foreach ($object as $key => $value) {
20
            Tools::runSetter($this->resultObject, $key, $value);
21
        }
22
        if (\array_key_exists('name', $object) && $object['name']) {
23
            $this->detector->getResultObject()->setIsRobot(true);
24
        }
25
    }
26
27
    protected function detectByConfig(): array
28
    {
29
        foreach ($this->config as $type => $companyList) {
30
            $data = $this->detectByType($type);
31
            if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data 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...
32
                return \array_merge($data, ['homepage' => \array_key_exists($data['owner'], $this->homepages) ? $this->homepages[$data['owner']] : null, 'type' => $type]);
33
            }
34
        }
35
        return [];
36
    }
37
38
    protected function detectByType($key = 'none'): array
39
    {
40
        foreach ($this->config[$key] as $companyName => $patternList) {
41
            $data = $this->detectByPattern($patternList);
42
            if ($data) {
43
                return \array_merge($data, ['owner' => $companyName]);
44
            }
45
        }
46
        return [];
47
    }
48
49
    protected function detectByPattern(array $patternList)
50
    {
51
        foreach ($patternList as $name => $pattern) {
52
            $pattern = $this->getPattern($pattern);
53
54
            if (preg_match($pattern, $this->detector->getUserAgent())) {
55
                return ['name' => $name];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('name' => $name); (array<string,integer|string>) is incompatible with the return type of the parent method EndorphinStudio\Detector...ection::detectByPattern of type array|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
            }
57
        }
58
        return null;
59
    }
60
61
    private function getPattern(string $pattern): string
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
62
    {
63
        return sprintf('/%s/', $pattern);
64
    }
65
}