DatetimeField::makeField()   B
last analyzed

Complexity

Conditions 10
Paths 37

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
nc 37
nop 3
dl 0
loc 27
ccs 17
cts 17
cp 1
crap 10
rs 7.6666
c 1
b 0
f 0

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 KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
12
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName;
13
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
14
15
class DatetimeField
16
{
17
    private $decorator;
18
19 63
    public function __construct(Decorator $decorator)
20
    {
21 63
        $this->decorator = $decorator;
22 63
    }
23
24 12
    public function makeField(array $field, Column $column, bool $useTimestamps): array
25
    {
26 12
        if ($useTimestamps) {
27 6
            if ($field['field'] === ColumnName::CREATED_AT) {
28 3
                return [];
29 3
            } elseif ($field['field'] === ColumnName::UPDATED_AT) {
30 3
                $field['type'] = ColumnType::TIMESTAMPS;
31 3
                $field['field'] = null;
32
            }
33
        }
34
35 9
        if ($field['field'] === ColumnName::DELETED_AT && !$column->getNotnull()) {
36 3
            $field['type'] = ColumnType::SOFT_DELETES;
37 3
            $field['field'] = null;
38
        }
39
40 9
        if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) {
41 3
            $field['type'] = FieldGenerator::$fieldTypeMap[$field['type']];
42
        }
43
44 9
        if ($column->getLength() !== null && $column->getLength() > 0) {
45 9
            if ($field['type'] === ColumnType::SOFT_DELETES) {
46 3
                $field['field'] = ColumnName::DELETED_AT;
47
            }
48 9
            $field['args'][] = $column->getLength();
49
        }
50 9
        return $field;
51
    }
52
53 6
    public function makeDefault(Column $column): string
54
    {
55 6
        if (in_array($column->getDefault(), ['CURRENT_TIMESTAMP'], true)) {
56 3
            return ColumnModifier::USE_CURRENT;
57
        } else {
58 3
            $default = $this->decorator->columnDefaultToString($column->getDefault());
0 ignored issues
show
Bug introduced by
It seems like $column->getDefault() can also be of type null; however, parameter $args of KitLoong\MigrationsGener...columnDefaultToString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            $default = $this->decorator->columnDefaultToString(/** @scrutinizer ignore-type */ $column->getDefault());
Loading history...
59 3
            return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]);
60
        }
61
    }
62
63
    /**
64
     * @param  Column[]  $columns
65
     * @return bool
66
     */
67 12
    public function isUseTimestamps($columns): bool
68
    {
69
        /** @var Column[] $timestampsColumns */
70 12
        $timestampsColumns = [];
71 12
        foreach ($columns as $column) {
72 12
            if ($column->getName() === ColumnName::CREATED_AT || $column->getName() === ColumnName::UPDATED_AT) {
73 12
                $timestampsColumns[] = $column;
74
            }
75
        }
76
77 12
        $useTimestamps = false;
78
79 12
        if (count($timestampsColumns) === 2) {
80 6
            $useTimestamps = true;
81 6
            foreach ($timestampsColumns as $timestamp) {
82 6
                if ($timestamp->getNotnull() || $timestamp->getDefault() !== null) {
83 3
                    $useTimestamps = false;
84
                }
85
            }
86
        }
87 12
        return $useTimestamps;
88
    }
89
}
90