Completed
Push — master ( 1b605f...95b7c3 )
by Dan
06:00
created

MethodMatcher::matches()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 37
Code Lines 22

Duplication

Lines 8
Ratio 21.62 %

Importance

Changes 0
Metric Value
dl 8
loc 37
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 22
nc 13
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Nopolabs\Yabot\Plugin;
4
5
6
use Nopolabs\Yabot\Helpers\LogTrait;
7
use Nopolabs\Yabot\Message\Message;
8
use Psr\Log\LoggerInterface;
9
10
class MethodMatcher
11
{
12
    use LogTrait;
13
14
    private $name;
15
    private $isBot;
16
    private $channels;
17
    private $users;
18
    private $patterns;
19
    private $method;
20
21
    public function __construct(
22
        string $name,
23
        $isBot,
24
        array $channels,
25
        array $users,
26
        array $patterns,
27
        string $method,
28
        LoggerInterface $logger)
29
    {
30
        $this->name = $name;
31
        $this->isBot = $isBot;
32
        $this->channels = $channels;
33
        $this->users = $users;
34
        $this->patterns = $patterns;
35
        $this->method = $method;
36
        $this->setLog($logger);
37
    }
38
39
    public function getName(): string
40
    {
41
        return $this->name;
42
    }
43
44
    public function getMethod(): string
45
    {
46
        return $this->method;
47
    }
48
49
    public function matches(Message $message)
50
    {
51
        if ($message->isHandled()) {
52
            $this->debug('message already handled');
53
            return false;
54
        }
55
56 View Code Duplication
        if ($this->isBot !== null && $this->isBot !== $message->isBot()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
            $this->debug('isBot match failed');
58
            return false;
59
        }
60
61
        if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) {
62
            $this->debug('channels match failed '.json_encode($this->channels));
63
            return false;
64
        }
65
66 View Code Duplication
        if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
            $this->debug('users match failed '.json_encode($this->users));
68
            return false;
69
        }
70
71
        $matches = [];
72
        if (!empty($this->patterns)) {
73
            $text = $message->getPluginText();
74
            foreach ($this->patterns as $pattern) {
75
                if (preg_match($pattern, $text, $matches)) {
76
                    break;
77
                }
78
            }
79
            if (!$matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches 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...
80
                return false;
81
            }
82
        }
83
84
        return $matches;
85
    }
86
87
    private function debug($msg)
88
    {
89
        $this->debug("MethodMatcher {$this->name}: $msg");
90
    }
91
}