GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a518ec...203b50 )
by Regan
8s
created

src/Message.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Maknz\Slack;
4
5
use InvalidArgumentException;
6
7
class Message
8
{
9
    /**
10
     * Reference to the Slack client responsible for sending
11
     * the message.
12
     *
13
     * @var \Maknz\Slack\Client
14
     */
15
    protected $client;
16
17
    /**
18
     * The text to send with the message.
19
     *
20
     * @var string
21
     */
22
    protected $text;
23
24
    /**
25
     * The channel the message should be sent to.
26
     *
27
     * @var string
28
     */
29
    protected $channel;
30
31
    /**
32
     * The username the message should be sent as.
33
     *
34
     * @var string
35
     */
36
    protected $username;
37
38
    /**
39
     * The URL to the icon to use.
40
     *
41
     * @var string
42
     */
43
    protected $icon;
44
45
    /**
46
     * The type of icon we are using.
47
     *
48
     * @var enum
49
     */
50
    protected $iconType;
51
52
    /**
53
     * Whether the message text should be interpreted in Slack's
54
     * Markdown-like language.
55
     *
56
     * @var bool
57
     */
58
    protected $allow_markdown = true;
59
60
    /**
61
     * The attachment fields which should be formatted with
62
     * Slack's Markdown-like language.
63
     *
64
     * @var array
65
     */
66
    protected $markdown_in_attachments = [];
67
68
    /**
69
     * An array of attachments to send.
70
     *
71
     * @var array
72
     */
73
    protected $attachments = [];
74
75
    /**
76
     * @var string
77
     */
78
    const ICON_TYPE_URL = 'icon_url';
79
80
    /**
81
     * @var string
82
     */
83
    const ICON_TYPE_EMOJI = 'icon_emoji';
84
85
    /**
86
     * Instantiate a new Message.
87
     *
88
     * @param \Maknz\Slack\Client $client
89
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
90
     */
91
    public function __construct(Client $client)
92
    {
93
        $this->client = $client;
94
    }
95
96
    /**
97
     * Get the message text.
98
     *
99
     * @return string
100
     */
101
    public function getText()
102
    {
103
        return $this->text;
104
    }
105
106
    /**
107
     * Set the message text.
108
     *
109
     * @param string $text
110
     * @return $this
111
     */
112
    public function setText($text)
113
    {
114
        $this->text = $text;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Get the channel we will post to.
121
     *
122
     * @return string
123
     */
124
    public function getChannel()
125
    {
126
        return $this->channel;
127
    }
128
129
    /**
130
     * Set the channel we will post to.
131
     *
132
     * @param string $channel
133
     * @return $this
134
     */
135
    public function setChannel($channel)
136
    {
137
        $this->channel = $channel;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the username we will post as.
144
     *
145
     * @return string
146
     */
147
    public function getUsername()
148
    {
149
        return $this->username;
150
    }
151
152
    /**
153
     * Set the username we will post as.
154
     *
155
     * @param string $username
156
     * @return $this
157
     */
158
    public function setUsername($username)
159
    {
160
        $this->username = $username;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Get the icon (either URL or emoji) we will post as.
167
     *
168
     * @return string
169
     */
170
    public function getIcon()
171
    {
172
        return $this->icon;
173
    }
174
175
    /**
176
     * Set the icon (either URL or emoji) we will post as.
177
     *
178
     * @param string $icon
179
     * @return this
180
     */
181
    public function setIcon($icon)
182
    {
183
        if ($icon == null) {
184
            $this->icon = $this->iconType = null;
185
186
            return;
187
        }
188
189
        if (mb_substr($icon, 0, 1) == ':' && mb_substr($icon, mb_strlen($icon) - 1, 1) == ':') {
190
            $this->iconType = self::ICON_TYPE_EMOJI;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::ICON_TYPE_EMOJI of type string is incompatible with the declared type object<Maknz\Slack\enum> of property $iconType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
191
        } else {
192
            $this->iconType = self::ICON_TYPE_URL;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::ICON_TYPE_URL of type string is incompatible with the declared type object<Maknz\Slack\enum> of property $iconType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
193
        }
194
195
        $this->icon = $icon;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Get the icon type being used, if an icon is set.
202
     *
203
     * @return string
204
     */
205
    public function getIconType()
206
    {
207
        return $this->iconType;
208
    }
209
210
    /**
211
     * Get whether message text should be formatted with
212
     * Slack's Markdown-like language.
213
     *
214
     * @return bool
215
     */
216
    public function getAllowMarkdown()
217
    {
218
        return $this->allow_markdown;
219
    }
220
221
    /**
222
     * Set whether message text should be formatted with
223
     * Slack's Markdown-like language.
224
     *
225
     * @param bool $value
226
     * @return void
227
     */
228
    public function setAllowMarkdown($value)
229
    {
230
        $this->allow_markdown = (bool) $value;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Enable Markdown formatting for the message.
237
     *
238
     * @return void
239
     */
240
    public function enableMarkdown()
241
    {
242
        $this->setAllowMarkdown(true);
243
244
        return $this;
245
    }
246
247
    /**
248
     * Disable Markdown formatting for the message.
249
     *
250
     * @return void
251
     */
252
    public function disableMarkdown()
253
    {
254
        $this->setAllowMarkdown(false);
255
256
        return $this;
257
    }
258
259
    /**
260
     * Get the attachment fields which should be formatted
261
     * in Slack's Markdown-like language.
262
     *
263
     * @return array
264
     */
265
    public function getMarkdownInAttachments()
266
    {
267
        return $this->markdown_in_attachments;
268
    }
269
270
    /**
271
     * Set the attachment fields which should be formatted
272
     * in Slack's Markdown-like language.
273
     *
274
     * @param array $fields
275
     * @return void
276
     */
277
    public function setMarkdownInAttachments(array $fields)
278
    {
279
        $this->markdown_in_attachments = $fields;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Change the name of the user the post will be made as.
286
     *
287
     * @param string $username
288
     * @return $this
289
     */
290
    public function from($username)
291
    {
292
        $this->setUsername($username);
293
294
        return $this;
295
    }
296
297
    /**
298
     * Change the channel the post will be made to.
299
     *
300
     * @param string $channel
301
     * @return $this
302
     */
303
    public function to($channel)
304
    {
305
        $this->setChannel($channel);
306
307
        return $this;
308
    }
309
310
    /**
311
     * Chainable method for setting the icon.
312
     *
313
     * @param string $icon
314
     * @return $this
315
     */
316
    public function withIcon($icon)
317
    {
318
        $this->setIcon($icon);
319
320
        return $this;
321
    }
322
323
    /**
324
     * Add an attachment to the message.
325
     *
326
     * @param mixed $attachment
327
     * @return $this
328
     */
329
    public function attach($attachment)
330
    {
331
        if ($attachment instanceof Attachment) {
332
            $this->attachments[] = $attachment;
333
334
            return $this;
335
        } elseif (is_array($attachment)) {
336
            $attachmentObject = new Attachment($attachment);
337
338
            if (! isset($attachment['mrkdwn_in'])) {
339
                $attachmentObject->setMarkdownFields($this->getMarkdownInAttachments());
340
            }
341
342
            $this->attachments[] = $attachmentObject;
343
344
            return $this;
345
        }
346
347
        throw new InvalidArgumentException('Attachment must be an instance of Maknz\\Slack\\Attachment or a keyed array');
348
    }
349
350
    /**
351
     * Get the attachments for the message.
352
     *
353
     * @return array
354
     */
355
    public function getAttachments()
356
    {
357
        return $this->attachments;
358
    }
359
360
    /**
361
     * Set the attachments for the message.
362
     *
363
     * @param string $attachments
364
     * @return $this
365
     */
366
    public function setAttachments(array $attachments)
367
    {
368
        $this->clearAttachments();
369
370
        foreach ($attachments as $attachment) {
371
            $this->attach($attachment);
372
        }
373
374
        return $this;
375
    }
376
377
    /**
378
     * Remove all attachments for the message.
379
     *
380
     * @return $this
381
     */
382
    public function clearAttachments()
383
    {
384
        $this->attachments = [];
385
386
        return $this;
387
    }
388
389
    /**
390
     * Send the message.
391
     *
392
     * @param string $text The text to send
393
     * @return void
394
     */
395
    public function send($text = null)
396
    {
397
        if ($text) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $text of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
398
            $this->setText($text);
399
        }
400
401
        $this->client->sendMessage($this);
402
    }
403
}
404