Issues (6)

Generators/Modifier/DefaultModifier.php (1 issue)

Labels
Severity
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
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