Passed
Push — feature/collation ( 3b40b8...35613b )
by Kit Loong
64:11
created

StringField::makeField()   C

Complexity

Conditions 15
Paths 120

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 30
c 2
b 0
f 0
nc 120
nop 3
dl 0
loc 49
rs 5.75

How to fix   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
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators;
9
10
use Doctrine\DBAL\Schema\Column;
11
use Illuminate\Database\Schema\Builder;
12
use KitLoong\MigrationsGenerator\Generators\Modifier\CharsetModifier;
13
use KitLoong\MigrationsGenerator\Generators\Modifier\CollationModifier;
14
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName;
15
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
16
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
17
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
18
use KitLoong\MigrationsGenerator\Repositories\SQLSrvRepository;
19
use KitLoong\MigrationsGenerator\Support\Regex;
20
21
class StringField
22
{
23
    const SQLSRV_TEXT_TYPE = 'nvarchar';
24
    const SQLSRV_TEXT_LENGTH = -1;
25
26
    private $collationModifier;
27
    private $charsetModifier;
28
    private $pgSQLRepository;
29
    private $sqlSrvRepository;
30
    private $regex;
31
32
    public function __construct(
33
        CollationModifier $collationModifier,
34
        CharsetModifier $charsetModifier,
35
        PgSQLRepository $pgSQLRepository,
36
        SQLSrvRepository $sqlSrvRepository,
37
        Regex $regex
38
    ) {
39
        $this->collationModifier = $collationModifier;
40
        $this->charsetModifier = $charsetModifier;
41
        $this->pgSQLRepository = $pgSQLRepository;
42
        $this->sqlSrvRepository = $sqlSrvRepository;
43
        $this->regex = $regex;
44
    }
45
46
    public function makeField(string $tableName, array $field, Column $column): array
47
    {
48
        switch (app(MigrationsGeneratorSetting::class)->getPlatform()) {
49
            // It could be pgsql enum
50
            case Platform::POSTGRESQL:
51
                if (($pgSQLEnum = $this->getPgSQLEnumValue($tableName, $column->getName())) !== '') {
52
                    $field['type'] = ColumnType::ENUM;
53
                    $field['args'][] = $pgSQLEnum;
54
                }
55
                break;
56
            // It could be sqlsrv text
57
            case Platform::SQLSERVER:
58
                $colDef = $this->sqlSrvRepository->getColumnDefinition($tableName, $column->getName());
59
                if ($colDef->getType() === self::SQLSRV_TEXT_TYPE &&
60
                    $colDef->getLength() === self::SQLSRV_TEXT_LENGTH) {
61
                    $field['type'] = ColumnType::TEXT;
62
                }
63
                break;
64
            default:
65
        }
66
67
        // Continue if type is `string`
68
        if ($field['type'] === ColumnType::STRING) {
69
            if ($field['field'] === ColumnName::REMEMBER_TOKEN && $column->getLength() === 100 && !$column->getFixed()) {
70
                $field['type'] = ColumnType::REMEMBER_TOKEN;
71
                $field['field'] = null;
72
                $field['args'] = [];
73
            } else {
74
                if ($column->getFixed()) {
75
                    $field['type'] = ColumnType::CHAR;
76
                }
77
78
                if ($column->getLength() && $column->getLength() !== Builder::$defaultStringLength) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $column->getLength() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
79
                    $field['args'][] = $column->getLength();
80
                }
81
            }
82
        }
83
84
        $charset = $this->charsetModifier->generate($tableName, $column);
85
        if ($charset !== '') {
86
            $field['decorators'][] = $charset;
87
        }
88
89
        $collation = $this->collationModifier->generate($tableName, $column);
90
        if ($collation !== '') {
91
            $field['decorators'][] = $collation;
92
        }
93
94
        return $field;
95
    }
96
97
    private function getPgSQLEnumValue(string $tableName, string $column): string
98
    {
99
        $definition = ($this->pgSQLRepository->getCheckConstraintDefinition($tableName, $column));
100
        if (!empty($definition)) {
101
            $enumValues = $this->regex->getTextBetweenAll($definition, "'", "'::");
102
            if (!empty($enumValues)) {
103
                return "['".implode("', '", $enumValues)."']";
104
            }
105
        }
106
        return '';
107
    }
108
}
109