Completed
Push — master ( 8ed961...3d9e83 )
by Dan
05:14
created

MatcherTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 70
Duplicated Lines 11.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 8
loc 70
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
C matches() 8 36 11
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
    public function matches(Message $message)
17
    {
18
        if ($message->isHandled()) {
19
            $this->debug($this->name.': message already handled');
0 ignored issues
show
Bug introduced by
It seems like debug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
            return false;
21
        }
22
23
        if ($this->isBot !== null && $this->isBot !== $message->isBot()) {
24
            $this->debug($this->name.': isBot match failed');
0 ignored issues
show
Bug introduced by
It seems like debug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
            return false;
26
        }
27
28 View Code Duplication
        if (!empty($this->channels) && !in_array($message->getChannelName(), $this->channels)) {
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...
29
            $this->debug($this->name.': channels match failed '.json_encode($this->channels));
0 ignored issues
show
Bug introduced by
It seems like debug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
            return false;
31
        }
32
33 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...
34
            $this->debug($this->name.': users match failed '.json_encode($this->users));
0 ignored issues
show
Bug introduced by
It seems like debug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
            return false;
36
        }
37
38
        if (empty($this->patterns)) {
39
            return true;
40
        }
41
42
        $matches = [];
43
        $text = $message->getPluginText();
44
        foreach ($this->patterns as $pattern) {
45
            if (preg_match($pattern, $text, $matches)) {
46
                break;
47
            }
48
        }
49
50
        return $matches;
51
    }
52
53
    private function setName($name)
54
    {
55
        $this->name = $name;
56
    }
57
58
    private function setIsBot($isBot)
59
    {
60
        $this->isBot = $isBot;
61
    }
62
63
    private function setChannels(array $channels)
64
    {
65
        $this->channels = $channels;
66
    }
67
68
    private function setUsers(array $users)
69
    {
70
        $this->users = $users;
71
    }
72
73
    private function setPatterns(array $patterns)
74
    {
75
        $this->patterns = $patterns;
76
    }
77
}