Completed
Pull Request — develop (#32)
by Michael
05:23
created

AbstractBot::reply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Crummy\Phlack\Bot;
4
5
use Crummy\Phlack\Common\Exception\InvalidArgumentException;
6
use Crummy\Phlack\Common\Matcher;
7
use Crummy\Phlack\Common\Responder\ResponderInterface;
8
use Crummy\Phlack\Message\MessageInterface;
9
10
abstract class AbstractBot implements ResponderAware, Matcher\MatcherAggregate
11
{
12
    private $matcher;
13
14
    /**
15
     * @var \Crummy\Phlack\Common\Responder\ResponderInterface
16
     */
17
    protected $responder;
18
19
    /**
20
     * @param Matcher\MatcherInterface|\Closure $matcher
21
     * @param ResponderInterface                $responder
22
     */
23 11
    public function __construct($matcher = null, ResponderInterface $responder = null)
24
    {
25 11
        $matcher = $matcher ?: new Matcher\DefaultMatcher();
26
27 11
        $this->setMatcher($matcher);
0 ignored issues
show
Bug introduced by
It seems like $matcher defined by $matcher ?: new \Crummy\...atcher\DefaultMatcher() on line 25 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...
28
29 11
        if ($responder) {
30 11
            $this->setResponder($responder);
31 11
        }
32 11
    }
33
34
    /**
35
     * @param Matcher\MatcherInterface $matcher
36
     *
37
     * @throws \Crummy\Phlack\Common\Exception\InvalidArgumentException When given an invalid matcher.
38
     *
39
     * @return $this
40
     */
41 11
    public function setMatcher($matcher)
42
    {
43 11
        if (!$matcher instanceof Matcher\MatcherInterface && !is_callable($matcher)) {
44 1
            throw new InvalidArgumentException(sprintf(
45 1
                'The matcher must be callable, or implement \Crummy\Phlack\Common\Matcher\MatcherInterface. "%" given.',
46 1
                is_object($matcher) ? get_class($matcher) : gettype($matcher)
47 1
            ));
48
        }
49
50 11
        $this->matcher = $matcher;
51
52 11
        return $this;
53
    }
54
55
    /**
56
     * @return Matcher\MatcherInterface|callable
57
     */
58 2
    public function getMatcher()
59
    {
60 2
        return $this->matcher;
61
    }
62
63
    /**
64
     * @param ResponderInterface $responder
65
     *
66
     * @return self
67
     */
68 11
    public function setResponder(ResponderInterface $responder)
69
    {
70 11
        $this->responder = $responder;
71
72 11
        return $this;
73
    }
74
75
    /**
76
     * @param string $text
77
     *
78
     * @return \Crummy\Phlack\WebHook\Reply\Reply
79
     */
80 2
    protected function say($text)
81
    {
82 2
        return $this->responder->say($text);
83
    }
84
85
    /**
86
     * @param string $text
87
     *
88
     * @return \Crummy\Phlack\WebHook\Reply\Reply
89
     */
90
    protected function emote($text)
91
    {
92
        return $this->responder->emote($text);
93
    }
94
95
    /**
96
     * @param string $user The user_id to tell
97
     * @param string $text
98
     *
99
     * @return \Crummy\Phlack\WebHook\Reply\Reply
100
     */
101
    protected function tell($user, $text)
102
    {
103
        return $this->responder->tell($user, $text);
104
    }
105
106
    /**
107
     * @param \Crummy\Phlack\WebHook\CommandInterface $user The user_id, or a CommandInterface to inspect.
108
     * @param string                                  $text
109
     *
110
     * @return \Crummy\Phlack\WebHook\Reply\Reply
111
     */
112 4
    protected function reply($user, $text)
113
    {
114 4
        return $this->responder->reply($user, $text);
115
    }
116
117
    /**
118
     * @param string $text
119
     *
120
     * @return \Crummy\Phlack\WebHook\Reply\Reply
121
     */
122
    protected function shout($text)
123
    {
124
        return $this->responder->shout($text);
125
    }
126
127
    /**
128
     * @param MessageInterface $message
129
     *
130
     * @return \Crummy\Phlack\WebHook\Reply\EmptyReply
131
     */
132
    protected function send(MessageInterface $message)
133
    {
134
        return $this->responder->send($message);
135
    }
136
}
137