Passed
Push — feature/collation ( e2b273...7d249b )
by Kit Loong
64:57
created

StringField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B makeField() 0 32 10
A __construct() 0 10 1
A getPgSQLEnumValue() 0 12 4
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\Support\Regex;
19
20
class StringField
21
{
22
    private $collationModifier;
23
    private $charsetModifier;
24
    private $pgSQLRepository;
25
    private $regex;
26
27
    public function __construct(
28
        CollationModifier $collationModifier,
29
        CharsetModifier $charsetModifier,
30
        PgSQLRepository $pgSQLRepository,
31
        Regex $regex
32
    ) {
33
        $this->collationModifier = $collationModifier;
34
        $this->charsetModifier = $charsetModifier;
35
        $this->pgSQLRepository = $pgSQLRepository;
36
        $this->regex = $regex;
37
    }
38
39
    public function makeField(string $tableName, array $field, Column $column): array
40
    {
41
        if (($pgSQLEnum = $this->getPgSQLEnumValue($tableName, $column->getName())) !== '') {
42
            $field['type'] = ColumnType::ENUM;
43
            $field['args'][] = $pgSQLEnum;
44
        } else {
45
            if ($field['field'] === ColumnName::REMEMBER_TOKEN && $column->getLength() === 100 && !$column->getFixed()) {
46
                $field['type'] = ColumnType::REMEMBER_TOKEN;
47
                $field['field'] = null;
48
                $field['args'] = [];
49
            } else {
50
                if ($column->getFixed()) {
51
                    $field['type'] = ColumnType::CHAR;
52
                }
53
54
                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...
55
                    $field['args'][] = $column->getLength();
56
                }
57
            }
58
        }
59
60
        $charset = $this->charsetModifier->generate($tableName, $column);
61
        if ($charset !== '') {
62
            $field['decorators'][] = $charset;
63
        }
64
65
        $collation = $this->collationModifier->generate($tableName, $column);
66
        if ($collation !== '') {
67
            $field['decorators'][] = $collation;
68
        }
69
70
        return $field;
71
    }
72
73
    private function getPgSQLEnumValue(string $tableName, string $column): string
74
    {
75
        if (app(MigrationsGeneratorSetting::class)->getPlatform() === Platform::POSTGRESQL) {
76
            $definition = ($this->pgSQLRepository->getCheckConstraintDefinition($tableName, $column));
77
            if (!empty($definition)) {
78
                $enumValues = $this->regex->getTextBetweenAll($definition, "'", "'::");
79
                if (!empty($enumValues)) {
80
                    return "['".implode("', '", $enumValues)."']";
81
                }
82
            }
83
        }
84
        return '';
85
    }
86
}
87