Completed
Push — 4.x ( 0f520e...4acd94 )
by Kit Loong
01:32
created

IntegerField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 62
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 3
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeField() 3 13 3
A handleBoolean() 0 9 2
B handleInteger() 0 21 6
A checkIsMySQLBoolean() 0 13 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 * Time: 12:50
7
 */
8
9
namespace KitLoong\MigrationsGenerator\Generators;
10
11
use Doctrine\DBAL\Schema\Column;
12
use Illuminate\Support\Collection;
13
use Illuminate\Support\Facades\DB;
14
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
15
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
16
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
17
18
class IntegerField
19
{
20
    public function makeField(string $tableName, array $field, Column $column, Collection $indexes): array
21
    {
22 View Code Duplication
        if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
            $field['type'] = FieldGenerator::$fieldTypeMap[$field['type']];
24
        }
25
26
        $isBoolean = $this->checkIsMySQLBoolean($tableName, $field, $column);
27
        if ($isBoolean) {
28
            return $this->handleBoolean($field, $column);
29
        } else {
30
            return $this->handleInteger($field, $column, $indexes);
31
        }
32
    }
33
34
    private function handleBoolean(array $field, Column $column): array
35
    {
36
        $field['type'] = ColumnType::BOOLEAN;
37
        if ($column->getUnsigned()) {
38
            $field['decorators'][] = ColumnModifier::UNSIGNED;
39
        }
40
41
        return $field;
42
    }
43
44
    private function handleInteger(array $field, Column $column, Collection $indexes): array
45
    {
46
        if ($column->getUnsigned() && $column->getAutoincrement()) {
47
            if ($field['type'] === 'integer') {
48
                $field['type'] = ColumnType::INCREMENTS;
49
            } else {
50
                $field['type'] = str_replace('Integer', 'Increments', $field['type']);
51
            }
52
53
            $indexes->forget($field['field']);
54
        } else {
55
            if ($column->getUnsigned()) {
56
                $field['type'] = 'unsigned'.ucfirst($field['type']);
57
            }
58
            if ($column->getAutoincrement()) {
59
                $field['args'][] = 'true';
60
                $indexes->forget($field['field']);
61
            }
62
        }
63
        return $field;
64
    }
65
66
    private function checkIsMySQLBoolean(string $tableName, array $field, Column $column): bool
67
    {
68
        /** @var MigrationGeneratorSetting $setting */
69
        $setting = resolve(MigrationGeneratorSetting::class);
70
71
        if ($setting->getPlatform() === Platform::MYSQL &&
72
            $field['type'] === ColumnType::TINY_INTEGER &&
73
            !$column->getAutoincrement()) {
74
            $column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'");
75
            return !empty($column);
76
        }
77
        return false;
78
    }
79
}
80