Passed
Push — 2.x ( 927817...94cb61 )
by Aleksei
13:29
created

NameBasedOnChangesGenerator::generate()   F

Complexity

Conditions 14
Paths 516

Size

Total Lines 62
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 32
nc 516
nop 1
dl 0
loc 62
rs 2.7722
c 1
b 0
f 0

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 Cycle\Schema\Generator\Migrations;
6
7
use Cycle\Database\Schema\AbstractTable;
8
use Cycle\Migrations\Atomizer\Atomizer;
9
10
final class NameBasedOnChangesGenerator implements NameGeneratorInterface
11
{
12
    public function generate(Atomizer $atomizer): string
13
    {
14
        $name = [];
15
16
        foreach ($atomizer->getTables() as $table) {
17
            if ($table->getStatus() === AbstractTable::STATUS_NEW) {
18
                $name[] = 'create_' . $table->getName();
19
                continue;
20
            }
21
22
            if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) {
23
                $name[] = 'drop_' . $table->getName();
24
                continue;
25
            }
26
27
            if ($table->getComparator()->isRenamed()) {
28
                $name[] = 'rename_' . $table->getInitialName();
29
                continue;
30
            }
31
32
            $name[] = 'change_' . $table->getName();
33
34
            $comparator = $table->getComparator();
35
36
            foreach ($comparator->addedColumns() as $column) {
37
                $name[] = 'add_' . $column->getName();
38
            }
39
40
            foreach ($comparator->droppedColumns() as $column) {
41
                $name[] = 'rm_' . $column->getName();
42
            }
43
44
            foreach ($comparator->alteredColumns() as $column) {
45
                $name[] = 'alter_' . $column[0]->getName();
46
            }
47
48
            foreach ($comparator->addedIndexes() as $index) {
49
                $name[] = 'add_index_' . $index->getName();
50
            }
51
52
            foreach ($comparator->droppedIndexes() as $index) {
53
                $name[] = 'rm_index_' . $index->getName();
54
            }
55
56
            foreach ($comparator->alteredIndexes() as $index) {
57
                $name[] = 'alter_index_' . $index[0]->getName();
58
            }
59
60
            foreach ($comparator->addedForeignKeys() as $fk) {
61
                $name[] = 'add_fk_' . $fk->getName();
62
            }
63
64
            foreach ($comparator->droppedForeignKeys() as $fk) {
65
                $name[] = 'rm_fk_' . $fk->getName();
66
            }
67
68
            foreach ($comparator->alteredForeignKeys() as $fk) {
69
                $name[] = 'alter_fk_' . $fk[0]->getName();
70
            }
71
        }
72
73
        return \implode('_', $name);
74
    }
75
}
76