Passed
Push — master ( 7131c7...46dbbc )
by Alexander
05:55 queued 03:02
created

SchemaCommand::displaySchema()   D

Complexity

Conditions 22
Paths 25

Size

Total Lines 126
Code Lines 93

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 506

Importance

Changes 0
Metric Value
cc 22
eloc 93
c 0
b 0
f 0
nc 25
nop 3
dl 0
loc 126
ccs 0
cts 82
cp 0
crap 506
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Command\Schema;
6
7
use Cycle\Schema\Renderer\OutputSchemaRenderer;
8
use Cycle\Schema\Renderer\SchemaToArrayConverter;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Console\ExitCode;
14
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
15
16
final class SchemaCommand extends Command
17
{
18
    protected static $defaultName = 'cycle/schema';
19
20
    private CycleDependencyProxy $promise;
21
22
    public function __construct(CycleDependencyProxy $promise)
23
    {
24
        $this->promise = $promise;
25
        parent::__construct();
26
    }
27
28
    public function configure(): void
29
    {
30
        $this->setDescription('Shown current schema');
31
        $this->addArgument('role', InputArgument::OPTIONAL, 'Roles to display (separated by ",").');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        /** @var string|null $roleArgument */
37
        $roleArgument = $input->getArgument('role');
38
        $schema = $this->promise->getSchema();
39
        $roles = $roleArgument !== null ? explode(',', $roleArgument) : $schema->getRoles();
40
41
        $schemaArray = (new SchemaToArrayConverter())->convert($schema);
42
43
        $notFound = [];
44
        $found = [];
45
        foreach ($roles as $role) {
46
            if (!\array_key_exists($role, $schemaArray)) {
47
                $notFound[] = $role;
48
                continue;
49
            }
50
            $found[$role] = $schemaArray[$role];
51
        }
52
        $renderer = new OutputSchemaRenderer(OutputSchemaRenderer::FORMAT_CONSOLE_COLOR);
53
        $output->write($renderer->render($found));
54
55
        if ($notFound !== []) {
56
            $output->writeln(sprintf('<fg=red>Undefined roles: %s</>', implode(', ', $notFound)));
57
        }
58
59
        return ExitCode::OK;
60
    }
61
}
62