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

PluginMatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 22.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 12
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
C matches() 12 24 8
A debug() 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\Plugin;
4
5
6
use Nopolabs\Yabot\Helpers\LogTrait;
7
use Nopolabs\Yabot\Message\Message;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\LogLevel;
10
11
class PluginMatcher
12
{
13
    use LogTrait;
14
15
    private $pluginId;
16
    private $isBot;
17
    private $channels;
18
    private $users;
19
20
    public function __construct(
21
        string $pluginId,
22
        bool $isBot = null,
23
        array $channels,
24
        array $users,
25
        LoggerInterface $logger)
26
    {
27
        $this->pluginId = $pluginId;
28
        $this->isBot = $isBot;
29
        $this->channels = $channels;
30
        $this->users = $users;
31
        $this->setLog($logger);
32
    }
33
34
    public function matches(Message $message) : bool
35
    {
36
        if ($message->isHandled()) {
37
            $this->debug('message already handled');
38
            return false;
39
        }
40
41 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...
42
            $this->debug('isBot match failed');
43
            return false;
44
        }
45
46 View Code Duplication
        if ($this->channels && !in_array($message->getChannelName(), $this->channels)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->channels 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...
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...
47
            $this->debug('channels match failed '.json_encode($this->channels));
48
            return false;
49
        }
50
51 View Code Duplication
        if ($this->users && !in_array($message->getUsername(), $this->users)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->users 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...
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...
52
            $this->debug('users match failed '.json_encode($this->users));
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59
    private function debug($msg)
60
    {
61
        $this->log(LogLevel::DEBUG, "PluginMatcher {$this->pluginId}: $msg");
62
    }
63
}
64