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

MatcherTrait::matches()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 8
Ratio 22.22 %

Importance

Changes 0
Metric Value
dl 8
loc 36
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 21
nc 8
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\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
}