Skip to content

Commit

Permalink
Fix #728: Refactor AbstractSchema::getColumnPhpType()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jul 24, 2023
1 parent 13ddab5 commit 669fca4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -3,7 +3,8 @@
## 1.0.1 under development

- Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw)
- Chg #724: Typecast refactoring. (@Tigrov)
- Chg #724: Typecast refactoring (@Tigrov)
- Chg #728: Refactor `AbstractSchema::getColumnPhpType()` (@Tigrov)

## 1.0.0 April 12, 2023

Expand Down
30 changes: 9 additions & 21 deletions src/Schema/AbstractSchema.php
Expand Up @@ -402,36 +402,24 @@ protected function findTableNames(string $schema): array
*/
protected function getColumnPhpType(ColumnSchemaInterface $column): string
{
/** @psalm-var string[] $typeMap */
$typeMap = [
return match ($column->getType()) {
// abstract type => php type
SchemaInterface::TYPE_TINYINT => SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_SMALLINT => SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_INTEGER => SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_BIGINT => SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_INTEGER => PHP_INT_SIZE === 4 && $column->isUnsigned()
? SchemaInterface::PHP_TYPE_STRING
: SchemaInterface::PHP_TYPE_INTEGER,
SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE === 8 && !$column->isUnsigned()
? SchemaInterface::PHP_TYPE_INTEGER
: SchemaInterface::PHP_TYPE_STRING,
SchemaInterface::TYPE_BOOLEAN => SchemaInterface::PHP_TYPE_BOOLEAN,
SchemaInterface::TYPE_DECIMAL => SchemaInterface::PHP_TYPE_DOUBLE,
SchemaInterface::TYPE_FLOAT => SchemaInterface::PHP_TYPE_DOUBLE,
SchemaInterface::TYPE_DOUBLE => SchemaInterface::PHP_TYPE_DOUBLE,
SchemaInterface::TYPE_BINARY => SchemaInterface::PHP_TYPE_RESOURCE,
SchemaInterface::TYPE_JSON => SchemaInterface::PHP_TYPE_ARRAY,
];

if (isset($typeMap[$column->getType()])) {
if ($column->getType() === SchemaInterface::TYPE_BIGINT) {
return PHP_INT_SIZE === 8 && !$column->isUnsigned()
? SchemaInterface::PHP_TYPE_INTEGER : SchemaInterface::PHP_TYPE_STRING;
}

if ($column->getType() === SchemaInterface::TYPE_INTEGER) {
return PHP_INT_SIZE === 4 && $column->isUnsigned()
? SchemaInterface::PHP_TYPE_STRING : SchemaInterface::PHP_TYPE_INTEGER;
}

return $typeMap[$column->getType()];
}

return SchemaInterface::PHP_TYPE_STRING;
default => SchemaInterface::PHP_TYPE_STRING,
};
}

/**
Expand Down

0 comments on commit 669fca4

Please sign in to comment.