Skip to content

Commit d85d579

Browse files
committed
Remove deprecated Type::getName usage
1 parent 2cec022 commit d85d579

27 files changed

+156
-200
lines changed

src/DBAL/Models/DBALColumn.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace KitLoong\MigrationsGenerator\DBAL\Models;
44

55
use Doctrine\DBAL\Schema\Column as DoctrineDBALColumn;
6-
use KitLoong\MigrationsGenerator\DBAL\Types\Types;
76
use KitLoong\MigrationsGenerator\Enum\Migrations\ColumnName;
87
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType;
98
use KitLoong\MigrationsGenerator\Schema\Models\Column;
@@ -105,7 +104,7 @@ public function __construct(string $table, DoctrineDBALColumn $column)
105104
{
106105
$this->tableName = $table;
107106
$this->name = $column->getName();
108-
$this->type = $this->mapToColumnType($column->getType()->getName());
107+
$this->type = ColumnType::fromDBALType($column->getType());
109108
$this->length = $column->getLength();
110109
$this->scale = $column->getScale();
111110
$this->precision = $column->getPrecision();
@@ -272,33 +271,6 @@ public function isRawDefault(): bool
272271
return $this->rawDefault;
273272
}
274273

275-
/**
276-
* Converts built-in DBALTypes to ColumnType (Laravel column).
277-
*
278-
* @param string $dbalType
279-
* @return \KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType
280-
*/
281-
private function mapToColumnType(string $dbalType): ColumnType
282-
{
283-
$map = [
284-
Types::BIGINT => ColumnType::BIG_INTEGER(),
285-
Types::BLOB => ColumnType::BINARY(),
286-
Types::DATE_MUTABLE => ColumnType::DATE(),
287-
Types::DATE_IMMUTABLE => ColumnType::DATE(),
288-
Types::DATETIME_MUTABLE => ColumnType::DATETIME(),
289-
Types::DATETIME_IMMUTABLE => ColumnType::DATETIME(),
290-
Types::DATETIMETZ_MUTABLE => ColumnType::DATETIME_TZ(),
291-
Types::DATETIMETZ_IMMUTABLE => ColumnType::DATETIME_TZ(),
292-
Types::SMALLINT => ColumnType::SMALL_INTEGER(),
293-
Types::GUID => ColumnType::UUID(),
294-
Types::TIME_MUTABLE => ColumnType::TIME(),
295-
Types::TIME_IMMUTABLE => ColumnType::TIME(),
296-
];
297-
298-
// $dbalType outside from the map has the same name with ColumnType.
299-
return $map[$dbalType] ?? ColumnType::from($dbalType);
300-
}
301-
302274
/**
303275
* Set the column type to "increments" or "*Increments" if the column is auto increment.
304276
* If the DB supports unsigned, should check if the column is unsigned.

src/DBAL/RegisterColumnType.php

Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,13 @@
44

55
use Doctrine\DBAL\Platforms\AbstractPlatform;
66
use Doctrine\DBAL\Types\Type;
7+
use Doctrine\DBAL\Types\Types as DoctrineDBALTypes;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\Facades\DB;
910
use KitLoong\MigrationsGenerator\DBAL\Types\CustomType;
10-
use KitLoong\MigrationsGenerator\DBAL\Types\DoubleType;
11-
use KitLoong\MigrationsGenerator\DBAL\Types\EnumType;
12-
use KitLoong\MigrationsGenerator\DBAL\Types\GeometryCollectionType;
13-
use KitLoong\MigrationsGenerator\DBAL\Types\GeometryType;
14-
use KitLoong\MigrationsGenerator\DBAL\Types\IpAddressType;
15-
use KitLoong\MigrationsGenerator\DBAL\Types\JsonbType;
16-
use KitLoong\MigrationsGenerator\DBAL\Types\LineStringType;
17-
use KitLoong\MigrationsGenerator\DBAL\Types\LongTextType;
18-
use KitLoong\MigrationsGenerator\DBAL\Types\MacAddressType;
19-
use KitLoong\MigrationsGenerator\DBAL\Types\MediumIntegerType;
20-
use KitLoong\MigrationsGenerator\DBAL\Types\MediumTextType;
21-
use KitLoong\MigrationsGenerator\DBAL\Types\MultiLineStringType;
22-
use KitLoong\MigrationsGenerator\DBAL\Types\MultiPointType;
23-
use KitLoong\MigrationsGenerator\DBAL\Types\MultiPolygonType;
24-
use KitLoong\MigrationsGenerator\DBAL\Types\PointType;
25-
use KitLoong\MigrationsGenerator\DBAL\Types\PolygonType;
26-
use KitLoong\MigrationsGenerator\DBAL\Types\SetType;
27-
use KitLoong\MigrationsGenerator\DBAL\Types\TimestampType;
28-
use KitLoong\MigrationsGenerator\DBAL\Types\TimestampTzType;
29-
use KitLoong\MigrationsGenerator\DBAL\Types\TimeTzType;
30-
use KitLoong\MigrationsGenerator\DBAL\Types\TinyIntegerType;
3111
use KitLoong\MigrationsGenerator\DBAL\Types\Types;
32-
use KitLoong\MigrationsGenerator\DBAL\Types\UUIDType;
33-
use KitLoong\MigrationsGenerator\DBAL\Types\YearType;
3412
use KitLoong\MigrationsGenerator\Enum\Driver;
13+
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType;
3514
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;
3615

3716
class RegisterColumnType
@@ -53,30 +32,29 @@ public function handle(): void
5332

5433
$doctrineTypes = [
5534
Driver::MYSQL()->getValue() => [
56-
'bit' => Types::BOOLEAN,
57-
'geomcollection' => Types::GEOMETRY_COLLECTION,
58-
'json' => Types::JSON,
59-
'mediumint' => Types::MEDIUM_INTEGER,
60-
'tinyint' => Types::TINY_INTEGER,
35+
'bit' => DoctrineDBALTypes::BOOLEAN,
36+
'geomcollection' => ColumnType::GEOMETRY_COLLECTION,
37+
'mediumint' => ColumnType::MEDIUM_INTEGER,
38+
'tinyint' => ColumnType::TINY_INTEGER,
6139
],
6240
Driver::PGSQL()->getValue() => [
63-
'_int4' => Types::TEXT,
64-
'_int8' => Types::TEXT,
65-
'_numeric' => Types::FLOAT,
66-
'_text' => Types::TEXT,
67-
'cidr' => Types::STRING,
68-
'geography' => Types::GEOMETRY,
69-
'inet' => Types::IP_ADDRESS,
70-
'macaddr' => Types::MAC_ADDRESS,
71-
'oid' => Types::STRING,
41+
'_int4' => DoctrineDBALTypes::TEXT,
42+
'_int8' => DoctrineDBALTypes::TEXT,
43+
'_numeric' => DoctrineDBALTypes::FLOAT,
44+
'_text' => DoctrineDBALTypes::TEXT,
45+
'cidr' => DoctrineDBALTypes::STRING,
46+
'geography' => ColumnType::GEOMETRY,
47+
'inet' => ColumnType::IP_ADDRESS,
48+
'macaddr' => ColumnType::MAC_ADDRESS,
49+
'oid' => DoctrineDBALTypes::STRING,
7250
],
7351
Driver::SQLITE()->getValue() => [],
7452
Driver::SQLSRV()->getValue() => [
75-
'geography' => Types::GEOMETRY,
76-
'money' => Types::DECIMAL,
77-
'smallmoney' => Types::DECIMAL,
78-
'tinyint' => Types::TINY_INTEGER,
79-
'xml' => Types::TEXT,
53+
'geography' => ColumnType::GEOMETRY,
54+
'money' => DoctrineDBALTypes::DECIMAL,
55+
'smallmoney' => DoctrineDBALTypes::DECIMAL,
56+
'tinyint' => ColumnType::TINY_INTEGER,
57+
'xml' => DoctrineDBALTypes::TEXT,
8058
],
8159
];
8260

@@ -93,38 +71,18 @@ public function handle(): void
9371
*/
9472
private function registerLaravelColumnType(): void
9573
{
96-
/**
97-
* The map of supported doctrine mapping types.
98-
*/
99-
$typeMap = [
100-
// [$name => $className]
101-
Types::DOUBLE => DoubleType::class,
102-
Types::ENUM => EnumType::class,
103-
Types::GEOMETRY => GeometryType::class,
104-
Types::GEOMETRY_COLLECTION => GeometryCollectionType::class,
105-
Types::IP_ADDRESS => IpAddressType::class,
106-
Types::JSONB => JsonbType::class,
107-
Types::LINE_STRING => LineStringType::class,
108-
Types::LONG_TEXT => LongTextType::class,
109-
Types::MAC_ADDRESS => MacAddressType::class,
110-
Types::MEDIUM_INTEGER => MediumIntegerType::class,
111-
Types::MEDIUM_TEXT => MediumTextType::class,
112-
Types::MULTI_LINE_STRING => MultiLineStringType::class,
113-
Types::MULTI_POINT => MultiPointType::class,
114-
Types::MULTI_POLYGON => MultiPolygonType::class,
115-
Types::POINT => PointType::class,
116-
Types::POLYGON => PolygonType::class,
117-
Types::SET => SetType::class,
118-
Types::TIMESTAMP => TimestampType::class,
119-
Types::TIMESTAMP_TZ => TimestampTzType::class,
120-
Types::TIME_TZ => TimeTzType::class,
121-
Types::TINY_INTEGER => TinyIntegerType::class,
122-
Types::UUID => UUIDType::class,
123-
Types::YEAR => YearType::class,
124-
];
74+
/** @var array<string, string> $typesMap */
75+
$typesMap = array_flip(Types::ADDITIONAL_TYPES_MAP);
76+
77+
foreach ($typesMap as $type => $doctrineTypeClassName) {
78+
// Add a new type by providing a `type` name and a `\Doctrine\DBAL\Types\Type` class name.
79+
// eg: `$type = double`, `$doctrineTypeClassName = \KitLoong\MigrationsGenerator\DBAL\Types\DoubleType`
80+
$this->addOrOverrideType($type, $doctrineTypeClassName);
12581

126-
foreach ($typeMap as $dbType => $class) {
127-
$this->overrideDoctrineType($dbType, $class);
82+
// Register type mapping so that Doctrine DBAL can recognize the DB column type.
83+
// eg: `$type = double`
84+
// Now Doctrine DBAL can recognize `column double NOT NULL` and create a column instance with type `\KitLoong\MigrationsGenerator\DBAL\Types\DoubleType`.
85+
$this->registerDoctrineTypeMapping($type, $type);
12886
}
12987
}
13088

@@ -177,34 +135,21 @@ private function getCustomTypes(): Collection
177135
return new Collection();
178136
}
179137

180-
/**
181-
* Register custom doctrine type, override if exists.
182-
*
183-
* @param string $dbType
184-
* @param string $class The class name of the custom type.
185-
* @throws \Doctrine\DBAL\Exception
186-
*/
187-
private function overrideDoctrineType(string $dbType, string $class): void
188-
{
189-
$this->addOrOverrideType($dbType, $class);
190-
$this->registerDoctrineTypeMapping($dbType, $dbType);
191-
}
192-
193138
/**
194139
* Add or override doctrine type.
195140
*
196-
* @param string $dbType
197-
* @param string $class The class name of the custom type.
141+
* @param string $type
142+
* @param string $class The class name which is extends {@see \Doctrine\DBAL\Types\Type}.
198143
* @throws \Doctrine\DBAL\Exception
199144
*/
200-
private function addOrOverrideType(string $dbType, string $class): void
145+
private function addOrOverrideType(string $type, string $class): void
201146
{
202-
if (!Type::hasType($dbType)) {
203-
Type::addType($dbType, $class);
147+
if (!Type::hasType($type)) {
148+
Type::addType($type, $class);
204149
return;
205150
}
206151

207-
Type::overrideType($dbType, $class);
152+
Type::overrideType($type, $class);
208153
}
209154

210155
/**

src/DBAL/Types/DoubleType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::DOUBLE;
27+
return '';
2828
}
2929
}

src/DBAL/Types/EnumType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::ENUM;
27+
return '';
2828
}
2929
}

src/DBAL/Types/GeometryCollectionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::GEOMETRY_COLLECTION;
27+
return '';
2828
}
2929
}

src/DBAL/Types/GeometryType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::GEOMETRY;
27+
return '';
2828
}
2929
}

src/DBAL/Types/IpAddressType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::IP_ADDRESS;
27+
return '';
2828
}
2929
}

src/DBAL/Types/JsonbType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::JSONB;
27+
return '';
2828
}
2929
}

src/DBAL/Types/LineStringType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::LINE_STRING;
27+
return '';
2828
}
2929
}

src/DBAL/Types/LongTextType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::LONG_TEXT;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MacAddressType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MAC_ADDRESS;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MediumIntegerType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MEDIUM_INTEGER;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MediumTextType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MEDIUM_TEXT;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MultiLineStringType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MULTI_LINE_STRING;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MultiPointType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MULTI_POINT;
27+
return '';
2828
}
2929
}

src/DBAL/Types/MultiPolygonType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::MULTI_POLYGON;
27+
return '';
2828
}
2929
}

src/DBAL/Types/PointType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::POINT;
27+
return '';
2828
}
2929
}

src/DBAL/Types/PolygonType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::POLYGON;
27+
return '';
2828
}
2929
}

src/DBAL/Types/SetType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::SET;
27+
return '';
2828
}
2929
}

src/DBAL/Types/TimeTzType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::TIME_TZ;
27+
return '';
2828
}
2929
}

src/DBAL/Types/TimestampType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::TIMESTAMP;
27+
return '';
2828
}
2929
}

src/DBAL/Types/TimestampTzType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::TIMESTAMP_TZ;
27+
return '';
2828
}
2929
}

src/DBAL/Types/TinyIntegerType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
2424
*/
2525
public function getName()
2626
{
27-
return Types::TINY_INTEGER;
27+
return '';
2828
}
2929
}

0 commit comments

Comments
 (0)