MatcherTrait   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 99
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 18
loc 99
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
debug() 0 1 ?
A matches() 0 20 5
A matchesIsBot() 0 9 3
A matchesChannels() 9 9 3
A matchesUsers() 9 9 3
A matchPatterns() 0 12 3
A setName() 0 4 1
A setIsBot() 0 4 1
A setChannels() 0 4 1
A setUsers() 0 4 1
A setPatterns() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nopolabs\Yabot\Helpers;
4
5
6
use Nopolabs\Yabot\Message\Message;
7
8
trait MatcherTrait
9
{
10
    private $name;
11
    private $isBot;
12
    private $channels;
13
    private $users;
14
    private $patterns;
15
16
    abstract protected function debug($message, array $context = array());
17
18
    public function matches(Message $message) : array
19
    {
20
        if (!$this->matchesIsBot($message)) {
21
            return [];
22
        }
23
24
        if (!$this->matchesChannels($message)) {
25
            return [];
26
        }
27
28
        if (!$this->matchesUsers($message)) {
29
            return [];
30
        }
31
32
        if (empty($this->patterns)) {
33
            return [$message->getFormattedText()];
34
        }
35
36
        return $this->matchPatterns($message);
37
    }
38
39
    protected function matchesIsBot(Message $message) : bool
40
    {
41
        if ($this->isBot !== null && $this->isBot !== $message->isBot()) {
42
            $this->debug($this->name.': isBot match failed');
43
            return false;
44
        }
45
46
        return true;
47
    }
48
49 View Code Duplication
    protected function matchesChannels(Message $message) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) {
52
            $this->debug($this->name.': channels match failed '.json_encode($this->channels));
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59 View Code Duplication
    protected function matchesUsers(Message $message) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        if (!empty($this->users) && !in_array($message->getUsername(), $this->users)) {
62
            $this->debug($this->name.': users match failed '.json_encode($this->users));
63
            return false;
64
        }
65
66
        return true;
67
    }
68
69
    protected function matchPatterns(Message $message): array
70
    {
71
        $matches = [];
72
        $text = $message->getPluginText();
73
        foreach ($this->patterns as $pattern) {
74
            if (preg_match($pattern, $text, $matches)) {
75
                break;
76
            }
77
        }
78
79
        return $matches;
80
    }
81
82
    private function setName($name)
83
    {
84
        $this->name = $name;
85
    }
86
87
    private function setIsBot($isBot)
88
    {
89
        $this->isBot = $isBot;
90
    }
91
92
    private function setChannels(array $channels)
93
    {
94
        $this->channels = $channels;
95
    }
96
97
    private function setUsers(array $users)
98
    {
99
        $this->users = $users;
100
    }
101
102
    private function setPatterns(array $patterns)
103
    {
104
        $this->patterns = $patterns;
105
    }
106
}