BotCommand::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
4
namespace Sysbot\Telegram\ExtendedTypes;
5
6
7
use GuzzleHttp\Promise\PromiseInterface;
8
use Sysbot\Telegram\API;
9
use Sysbot\Telegram\Types\BotCommandScope;
10
11
trait BotCommand
12
{
13
14
    public function __construct(protected ?API $api)
15
    {
16
    }
17
18
    public function add(?BotCommandScope $scope = null, ?string $languageCode = null): ?PromiseInterface
19
    {
20
        return $this->api?->getMyCommands(scope: $scope, languageCode: $languageCode)->then(
21
            function (array $commands) use ($scope, $languageCode) {
22
                $commands = array_merge($commands, [$this]);
23
                return $this->api->setMyCommands(commands: $commands, scope: $scope, languageCode: $languageCode);
0 ignored issues
show
Bug introduced by
The method setMyCommands() does not exist on null. ( Ignorable by Annotation )

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

23
                return $this->api->/** @scrutinizer ignore-call */ setMyCommands(commands: $commands, scope: $scope, languageCode: $languageCode);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
            }
25
        );
26
    }
27
28
    public function remove(?BotCommandScope $scope = null, ?string $languageCode = null): ?PromiseInterface
29
    {
30
        return $this->api?->getMyCommands(scope: $scope, languageCode: $languageCode)->then(
31
            function (array $commands) use ($scope, $languageCode) {
32
                $commands = array_udiff(
33
                    $commands,
34
                    [$this],
35
                    fn(BotCommand $a, BotCommand $b) => $a->command <=> $b->command
36
                );
37
                return $this->api->setMyCommands(commands: $commands, scope: $scope, languageCode: $languageCode);
38
            }
39
        );
40
    }
41
42
}