Passed
Pull Request — master (#124)
by Alexander
02:17
created

CommandLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 2
b 0
f 0
dl 0
loc 66
ccs 22
cts 25
cp 0.88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCommandInstance() 0 8 2
A get() 0 17 2
A has() 0 3 2
A getNames() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Command\LazyCommand;
10
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
11
use Symfony\Component\Console\Exception\CommandNotFoundException;
12
13
final class CommandLoader implements CommandLoaderInterface
14
{
15
    private ContainerInterface $container;
16
17
    /**
18
     * @var array<string, string>
19
     */
20
    private array $commandMap;
21
22
    /**
23
     * @param array<string, string> $commandMap An array with command names as keys and service ids as values
24
     */
25 33
    public function __construct(ContainerInterface $container, array $commandMap)
26
    {
27 33
        $this->container = $container;
28 33
        $this->commandMap = $commandMap;
29 33
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 9
    public function get(string $name)
35
    {
36 9
        if (!$this->has($name)) {
37
            throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
38
        }
39
40
        /** @var Command $commandClass */
41 9
        $commandClass = $this->commandMap[$name];
42 9
        $description = $commandClass::getDefaultDescription() ?? $this->getCommandInstance($name)->getName();
43
44 9
        return new LazyCommand(
45 9
            $name,
46 9
            [],
47
            $description,
0 ignored issues
show
Bug introduced by
It seems like $description can also be of type null; however, parameter $description of Symfony\Component\Consol...yCommand::__construct() 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

47
            /** @scrutinizer ignore-type */ $description,
Loading history...
48 9
            false,
49 9
            function () use ($name) {
50 8
                return $this->getCommandInstance($name);
51 9
            }
52
        );
53
    }
54
55 8
    private function getCommandInstance(string $name): Command
56
    {
57 8
        $command = $this->container->get($this->commandMap[$name]);
58 8
        if ($command->getName() !== $name) {
59 1
            $command->setName($name);
60
        }
61
62 8
        return $command;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 9
    public function has(string $name)
69
    {
70 9
        return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getNames()
77
    {
78
        return array_keys($this->commandMap);
79
    }
80
}
81