Issues (291)

src/Utils/ImmutableCaster.php (1 issue)

Severity
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\DBAL\Types\Types;
9
10
class ImmutableCaster
11
{
12
    public static function castSchemaToImmutable(Schema $schema): void
13
    {
14
        foreach ($schema->getTables() as $table) {
15
            foreach ($table->getColumns() as $column) {
16
                self::toImmutableType($column);
17
            }
18
        }
19
    }
20
21
    /**
22
     * Changes the type of a column to an immutable date type if the type is a date.
23
     * This is needed because by default, when reading a Schema, Doctrine assumes a mutable datetime.
24
     */
25
    private static function toImmutableType(Column $column): void
26
    {
27
        $mapping = [
28
            Types::DATE_MUTABLE => Types::DATE_IMMUTABLE,
29
            Types::DATETIME_MUTABLE => Types::DATETIME_IMMUTABLE,
30
            Types::DATETIMETZ_MUTABLE => Types::DATETIMETZ_IMMUTABLE,
31
            Types::TIME_MUTABLE => Types::TIME_IMMUTABLE
32
        ];
33
34
        $typeName = $column->getType()->getName();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Types\Type::getName() has been deprecated: this method will be removed in Doctrine DBAL 4.0, use {@see TypeRegistry::lookupName()} instead. ( Ignorable by Annotation )

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

34
        $typeName = /** @scrutinizer ignore-deprecated */ $column->getType()->getName();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
35
        if (isset($mapping[$typeName])) {
36
            $column->setType(Type::getType($mapping[$typeName]));
37
        }
38
    }
39
}
40