DefaultModifier::generate()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 20
nc 12
nop 2
dl 0
loc 24
ccs 20
cts 20
cp 1
crap 12
rs 6.9666
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/31
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators\Modifier;
9
10
use Doctrine\DBAL\Schema\Column;
11
use KitLoong\MigrationsGenerator\Generators\BooleanField;
12
use KitLoong\MigrationsGenerator\Generators\DatetimeField;
13
use KitLoong\MigrationsGenerator\Generators\Decorator;
14
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
15
use KitLoong\MigrationsGenerator\Types\DBALTypes;
16
17
class DefaultModifier
18
{
19
    private $booleanField;
20
    private $datetimeField;
21
    private $decorator;
22
23 63
    public function __construct(BooleanField $booleanField, DatetimeField $datetimeField, Decorator $decorator)
24
    {
25 63
        $this->booleanField = $booleanField;
26 63
        $this->datetimeField = $datetimeField;
27 63
        $this->decorator = $decorator;
28 63
    }
29
30
    /**
31
     * @param  string  $dbalType
32
     * @param  Column  $column
33
     * @return string
34
     */
35 12
    public function generate(string $dbalType, Column $column): string
36
    {
37 8
        switch ($dbalType) {
38 4
            case DBALTypes::SMALLINT:
39 4
            case DBALTypes::INTEGER:
40 4
            case DBALTypes::BIGINT:
41 4
            case DBALTypes::MEDIUMINT:
42 4
            case DBALTypes::TINYINT:
43 4
            case DBALTypes::DECIMAL:
44 4
            case DBALTypes::FLOAT:
45 4
            case DBALTypes::DOUBLE:
46 3
                $default = $column->getDefault();
47 3
                break;
48 3
            case DBALTypes::BOOLEAN:
49 3
                $default = $this->booleanField->makeDefault($column);
50 3
                break;
51 2
            case DBALTypes::DATETIME_MUTABLE:
52 2
            case DBALTypes::TIMESTAMP:
53 3
                return $this->datetimeField->makeDefault($column);
54
            default:
55 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

55
                $default = $this->decorator->columnDefaultToString(/** @scrutinizer ignore-type */ $column->getDefault());
Loading history...
56
        }
57
58 9
        return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]);
59
    }
60
}
61