Completed
Push — feature/collation ( 3fa00f )
by Kit Loong
04:36
created

StringField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\CollationModifier;
13
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName;
14
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
15
16
class StringField
17
{
18
    private $collationModifier;
19
20
    public function __construct(CollationModifier $collationModifier)
21
    {
22
        $this->collationModifier = $collationModifier;
23
    }
24
25
    public function makeField(string $tableName, array $field, Column $column): array
26
    {
27
        if ($field['field'] === ColumnName::REMEMBER_TOKEN && $column->getLength() === 100 && !$column->getFixed()) {
28
            $field['type'] = ColumnType::REMEMBER_TOKEN;
29
            $field['field'] = null;
30
            $field['args'] = [];
31
        } else {
32
            if ($column->getFixed()) {
33
                $field['type'] = ColumnType::CHAR;
34
            }
35
36
            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...
37
                $field['args'][] = $column->getLength();
38
            }
39
        }
40
41
        $collation = $this->collationModifier->generate($tableName, $column);
42
        if ($collation !== '') {
43
            $field['decorators'][] = $collation;
44
        }
45
46
        return $field;
47
    }
48
}
49