Passed
Pull Request — master (#16)
by
unknown
01:58
created

SchemaCommand::execute()   F

Complexity

Conditions 20
Paths 793

Size

Total Lines 100
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 20
eloc 72
c 2
b 1
f 0
nc 793
nop 2
dl 0
loc 100
ccs 0
cts 84
cp 0
crap 420
rs 0.2875

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
namespace Yiisoft\Yii\Cycle\Command;
4
5
use Cycle\ORM\Relation;
6
use Cycle\ORM\Schema;
7
use Cycle\ORM\SchemaInterface;
8
use Spiral\Database\DatabaseManager;
9
use Spiral\Migrations\Config\MigrationConfig;
10
use Spiral\Migrations\Migrator;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Yiisoft\Yii\Console\ExitCode;
14
15
class SchemaCommand extends BaseMigrationCommand
16
{
17
    protected static $defaultName = 'cycle/schema';
18
19
    private SchemaInterface $schema;
20
    private const STR_RELATION = [
21
        Relation::HAS_ONE => 'has one',
22
        Relation::HAS_MANY => 'has many',
23
        Relation::BELONGS_TO => 'belongs to',
24
        Relation::REFERS_TO => 'refers to',
25
        Relation::MANY_TO_MANY => 'many to many',
26
        Relation::BELONGS_TO_MORPHED => 'belongs to morphed',
27
        Relation::MORPHED_HAS_ONE => 'morphed has one',
28
        Relation::MORPHED_HAS_MANY => 'morphed has many',
29
    ];
30
    private const STR_PREFETCH_MODE = [
31
        Relation::LOAD_PROMISE => 'promise',
32
        Relation::LOAD_EAGER => 'eager',
33
    ];
34
35
    public function __construct(
36
        DatabaseManager $dbal,
37
        MigrationConfig $conf,
38
        Migrator $migrator,
39
        SchemaInterface $schema
40
    ) {
41
        parent::__construct($dbal, $conf, $migrator);
42
        $this->schema = $schema;
43
    }
44
45
    public function configure(): void
46
    {
47
        $this->setDescription('Shown current schema');
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        foreach ($this->schema->getRoles() as $role) {
53
            $output->write("<fg=red>[{$role}</>");
54
            $alias = $this->schema->resolveAlias($role);
55
            // alias
56
            if ($alias !== null && $alias !== $role) {
57
                $output->write("=><fg=red>{$alias}</>");
58
            }
59
            // table
60
            $output->write("<fg=red>]</>");
61
62
            // database
63
            $database = $this->schema->define($role, Schema::DATABASE);
64
            $table = $this->schema->define($role, Schema::TABLE);
65
            if ($database !== null) {
66
                $output->write(" :: <fg=green>{$database}</>.<fg=green>{$table}</>");
67
            }
68
            $output->writeln('');
69
70
            // Entity
71
            $entity = $this->schema->define($role, Schema::ENTITY);
72
            $output->write('   Entity     : ');
73
            $output->writeln($entity === null ? 'no entity' : "<fg=blue>{$entity}</>");
74
            // Mapper
75
            $mapper = $this->schema->define($role, Schema::MAPPER);
76
            $output->write('   Mapper     : ');
77
            $output->writeln($mapper === null ? 'no mapper' : "<fg=blue>{$mapper}</>");
78
            // Constrain
79
            $constrain = $this->schema->define($role, Schema::CONSTRAIN);
80
            $output->write('   Constrain  : ');
81
            $output->writeln($constrain === null ? 'no constrain' : "<fg=blue>{$constrain}</>");
82
            // Repository
83
            $repository = $this->schema->define($role, Schema::REPOSITORY);
84
            $output->write('   Repository : ');
85
            $output->writeln($repository === null ? 'no repository' : "<fg=blue>{$repository}</>");
86
            // PK
87
            $pk = $this->schema->define($role, Schema::PRIMARY_KEY);
88
            $output->write('   Primary key: ');
89
            $output->writeln($pk === null ? 'no primary key' : "<fg=green>{$pk}</>");
90
            // Fields
91
            $columns = $this->schema->define($role, Schema::COLUMNS);
92
            $output->writeln("   Fields     :");
93
            $output->writeln("     (<fg=cyan>property</> -> <fg=green>db.field</> -> <fg=blue>typecast</>)");
94
            $types = $this->schema->define($role, Schema::TYPECAST);
95
            foreach ($columns as $property => $field) {
96
                $typecast = $types[$property] ?? $types[$field] ?? null;
97
                $output->write("     <fg=cyan>{$property}</> -> <fg=green>{$field}</>");
98
                if ($typecast !== null) {
99
                    $output->write(" -> <fg=blue>{$typecast}</>");
100
                }
101
                $output->writeln('');
102
            }
103
104
            // Relations
105
            $relations = $this->schema->define($role, Schema::RELATIONS);
106
            if (count($relations) > 0) {
107
                $output->writeln('   Relations  :');
108
                foreach ($relations as $field => $relation) {
109
                    $type = self::STR_RELATION[$relation[Relation::TYPE] ?? ''] ?? '?';
110
                    $target = $relation[Relation::TARGET] ?? '?';
111
                    $loading = self::STR_PREFETCH_MODE[$relation[Relation::LOAD] ?? ''] ?? '?';
112
                    $relSchema = $relation[Relation::SCHEMA];
113
                    $innerKey = $relSchema[Relation::INNER_KEY] ?? '?';
114
                    $outerKey = $relSchema[Relation::OUTER_KEY] ?? '?';
115
                    $where = $relSchema[Relation::WHERE] ?? [];
116
                    $cascade = $relSchema[Relation::CASCADE] ?? null;
117
                    $cascadeStr = $cascade ? 'cascaded' : 'not cascaded';
118
                    $nullable = $relSchema[Relation::NULLABLE] ?? null;
119
                    $nullableStr = $nullable ? 'nullable' : ($nullable === false ? 'not null' : 'n/a');
120
                    // Many-To-Many relation(s) options
121
                    $mmInnerKey = $relSchema[Relation::THROUGH_INNER_KEY] ?? '?';
122
                    $mmOuterKey = $relSchema[Relation::THROUGH_OUTER_KEY] ?? '?';
123
                    $mmEntity = $relSchema[Relation::THROUGH_ENTITY] ?? null;
124
                    $mmWhere = $relSchema[Relation::THROUGH_WHERE] ?? [];
125
                    // print
126
                    $output->write("     <fg=red>{$role}</>-><fg=cyan>{$field}</> {$type} <fg=red>{$target}</> {$loading} load");
127
                    $output->writeln(" <fg=yellow>{$cascadeStr}</>");
128
                    $output->write("       {$nullableStr} <fg=green>{$table}</>.<fg=green>{$innerKey}</> <=");
129
                    if ($mmEntity !== null) {
130
                        $output->write(" <fg=red>{$mmEntity}</>.<fg=green>{$mmInnerKey}</>");
131
                        $output->write("|");
132
                        $output->write("<fg=red>{$mmEntity}</>.<fg=green>{$mmOuterKey}</> ");
133
                    }
134
                    $output->writeln("=> <fg=red>{$target}</>.<fg=green>{$outerKey}</> ");
135
                    if (count($where)) {
136
                        $output->write("       Where:");
137
                        $output->writeln(str_replace(["\r\n", "\n"], "\n       ", "\n" . print_r($where, 1)));
138
                    }
139
                    if (count($mmWhere)) {
140
                        $output->write("       Through where:");
141
                        $output->writeln(str_replace(["\r\n", "\n"], "\n       ", "\n" . print_r($mmWhere, 1)));
142
                    }
143
                }
144
            } else {
145
                $output->writeln('   No relations');
146
            }
147
        }
148
149
        return ExitCode::OK;
150
    }
151
}
152