Issues (66)

src/Command/RememberCommand.php (7 issues)

1
<?php
2
3
4
namespace PortlandLabs\Slackbot\Command;
5
6
7
use Illuminate\Support\Str;
8
use PortlandLabs\Slackbot\Bot;
9
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...
10
use PortlandLabs\Slackbot\Permission\Admin;
11
use PortlandLabs\Slackbot\Permission\Checker;
12
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message;
13
use Psr\SimpleCache\CacheInterface;
14
use Psr\SimpleCache\CacheException;
15
16
class RememberCommand extends SimpleCommand
17
{
18
19
    protected $description = 'Remember something interesting to be `recall`ed later';
20
21
    protected $signature = 'remember {thing} {value}';
22
23
    /** @var CacheInterface */
24
    protected $cache;
25
26
    protected $role = Admin::class;
27
28
    public function __construct(Bot $bot, Manager $argumentManager, Checker $checker, CacheInterface $cache)
29
    {
30
        parent::__construct($bot, $argumentManager, $checker);
31
32
        $this->cache = $cache;
33
        $this->checker = $checker;
34
    }
35
36
    /**
37
     * @param Message $message
38
     * @param Manager $manager
39
     */
40
    public function run(Message $message, Manager $manager)
41
    {
42
        $thing = $manager->get('thing');
43
        $text = $message->getText();
44
        $text = substr($text, strpos($text, '>'));
0 ignored issues
show
It seems like $text can also be of type null; however, parameter $haystack of strpos() 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

44
        $text = substr($text, strpos(/** @scrutinizer ignore-type */ $text, '>'));
Loading history...
It seems like $text can also be of type null; however, parameter $string of substr() 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

44
        $text = substr(/** @scrutinizer ignore-type */ $text, strpos($text, '>'));
Loading history...
45
46
        $value = trim(substr($text, strpos($text, $thing) + strlen($thing)));
0 ignored issues
show
It seems like $thing can also be of type boolean and null; however, parameter $needle of strpos() 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

46
        $value = trim(substr($text, strpos($text, /** @scrutinizer ignore-type */ $thing) + strlen($thing)));
Loading history...
It seems like $thing can also be of type boolean and null; however, parameter $string of strlen() 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

46
        $value = trim(substr($text, strpos($text, $thing) + strlen(/** @scrutinizer ignore-type */ $thing)));
Loading history...
47
48
        // trim off quotes
49
        $value = ltrim($value, '"\'');
50
51
        $key = Str::snake('remember ' . $thing);
52
53
        try {
54
            $this->cache->set($key, $value);
55
            $keys = $this->cache->get('rememberindex', []);
56
            $keys[$key] = $thing;
57
            $this->cache->set('rememberindex', $keys);
58
        } catch (CacheException $e) {
59
            $this->bot->logger()->error('[CMD.RMB] Failed to remember: ' . $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

59
            $this->bot->logger()->error('[CMD.RMB] Failed to remember: ' . $e->/** @scrutinizer ignore-call */ getMessage());
Loading history...
60
            $this->bot->feignTyping($message->getChannel(), 'Hmm.. I wasn\'t able to remember that... What was it again?');
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

60
            $this->bot->feignTyping(/** @scrutinizer ignore-type */ $message->getChannel(), 'Hmm.. I wasn\'t able to remember that... What was it again?');
Loading history...
61
            return;
62
        }
63
64
        $this->bot->feignTyping($message->getChannel(), 'Okay, I\'ve got that remembered! Use the `recall` command to recall.');
65
    }
66
}