Skip to content

Commit 4b66d02

Browse files
committed
Stop relying on Type::__toString
It is a somewhat fragile thing to do.
1 parent 8d18a33 commit 4b66d02

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919

2020
namespace Doctrine\DBAL\Platforms;
2121

22-
use Doctrine\DBAL\DBALException;
22+
use Doctrine\Common\EventManager;
2323
use Doctrine\DBAL\Connection;
24-
use Doctrine\DBAL\Schema\Identifier;
25-
use Doctrine\DBAL\Types;
24+
use Doctrine\DBAL\DBALException;
25+
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
26+
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
27+
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
28+
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
29+
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
30+
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
31+
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
32+
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
33+
use Doctrine\DBAL\Events;
34+
use Doctrine\DBAL\Schema\Column;
35+
use Doctrine\DBAL\Schema\ColumnDiff;
2636
use Doctrine\DBAL\Schema\Constraint;
37+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
38+
use Doctrine\DBAL\Schema\Identifier;
39+
use Doctrine\DBAL\Schema\Index;
2740
use Doctrine\DBAL\Schema\Sequence;
2841
use Doctrine\DBAL\Schema\Table;
29-
use Doctrine\DBAL\Schema\Index;
30-
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
3142
use Doctrine\DBAL\Schema\TableDiff;
32-
use Doctrine\DBAL\Schema\Column;
33-
use Doctrine\DBAL\Schema\ColumnDiff;
43+
use Doctrine\DBAL\Types;
3444
use Doctrine\DBAL\Types\Type;
35-
use Doctrine\DBAL\Events;
36-
use Doctrine\Common\EventManager;
37-
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
38-
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
39-
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
40-
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
41-
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
42-
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
43-
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
44-
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
4545

4646
/**
4747
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
@@ -1556,7 +1556,7 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
15561556
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false;
15571557
$columnData['comment'] = $this->getColumnComment($column);
15581558

1559-
if (strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
1559+
if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) {
15601560
$columnData['length'] = 255;
15611561
}
15621562

@@ -2279,15 +2279,20 @@ public function getDefaultValueDeclarationSQL($field)
22792279
if (isset($field['default'])) {
22802280
$default = " DEFAULT '".$field['default']."'";
22812281
if (isset($field['type'])) {
2282-
if (in_array((string) $field['type'], ["Integer", "BigInt", "SmallInt"])) {
2282+
$type = $field['type'];
2283+
if ($type instanceof Types\IntegerType ||
2284+
$type instanceof Types\BigIntType ||
2285+
$type instanceof Types\SmallIntType
2286+
) {
22832287
$default = " DEFAULT ".$field['default'];
2284-
} elseif (in_array((string) $field['type'], ['DateTime', 'DateTimeTz']) && $field['default'] == $this->getCurrentTimestampSQL()) {
2288+
} elseif (($type instanceof Types\DateTimeType || $type instanceof Types\DateTimeTzType)
2289+
&& $field['default'] == $this->getCurrentTimestampSQL()) {
22852290
$default = " DEFAULT ".$this->getCurrentTimestampSQL();
2286-
} elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
2291+
} elseif ($type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL()) {
22872292
$default = " DEFAULT ".$this->getCurrentTimeSQL();
2288-
} elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
2289-
$default = " DEFAULT ".$this->getCurrentDateSQL();
2290-
} elseif ((string) $field['type'] == 'Boolean') {
2293+
} elseif ($type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL()) {
2294+
$default = " DEFAULT ".$this>getCurrentDateSQL();
2295+
} elseif ($type instanceof Types\BooleanType) {
22912296
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
22922297
}
22932298
}

lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Doctrine\DBAL\Schema\Index;
2828
use Doctrine\DBAL\Schema\Table;
2929
use Doctrine\DBAL\Schema\TableDiff;
30+
use Doctrine\DBAL\Types;
3031

3132
/**
3233
* The SQLServerPlatform provides the behavior, features and SQL dialect of the
@@ -1554,15 +1555,21 @@ public function getDefaultValueDeclarationSQL($field)
15541555
return " DEFAULT '" . $field['default'] . "'";
15551556
}
15561557

1557-
if (in_array((string) $field['type'], ['Integer', 'BigInt', 'SmallInt'])) {
1558+
$type = $field['type'];
1559+
1560+
if ($type instanceof Types\IntegerType ||
1561+
$type instanceof Types\BigIntType ||
1562+
$type instanceof Types\SmallIntType
1563+
) {
15581564
return " DEFAULT " . $field['default'];
15591565
}
15601566

1561-
if (in_array((string) $field['type'], ['DateTime', 'DateTimeTz']) && $field['default'] == $this->getCurrentTimestampSQL()) {
1567+
if (($type instanceof Types\DateTimeType || $type instanceof Types\DateTimeTzType)
1568+
&& $field['default'] == $this->getCurrentTimestampSQL()) {
15621569
return " DEFAULT " . $this->getCurrentTimestampSQL();
15631570
}
15641571

1565-
if ((string) $field['type'] == 'Boolean') {
1572+
if ($type instanceof Types\BooleanType) {
15661573
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
15671574
}
15681575

lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Doctrine\DBAL\Schema\Index;
2828
use Doctrine\DBAL\Schema\Table;
2929
use Doctrine\DBAL\Schema\TableDiff;
30+
use Doctrine\DBAL\Types;
3031

3132
/**
3233
* The SqlitePlatform class describes the specifics and dialects of the SQLite
@@ -904,7 +905,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff)
904905
if ( ! $columnDiff->fromColumn instanceof Column ||
905906
! $columnDiff->column instanceof Column ||
906907
! $columnDiff->column->getAutoincrement() ||
907-
! (string) $columnDiff->column->getType() === 'Integer'
908+
! $columnDiff->column->getType() instanceof Types\IntegerType
908909
) {
909910
continue;
910911
}
@@ -915,9 +916,9 @@ private function getSimpleAlterTableSQL(TableDiff $diff)
915916
continue;
916917
}
917918

918-
$fromColumnType = (string) $columnDiff->fromColumn->getType();
919+
$fromColumnType = $columnDiff->fromColumn->getType();
919920

920-
if ($fromColumnType === 'SmallInt' || $fromColumnType === 'BigInt') {
921+
if ($fromColumnType instanceof Types\SmallIntType || $fromColumnType instanceof Types\BigIntType) {
921922
unset($diff->changedColumns[$oldColumnName]);
922923
}
923924
}
@@ -942,17 +943,17 @@ private function getSimpleAlterTableSQL(TableDiff $diff)
942943
}
943944

944945
$field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray());
945-
$type = (string) $field['type'];
946+
$type = $field['type'];
946947
switch (true) {
947948
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
948-
case $type == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL():
949-
case $type == 'Date' && $field['default'] == $this->getCurrentDateSQL():
950-
case $type == 'Time' && $field['default'] == $this->getCurrentTimeSQL():
949+
case $type instanceof Types\DateTimeType && $field['default'] == $this->getCurrentTimestampSQL():
950+
case $type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL():
951+
case $type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL():
951952
return false;
952953
}
953954

954955
$field['name'] = $column->getQuotedName($this);
955-
if (strtolower($field['type']) == 'string' && $field['length'] === null) {
956+
if ($type instanceof Types\StringType && $field['length'] === null) {
956957
$field['length'] = 255;
957958
}
958959

tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL461Test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doctrine\Tests\DBAL\Functional\Ticket;
44

55
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
6+
use Doctrine\DBAL\Types\DecimalType;
67

78
/**
89
* @group DBAL-461
@@ -31,6 +32,6 @@ public function testIssue()
3132
'comment' => null,
3233
));
3334

34-
$this->assertEquals('Decimal', (string)$column->getType());
35+
$this->assertInstanceOf(DecimalType::class, $column->getType());
3536
}
3637
}

0 commit comments

Comments
 (0)