Debug   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 19
dl 0
loc 50
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommandWithArguments() 0 11 3
A createCommandWithMemorySubCommandAndArguments() 0 6 1
A addArgument() 0 3 1
A __construct() 0 4 1
A withArguments() 0 6 1
A createCommandWithHelpSubCommandAndArguments() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Redislabs\Module\ReJSON\Command;
5
6
use Redislabs\Interfaces\CommandInterface;
7
use Redislabs\Command\CommandAbstract;
8
use Redislabs\Module\ReJSON\Exceptions\InvalidDebugSubcommandException;
9
use Redislabs\Module\ReJSON\Path;
10
use function in_array;
11
12
final class Debug extends CommandAbstract implements CommandInterface
13
{
14
    protected static $command = 'JSON.DEBUG';
15
16
    private static $validSubCommands = ['MEMORY', 'HELP'];
17
18
    private function __construct(
19
        string $subcommand
20
    ) {
21
        $this->arguments = [$subcommand];
22
    }
23
24
    private function addArgument($arg)
25
    {
26
        $this->arguments[] = $arg;
27
    }
28
29
    private function withArguments(string $key, Path $path)
30
    {
31
        $new = clone $this;
32
        $new->addArgument($key);
33
        $new->addArgument($path->getPath());
34
        return $new;
35
    }
36
37
    public static function createCommandWithArguments(string $subcommand, ?string $key, string $path) : CommandInterface
38
    {
39
        if (!in_array($subcommand, self::$validSubCommands, true)) {
40
            throw new InvalidDebugSubcommandException(
41
                sprintf('%s is not a valid debug subcommand.', $subcommand)
42
            );
43
        }
44
        if ($subcommand === 'MEMORY') {
45
            return self::createCommandWithMemorySubCommandAndArguments($key, $path);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type null; however, parameter $key of Redislabs\Module\ReJSON\...ubCommandAndArguments() 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

45
            return self::createCommandWithMemorySubCommandAndArguments(/** @scrutinizer ignore-type */ $key, $path);
Loading history...
46
        }
47
        return self::createCommandWithHelpSubCommandAndArguments();
48
    }
49
50
    public static function createCommandWithMemorySubCommandAndArguments(string $key, string $path) : CommandInterface
51
    {
52
        $debugObj = new self(
53
            'MEMORY'
54
        );
55
        return $debugObj->withArguments($key, new Path($path));
56
    }
57
58
    public static function createCommandWithHelpSubCommandAndArguments() : CommandInterface
59
    {
60
        return new self(
61
            'HELP'
62
        );
63
    }
64
}
65