Completed
Pull Request — develop (#32)
by Michael
02:21
created

AbstractBot   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 73.32%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 15
c 8
b 2
f 0
lcom 2
cbo 3
dl 0
loc 125
ccs 22
cts 30
cp 0.7332
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A setMatcher() 0 13 4
A getMatcher() 0 4 1
A setResponder() 0 5 1
A say() 0 4 1
A emote() 0 4 1
A tell() 0 4 1
A reply() 0 4 1
A shout() 0 4 1
A send() 0 4 1
1
<?php
2
3
namespace Crummy\Phlack\Bot;
4
5
use Crummy\Phlack\Common\Exception\InvalidArgumentException;
6
use Crummy\Phlack\Common\Iterocitor;
7
use Crummy\Phlack\Common\Matcher;
8
use Crummy\Phlack\Common\Responder\ResponderInterface;
9
use Crummy\Phlack\Message\MessageInterface;
10
11
abstract class AbstractBot implements ResponderAware, Matcher\MatcherAggregate
12
{
13
    private $matcher;
14
15
    /**
16
     * @var \Crummy\Phlack\Common\Responder\ResponderInterface
17
     */
18
    protected $responder;
19
20
    /**
21
     * @param Matcher\MatcherInterface|\Closure $matcher
22
     * @param ResponderInterface $responder
23
     */
24 11
    public function __construct($matcher = null, ResponderInterface $responder = null)
25
    {
26 11
        $matcher = $matcher ?: new Matcher\DefaultMatcher();
27
28 11
        $this->setMatcher($matcher);
0 ignored issues
show
Bug introduced by
It seems like $matcher defined by $matcher ?: new \Crummy\...atcher\DefaultMatcher() on line 26 can also be of type object<Closure>; however, Crummy\Phlack\Bot\AbstractBot::setMatcher() does only seem to accept object<Crummy\Phlack\Com...tcher\MatcherInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
29
30 11
        if ($responder) {
31 11
            $this->setResponder($responder);
32
        }
33 11
    }
34
35
    /**
36
     * @param Matcher\MatcherInterface $matcher
37
     *
38
     * @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher.
39
     *
40
     * @return $this
41
     */
42 11
    public function setMatcher($matcher)
43
    {
44 11
        if (!$matcher instanceof Matcher\MatcherInterface && !is_callable($matcher)) {
45 1
            throw new InvalidArgumentException(sprintf(
46 1
                'The matcher must be callable, or implement \Crummy\Phlack\Common\Matcher\MatcherInterface. "%" given.',
47 1
                is_object($matcher) ? get_class($matcher) : gettype($matcher)
48
            ));
49
        }
50
51 11
        $this->matcher = $matcher;
52
53 11
        return $this;
54
    }
55
56
    /**
57
     * @return Matcher\MatcherInterface|callable
58
     */
59 2
    public function getMatcher()
60
    {
61 2
        return $this->matcher;
62
    }
63
64
    /**
65
     * @param ResponderInterface $responder
66
     * @return self
67
     */
68 11
    public function setResponder(ResponderInterface $responder)
69
    {
70 11
        $this->responder = $responder;
71 11
        return $this;
72
    }
73
74
    /**
75
     * @param string $text
76
     *
77
     * @return \Crummy\Phlack\WebHook\Reply\Reply
78
     */
79 2
    protected function say($text)
80
    {
81 2
        return $this->responder->say($text);
82
    }
83
84
    /**
85
     * @param string $text
86
     *
87
     * @return \Crummy\Phlack\WebHook\Reply\Reply
88
     */
89
    protected function emote($text)
90
    {
91
        return $this->responder->emote($text);
92
    }
93
94
    /**
95
     * @param string $user The user_id to tell
96
     * @param string $text
97
     *
98
     * @return \Crummy\Phlack\WebHook\Reply\Reply
99
     */
100
    protected function tell($user, $text)
101
    {
102
        return $this->responder->tell($user, $text);
103
    }
104
105
    /**
106
     * @param \Crummy\Phlack\WebHook\CommandInterface $user The user_id, or a CommandInterface to inspect.
107
     * @param string                                  $text
108
     *
109
     * @return \Crummy\Phlack\WebHook\Reply\Reply
110
     */
111 4
    protected function reply($user, $text)
112
    {
113 4
        return $this->responder->reply($user, $text);
114
    }
115
116
    /**
117
     * @param string $text
118
     *
119
     * @return \Crummy\Phlack\WebHook\Reply\Reply
120
     */
121
    protected function shout($text)
122
    {
123
        return $this->responder->shout($text);
124
    }
125
126
    /**
127
     * @param MessageInterface $message
128
     *
129
     * @return \Crummy\Phlack\WebHook\Reply\EmptyReply
130
     */
131
    protected function send(MessageInterface $message)
132
    {
133
        return $this->responder->send($message);
134
    }
135
}
136