Issues (66)

src/Command/RecallCommand.php (4 issues)

1
<?php
2
namespace PortlandLabs\Slackbot\Command;
3
4
use Illuminate\Support\Str;
5
use PortlandLabs\Slackbot\Bot;
6
use PortlandLabs\Slackbot\Command\Argument\Manager;
0 ignored issues
show
This use statement conflicts with another class in this namespace, PortlandLabs\Slackbot\Command\Manager. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use PortlandLabs\Slackbot\Permission\Checker;
8
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message;
9
use Psr\SimpleCache\CacheInterface;
10
use Psr\SimpleCache\CacheException;
11
12
class RecallCommand extends SimpleCommand
13
{
14
15
    protected $description = 'Recall things that have been `remember`ed';
16
17
    protected $signature = 'recall {thing?} {--l|list : List the things you can recall}';
18
19
    /** @var CacheInterface */
20
    protected $cache;
21
22
    public function __construct(Bot $bot, Manager $argumentManager, Checker $checker, CacheInterface $cache)
23
    {
24
        parent::__construct($bot, $argumentManager, $checker);
25
26
        $this->cache = $cache;
27
    }
28
29
    /**
30
     * @param Message $message
31
     * @param Manager $manager
32
     *
33
     * @throws \CL\Slack\Exception\SlackException
34
     * @throws \GuzzleHttp\Exception\GuzzleException
35
     */
36
    public function run(Message $message, Manager $manager)
37
    {
38
        if ($manager->get('list')) {
39
            $this->outputList($message);
40
            return;
41
        }
42
43
        $thing = $manager->get('thing');
44
        $key = Str::snake('remember ' . $thing);
45
46
        try {
47
            $value = $this->cache->get($key);
48
        } catch (CacheException $e) {
49
            $this->bot->logger()->error('[CMD.REC] Failed to recall: ' . $e->getMessage());
0 ignored issues
show
The method getMessage() does not exist on Psr\SimpleCache\CacheException. It seems like you code against a sub-type of Psr\SimpleCache\CacheException such as Cache\Adapter\Common\Exc...nvalidArgumentException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $this->bot->logger()->error('[CMD.REC] Failed to recall: ' . $e->/** @scrutinizer ignore-call */ getMessage());
Loading history...
50
        }
51
52
        if (!$value) {
53
            $this->bot->feignTyping($message->getChannel(), 'Hmm.. I\'m not able to recall that...');
0 ignored issues
show
It seems like $message->getChannel() can also be of type null; however, parameter $channel of PortlandLabs\Slackbot\Bot::feignTyping() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            $this->bot->feignTyping(/** @scrutinizer ignore-type */ $message->getChannel(), 'Hmm.. I\'m not able to recall that...');
Loading history...
54
            return;
55
        }
56
57
        $value = str_replace("\n", "\n> ", $value);
58
        $api = $this->bot->api();
59
        $api->getBuilder()
60
            ->send("Here's what I remember:\n> " . $value)->to($message->getChannel())
61
            ->withIcon(':thinking_face:')
62
            ->execute($api);
63
    }
64
65
    protected function outputList(Message $message)
66
    {
67
        $out = [];
68
        foreach ($this->cache->get('rememberindex', []) as $key => $thing) {
69
            $out[] = "`$thing`";
70
        }
71
72
        $last = null;
73
        if (count($out) > 1) {
74
            $last = array_pop($out);
75
        }
76
77
        $result = 'I can recall ' . implode(', ', $out);
78
79
        if ($last) {
80
            $result .= ' or ' . $last;
81
        }
82
83
        $this->bot->feignTyping($message->getChannel(), $result);
0 ignored issues
show
It seems like $message->getChannel() can also be of type null; however, parameter $channel of PortlandLabs\Slackbot\Bot::feignTyping() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $this->bot->feignTyping(/** @scrutinizer ignore-type */ $message->getChannel(), $result);
Loading history...
84
    }
85
}