From 48931af43b192808277c3945aa6c172f7fdda2a9 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 8 Aug 2024 16:18:40 +0700 Subject: [PATCH 1/8] Refactor column PHP type --- src/Command/AbstractCommand.php | 6 +- src/Constant/GettypeResult.php | 50 +++++++++++++++ src/Constant/PhpType.php | 50 +++++++++++++++ src/Schema/AbstractSchema.php | 71 ++++++--------------- src/Schema/Column/AbstractColumnSchema.php | 13 ++-- src/Schema/Column/BigIntColumnSchema.php | 20 ++++-- src/Schema/Column/BinaryColumnSchema.php | 12 ++-- src/Schema/Column/BitColumnSchema.php | 9 ++- src/Schema/Column/BooleanColumnSchema.php | 9 ++- src/Schema/Column/ColumnSchemaInterface.php | 27 ++------ src/Schema/Column/DoubleColumnSchema.php | 9 ++- src/Schema/Column/IntegerColumnSchema.php | 9 ++- src/Schema/Column/JsonColumnSchema.php | 3 +- src/Schema/Column/StringColumnSchema.php | 20 ++++-- src/Schema/SchemaInterface.php | 29 --------- tests/Db/Schema/ColumnSchemaTest.php | 13 ---- tests/Db/Schema/SchemaTest.php | 53 +++------------ tests/Provider/ColumnSchemaProvider.php | 18 +++--- tests/Support/Stub/ColumnSchema.php | 3 +- 19 files changed, 217 insertions(+), 207 deletions(-) create mode 100644 src/Constant/GettypeResult.php create mode 100644 src/Constant/PhpType.php diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index ca320ea4e..6f97718a8 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -8,12 +8,12 @@ use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Expression\Expression; +use Yiisoft\Db\Constant\GettypeResult; use Yiisoft\Db\Query\Data\DataReaderInterface; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\DMLQueryBuilderInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; use Yiisoft\Db\Schema\Builder\ColumnInterface; -use Yiisoft\Db\Schema\SchemaInterface; use function explode; use function get_resource_type; @@ -363,8 +363,8 @@ public function getRawSql(): string $params[$name] = match ($param->getType()) { DataType::INTEGER => (string) (int) $value, DataType::STRING, DataType::LOB => match (gettype($value)) { - SchemaInterface::PHP_TYPE_RESOURCE => $name, - SchemaInterface::PHP_TYPE_DOUBLE => (string) $value, + GettypeResult::RESOURCE => $name, + GettypeResult::DOUBLE => (string) $value, default => $value instanceof Expression ? (string) $value : $quoter->quoteValue((string) $value), diff --git a/src/Constant/GettypeResult.php b/src/Constant/GettypeResult.php new file mode 100644 index 000000000..1efd34918 --- /dev/null +++ b/src/Constant/GettypeResult.php @@ -0,0 +1,50 @@ + SQL data type - SchemaInterface::PHP_TYPE_BOOLEAN => DataType::BOOLEAN, - SchemaInterface::PHP_TYPE_INTEGER => DataType::INTEGER, - SchemaInterface::PHP_TYPE_RESOURCE => DataType::LOB, - SchemaInterface::PHP_TYPE_NULL => DataType::NULL, + GettypeResult::BOOLEAN => DataType::BOOLEAN, + GettypeResult::INTEGER => DataType::INTEGER, + GettypeResult::RESOURCE => DataType::LOB, + GettypeResult::NULL => DataType::NULL, default => DataType::STRING, }; } @@ -387,62 +388,32 @@ protected function findTableNames(string $schema): array protected function createColumnSchema(string $type, mixed ...$info): ColumnSchemaInterface { $isUnsigned = !empty($info['unsigned']); - $phpType = $this->getColumnPhpType($type, $isUnsigned); - $column = $this->createColumnSchemaFromPhpType($phpType, $type); + $column = $this->createColumnSchemaFromType($type, $isUnsigned); $column->unsigned($isUnsigned); return $column; } - /** - * Get the PHP type from an abstract database type. - * - * @param string $type The abstract database type. - * - * @return string The PHP type name. - */ - protected function getColumnPhpType(string $type, bool $isUnsigned = false): string + protected function createColumnSchemaFromType(string $type, bool $isUnsigned = false): ColumnSchemaInterface { return match ($type) { - // abstract type => php type - SchemaInterface::TYPE_TINYINT => SchemaInterface::PHP_TYPE_INTEGER, - SchemaInterface::TYPE_SMALLINT => SchemaInterface::PHP_TYPE_INTEGER, + SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type), + SchemaInterface::TYPE_BIT => new BitColumnSchema($type), + SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type), + SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type), SchemaInterface::TYPE_INTEGER => PHP_INT_SIZE !== 8 && $isUnsigned - ? SchemaInterface::PHP_TYPE_STRING - : SchemaInterface::PHP_TYPE_INTEGER, + ? new BigIntColumnSchema($type) + : new IntegerColumnSchema($type), SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8 || $isUnsigned - ? SchemaInterface::PHP_TYPE_STRING - : SchemaInterface::PHP_TYPE_INTEGER, - SchemaInterface::TYPE_BIT => SchemaInterface::PHP_TYPE_INTEGER, - 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, - default => SchemaInterface::PHP_TYPE_STRING, - }; - } - - protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface - { - if ($type === SchemaInterface::TYPE_BIT) { - return new BitColumnSchema($type, $phpType); - } - - return match ($phpType) { - SchemaInterface::PHP_TYPE_STRING => match ($type) { - SchemaInterface::TYPE_INTEGER => new BigIntColumnSchema($type, $phpType), - SchemaInterface::TYPE_BIGINT => new BigIntColumnSchema($type, $phpType), - default => new StringColumnSchema($type, $phpType), - }, - SchemaInterface::PHP_TYPE_INTEGER => new IntegerColumnSchema($type, $phpType), - SchemaInterface::PHP_TYPE_DOUBLE => new DoubleColumnSchema($type, $phpType), - SchemaInterface::PHP_TYPE_BOOLEAN => new BooleanColumnSchema($type, $phpType), - SchemaInterface::PHP_TYPE_RESOURCE => new BinaryColumnSchema($type, $phpType), - SchemaInterface::PHP_TYPE_ARRAY => new JsonColumnSchema($type, $phpType), - default => new StringColumnSchema($type, $phpType), + ? new BigIntColumnSchema($type) + : new IntegerColumnSchema($type), + SchemaInterface::TYPE_DECIMAL => new DoubleColumnSchema($type), + SchemaInterface::TYPE_FLOAT => new DoubleColumnSchema($type), + SchemaInterface::TYPE_DOUBLE => new DoubleColumnSchema($type), + SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type), + SchemaInterface::TYPE_JSON => new JsonColumnSchema($type), + default => new StringColumnSchema($type), }; } diff --git a/src/Schema/Column/AbstractColumnSchema.php b/src/Schema/Column/AbstractColumnSchema.php index db1a7267e..f24f15427 100644 --- a/src/Schema/Column/AbstractColumnSchema.php +++ b/src/Schema/Column/AbstractColumnSchema.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Schema\Column; +use Yiisoft\Db\Constant\PhpType; + /** * Represents the metadata of a column in a database table. * @@ -49,7 +51,6 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface public function __construct( private string $type, - private string|null $phpType = null, ) { } @@ -136,9 +137,9 @@ public function getPrecision(): int|null return $this->precision; } - public function getPhpType(): string|null + public function getPhpType(): string { - return $this->phpType; + return PhpType::MIXED; } public function getScale(): int|null @@ -187,12 +188,6 @@ public function name(string|null $name): static return $this; } - public function phpType(string|null $phpType): static - { - $this->phpType = $phpType; - return $this; - } - public function precision(int|null $precision): static { $this->precision = $precision; diff --git a/src/Schema/Column/BigIntColumnSchema.php b/src/Schema/Column/BigIntColumnSchema.php index 2dc2c341b..2d15473b3 100644 --- a/src/Schema/Column/BigIntColumnSchema.php +++ b/src/Schema/Column/BigIntColumnSchema.php @@ -5,8 +5,12 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\GettypeResult; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\SchemaInterface; +use function gettype; + use const PHP_INT_MAX; use const PHP_INT_MIN; @@ -14,22 +18,21 @@ class BigIntColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_BIGINT, - string|null $phpType = SchemaInterface::PHP_TYPE_STRING, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): int|string|ExpressionInterface|null { return match (gettype($value)) { - 'string' => $value === '' ? null : ( + GettypeResult::STRING => $value === '' ? null : ( $value <= PHP_INT_MAX && $value >= PHP_INT_MIN ? (int) $value : $value ), - 'NULL' => null, - 'integer' => $value, - 'boolean' => $value ? 1 : 0, + GettypeResult::NULL => null, + GettypeResult::INTEGER => $value, + GettypeResult::BOOLEAN => $value ? 1 : 0, default => $value instanceof ExpressionInterface ? $value : ( ($val = (string) $value) <= PHP_INT_MAX && $val >= PHP_INT_MIN ? (int) $val @@ -38,6 +41,11 @@ public function dbTypecast(mixed $value): int|string|ExpressionInterface|null }; } + public function getPhpType(): string + { + return PhpType::STRING; + } + public function phpTypecast(mixed $value): string|null { if ($value === null) { diff --git a/src/Schema/Column/BinaryColumnSchema.php b/src/Schema/Column/BinaryColumnSchema.php index b38eac3a4..361bb8e02 100644 --- a/src/Schema/Column/BinaryColumnSchema.php +++ b/src/Schema/Column/BinaryColumnSchema.php @@ -7,6 +7,7 @@ use PDO; use Yiisoft\Db\Command\Param; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\GettypeResult; use Yiisoft\Db\Schema\SchemaInterface; use function gettype; @@ -15,18 +16,17 @@ class BinaryColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_BINARY, - string|null $phpType = SchemaInterface::PHP_TYPE_RESOURCE, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): mixed { return match (gettype($value)) { - 'string' => new Param($value, PDO::PARAM_LOB), - 'resource' => $value, - 'NULL' => null, - 'boolean' => $value ? '1' : '0', + GettypeResult::STRING => new Param($value, PDO::PARAM_LOB), + GettypeResult::RESOURCE => $value, + GettypeResult::NULL => null, + GettypeResult::BOOLEAN => $value ? '1' : '0', default => $value instanceof ExpressionInterface ? $value : (string) $value, }; } diff --git a/src/Schema/Column/BitColumnSchema.php b/src/Schema/Column/BitColumnSchema.php index bc7a1ff5c..e0958adcb 100644 --- a/src/Schema/Column/BitColumnSchema.php +++ b/src/Schema/Column/BitColumnSchema.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Schema\Column; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -11,9 +12,8 @@ class BitColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_BIT, - string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): int|string|ExpressionInterface|null @@ -28,6 +28,11 @@ public function dbTypecast(mixed $value): int|string|ExpressionInterface|null }; } + public function getPhpType(): string + { + return PhpType::INT; + } + public function phpTypecast(mixed $value): int|null { if ($value === null) { diff --git a/src/Schema/Column/BooleanColumnSchema.php b/src/Schema/Column/BooleanColumnSchema.php index 935f23840..deb44f799 100644 --- a/src/Schema/Column/BooleanColumnSchema.php +++ b/src/Schema/Column/BooleanColumnSchema.php @@ -5,15 +5,15 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\SchemaInterface; class BooleanColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_BOOLEAN, - string|null $phpType = SchemaInterface::PHP_TYPE_BOOLEAN, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): bool|ExpressionInterface|null @@ -26,6 +26,11 @@ public function dbTypecast(mixed $value): bool|ExpressionInterface|null }; } + public function getPhpType(): string + { + return PhpType::BOOL; + } + public function phpTypecast(mixed $value): bool|null { if ($value === null) { diff --git a/src/Schema/Column/ColumnSchemaInterface.php b/src/Schema/Column/ColumnSchemaInterface.php index a1b71fbe7..8ff70d62f 100644 --- a/src/Schema/Column/ColumnSchemaInterface.php +++ b/src/Schema/Column/ColumnSchemaInterface.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Schema\Column; +use Yiisoft\Db\Constant\PhpType; + /** * This interface defines a set of methods that must be implemented by a class that represents the column schema of a * database table column. @@ -179,11 +181,13 @@ public function getName(): string|null; public function getPrecision(): int|null; /** - * @return string|null The PHP type of the column. + * Returns the PHP type of the column. Used for generating Active Record model properties. + * + * @return string The PHP type of the column. * - * @see phpType() + * @see PhpType */ - public function getPhpType(): string|null; + public function getPhpType(): string; /** * @return int|null The scale of the column. @@ -255,23 +259,6 @@ public function isUnsigned(): bool; */ public function name(string|null $name): static; - /** - * The PHP data type for representing the data stored in the column. - * It's determined based on the data type of the column as defined in the database schema. - * For example, if the column is a `varchar` or `text`, the `phpType()` method may return `string`. - * If the column is `int` or `tinyint`, the `phpType()` method may return `integer`. - * - * If set to `null`, the {@see ColumnSchema} will get PHP type automatically based on the - * column type. - * - * ```php - * $columns = [ - * 'description' => $this->text()->phpType('string'), - * ]; - * ``` - */ - public function phpType(string|null $phpType): static; - /** * Converts the input value according to {@see phpType} after retrieval from the database. * diff --git a/src/Schema/Column/DoubleColumnSchema.php b/src/Schema/Column/DoubleColumnSchema.php index 31a9f9563..515bb611f 100644 --- a/src/Schema/Column/DoubleColumnSchema.php +++ b/src/Schema/Column/DoubleColumnSchema.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\SchemaInterface; use function is_float; @@ -13,9 +14,8 @@ class DoubleColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_DOUBLE, - string|null $phpType = SchemaInterface::PHP_TYPE_DOUBLE, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): float|ExpressionInterface|null @@ -30,6 +30,11 @@ public function dbTypecast(mixed $value): float|ExpressionInterface|null }; } + public function getPhpType(): string + { + return PhpType::FLOAT; + } + public function phpTypecast(mixed $value): float|null { if ($value === null) { diff --git a/src/Schema/Column/IntegerColumnSchema.php b/src/Schema/Column/IntegerColumnSchema.php index 19a82c592..47c6930f0 100644 --- a/src/Schema/Column/IntegerColumnSchema.php +++ b/src/Schema/Column/IntegerColumnSchema.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\SchemaInterface; use function is_int; @@ -13,9 +14,8 @@ class IntegerColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_INTEGER, - string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): int|ExpressionInterface|null @@ -30,6 +30,11 @@ public function dbTypecast(mixed $value): int|ExpressionInterface|null }; } + public function getPhpType(): string + { + return PhpType::INT; + } + public function phpTypecast(mixed $value): int|null { if ($value === null) { diff --git a/src/Schema/Column/JsonColumnSchema.php b/src/Schema/Column/JsonColumnSchema.php index 861335f5c..50d1e7239 100644 --- a/src/Schema/Column/JsonColumnSchema.php +++ b/src/Schema/Column/JsonColumnSchema.php @@ -15,9 +15,8 @@ class JsonColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_JSON, - string|null $phpType = SchemaInterface::PHP_TYPE_ARRAY, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): ExpressionInterface|null diff --git a/src/Schema/Column/StringColumnSchema.php b/src/Schema/Column/StringColumnSchema.php index daeac2ced..e11290149 100644 --- a/src/Schema/Column/StringColumnSchema.php +++ b/src/Schema/Column/StringColumnSchema.php @@ -5,6 +5,8 @@ namespace Yiisoft\Db\Schema\Column; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Constant\GettypeResult; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\SchemaInterface; use function gettype; @@ -13,23 +15,29 @@ class StringColumnSchema extends AbstractColumnSchema { public function __construct( string $type = SchemaInterface::TYPE_STRING, - string|null $phpType = SchemaInterface::PHP_TYPE_STRING, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): mixed { return match (gettype($value)) { - 'string', 'resource' => $value, - 'NULL' => null, - 'boolean' => $value ? '1' : '0', + GettypeResult::STRING => $value, + GettypeResult::RESOURCE => $value, + GettypeResult::NULL => null, + GettypeResult::BOOLEAN => $value ? '1' : '0', default => $value instanceof ExpressionInterface ? $value : (string) $value, }; } - public function phpTypecast(mixed $value): mixed + public function getPhpType(): string { + return PhpType::STRING; + } + + public function phpTypecast(mixed $value): string|null + { + /** @var string|null $value */ return $value; } } diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 4a3c6089b..1e784b7ac 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -223,35 +223,6 @@ interface SchemaInterface extends ConstraintSchemaInterface */ public const TYPE_JSON = 'json'; - /** - * Define the php type as `integer` for cast to php value. - */ - public const PHP_TYPE_INTEGER = 'integer'; - /** - * Define the php type as `string` for cast to php value. - */ - public const PHP_TYPE_STRING = 'string'; - /** - * Define the php type as `boolean` for cast to php value. - */ - public const PHP_TYPE_BOOLEAN = 'boolean'; - /** - * Define the php type as `double` for cast to php value. - */ - public const PHP_TYPE_DOUBLE = 'double'; - /** - * Define the php type as `resource` for cast to php value. - */ - public const PHP_TYPE_RESOURCE = 'resource'; - /** - * Define the php type as `array` for cast to php value. - */ - public const PHP_TYPE_ARRAY = 'array'; - /** - * Define the php type as `null` for cast to php value. - */ - public const PHP_TYPE_NULL = 'NULL'; - /** * @psalm-param string[]|int[]|int|string|null $length */ diff --git a/tests/Db/Schema/ColumnSchemaTest.php b/tests/Db/Schema/ColumnSchemaTest.php index 15088df34..d9dc2d0fd 100644 --- a/tests/Db/Schema/ColumnSchemaTest.php +++ b/tests/Db/Schema/ColumnSchemaTest.php @@ -144,19 +144,6 @@ public function testName(): void $this->assertSame('', $column->getName()); } - public function testPhpType(): void - { - $column = new ColumnSchema(); - - $this->assertNull($column->getPhpType()); - $this->assertSame($column, $column->phpType(SchemaInterface::PHP_TYPE_STRING)); - $this->assertSame(SchemaInterface::PHP_TYPE_STRING, $column->getPhpType()); - - $column->phpType(null); - - $this->assertNull($column->getPhpType()); - } - public function testPrecision(): void { $column = new ColumnSchema(); diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 1d3810a63..1547c2b65 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -11,6 +11,8 @@ use Yiisoft\Db\Constraint\ForeignKeyConstraint; use Yiisoft\Db\Constraint\IndexConstraint; use Yiisoft\Db\Exception\NotSupportedException; +use Yiisoft\Db\Schema\Column\IntegerColumnSchema; +use Yiisoft\Db\Schema\Column\StringColumnSchema; use Yiisoft\Db\Schema\TableSchemaInterface; use Yiisoft\Db\Tests\AbstractSchemaTest; use Yiisoft\Db\Tests\Support\Assert; @@ -56,33 +58,6 @@ public function testFindViewNames(): void $this->assertSame([], Assert::invokeMethod($schema, 'findViewNames', ['dbo'])); } - /** - * @throws ReflectionException - */ - public function testGetColumnPhpType(): void - { - $db = $this->getConnection(); - - $schema = $db->getSchema(); - - $this->assertSame( - 'integer', - Assert::invokeMethod($schema, 'getColumnPhpType', ['bigint']), - ); - $this->assertSame( - 'boolean', - Assert::invokeMethod($schema, 'getColumnPhpType', ['boolean']), - ); - $this->assertSame( - 'integer', - Assert::invokeMethod($schema, 'getColumnPhpType', ['integer']), - ); - $this->assertSame( - 'string', - Assert::invokeMethod($schema, 'getColumnPhpType', ['string']), - ); - } - /** * @throws NotSupportedException */ @@ -406,36 +381,26 @@ public function testSetTableMetadata(): void private function createTableSchemaStub(): TableSchemaInterface { // defined column C_id - $columnCid = new ColumnSchema('C_id'); - $columnCid->autoIncrement(true); + $columnCid = new IntegerColumnSchema(); + $columnCid->autoIncrement(); $columnCid->dbType('int'); - $columnCid->primaryKey(true); - $columnCid->phpType('integer'); - $columnCid->type('integer'); + $columnCid->primaryKey(); // defined column C_not_null - $columnCNotNull = new ColumnSchema('C_not_null'); + $columnCNotNull = new IntegerColumnSchema(); $columnCNotNull->dbType('int'); - $columnCNotNull->phpType('int'); - $columnCNotNull->type('int'); // defined column C_check - $columnCCheck = new ColumnSchema('C_check'); + $columnCCheck = new StringColumnSchema(); $columnCCheck->dbType('varchar(255)'); - $columnCCheck->phpType('string'); - $columnCCheck->type('string'); // defined column C_default - $columnCDefault = new ColumnSchema('C_default'); + $columnCDefault = new IntegerColumnSchema(); $columnCDefault->dbType('int'); - $columnCDefault->phpType('integer'); - $columnCDefault->type('integer'); // defined column C_unique - $columnCUnique = new ColumnSchema('C_unique'); + $columnCUnique = new IntegerColumnSchema(); $columnCUnique->dbType('int'); - $columnCUnique->phpType('integer'); - $columnCUnique->type('integer'); // defined table T_constraints_1 $tableSchema = new TableSchema(); diff --git a/tests/Provider/ColumnSchemaProvider.php b/tests/Provider/ColumnSchemaProvider.php index 08f9bb83b..d5c070476 100644 --- a/tests/Provider/ColumnSchemaProvider.php +++ b/tests/Provider/ColumnSchemaProvider.php @@ -9,6 +9,7 @@ use Yiisoft\Db\Command\Param; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; +use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Schema\Column\BigIntColumnSchema; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BitColumnSchema; @@ -27,14 +28,14 @@ public static function predefinedTypes(): array { return [ // [class, type, phpType] - 'integer' => [IntegerColumnSchema::class, SchemaInterface::TYPE_INTEGER, SchemaInterface::PHP_TYPE_INTEGER], - 'bigint' => [BigIntColumnSchema::class, SchemaInterface::TYPE_BIGINT, SchemaInterface::PHP_TYPE_STRING], - 'double' => [DoubleColumnSchema::class, SchemaInterface::TYPE_DOUBLE, SchemaInterface::PHP_TYPE_DOUBLE], - 'string' => [StringColumnSchema::class, SchemaInterface::TYPE_STRING, SchemaInterface::PHP_TYPE_STRING], - 'binary' => [BinaryColumnSchema::class, SchemaInterface::TYPE_BINARY, SchemaInterface::PHP_TYPE_RESOURCE], - 'bit' => [BitColumnSchema::class, SchemaInterface::TYPE_BIT, SchemaInterface::PHP_TYPE_INTEGER], - 'boolean' => [BooleanColumnSchema::class, SchemaInterface::TYPE_BOOLEAN, SchemaInterface::PHP_TYPE_BOOLEAN], - 'json' => [JsonColumnSchema::class, SchemaInterface::TYPE_JSON, SchemaInterface::PHP_TYPE_ARRAY], + 'integer' => [IntegerColumnSchema::class, SchemaInterface::TYPE_INTEGER, PhpType::INT], + 'bigint' => [BigIntColumnSchema::class, SchemaInterface::TYPE_BIGINT, PhpType::STRING], + 'double' => [DoubleColumnSchema::class, SchemaInterface::TYPE_DOUBLE, PhpType::FLOAT], + 'string' => [StringColumnSchema::class, SchemaInterface::TYPE_STRING, PhpType::STRING], + 'binary' => [BinaryColumnSchema::class, SchemaInterface::TYPE_BINARY, PhpType::MIXED], + 'bit' => [BitColumnSchema::class, SchemaInterface::TYPE_BIT, PhpType::INT], + 'boolean' => [BooleanColumnSchema::class, SchemaInterface::TYPE_BOOLEAN, PhpType::BOOL], + 'json' => [JsonColumnSchema::class, SchemaInterface::TYPE_JSON, PhpType::MIXED], ]; } @@ -192,7 +193,6 @@ public static function phpTypecastColumns(): array [null, null], ['', ''], ['string', 'string'], - [$resource = fopen('php://memory', 'rb'), $resource], ], ], 'binary' => [ diff --git a/tests/Support/Stub/ColumnSchema.php b/tests/Support/Stub/ColumnSchema.php index eb11395d6..a01a6b2e7 100644 --- a/tests/Support/Stub/ColumnSchema.php +++ b/tests/Support/Stub/ColumnSchema.php @@ -10,9 +10,8 @@ final class ColumnSchema extends AbstractColumnSchema { public function __construct( private string $type = '', - private string|null $phpType = null, ) { - parent::__construct($type, $phpType); + parent::__construct($type); } public function dbTypecast(mixed $value): mixed From b274a028d91460fd8ded219411f74ba0be9ffd7f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 8 Aug 2024 09:19:06 +0000 Subject: [PATCH 2/8] Apply fixes from StyleCI --- tests/Db/Schema/ColumnSchemaTest.php | 1 - tests/Db/Schema/SchemaTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Db/Schema/ColumnSchemaTest.php b/tests/Db/Schema/ColumnSchemaTest.php index d9dc2d0fd..ad03714d5 100644 --- a/tests/Db/Schema/ColumnSchemaTest.php +++ b/tests/Db/Schema/ColumnSchemaTest.php @@ -5,7 +5,6 @@ namespace Yiisoft\Db\Tests\Db\Schema; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Support\Stub\ColumnSchema; /** diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 1547c2b65..f7c526aa2 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -17,7 +17,6 @@ use Yiisoft\Db\Tests\AbstractSchemaTest; use Yiisoft\Db\Tests\Support\Assert; use Yiisoft\Db\Tests\Support\DbHelper; -use Yiisoft\Db\Tests\Support\Stub\ColumnSchema; use Yiisoft\Db\Tests\Support\Stub\Schema; use Yiisoft\Db\Tests\Support\Stub\TableSchema; use Yiisoft\Db\Tests\Support\TestTrait; From 48979f56972ccf2f8fa34637fdd7e55205b4d352 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 8 Aug 2024 18:32:53 +0700 Subject: [PATCH 3/8] Fix psalm issue --- src/Schema/Column/BigIntColumnSchema.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Schema/Column/BigIntColumnSchema.php b/src/Schema/Column/BigIntColumnSchema.php index 2d15473b3..0497ed54e 100644 --- a/src/Schema/Column/BigIntColumnSchema.php +++ b/src/Schema/Column/BigIntColumnSchema.php @@ -24,6 +24,7 @@ public function __construct( public function dbTypecast(mixed $value): int|string|ExpressionInterface|null { + /** @var int|string|ExpressionInterface|null */ return match (gettype($value)) { GettypeResult::STRING => $value === '' ? null : ( $value <= PHP_INT_MAX && $value >= PHP_INT_MIN From dfb8036ef3b81a4d92efecac9326818fdf9b9c00 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 8 Aug 2024 11:33:09 +0000 Subject: [PATCH 4/8] Apply fixes from StyleCI --- src/Schema/Column/BigIntColumnSchema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Column/BigIntColumnSchema.php b/src/Schema/Column/BigIntColumnSchema.php index 0497ed54e..4a267c48a 100644 --- a/src/Schema/Column/BigIntColumnSchema.php +++ b/src/Schema/Column/BigIntColumnSchema.php @@ -24,7 +24,7 @@ public function __construct( public function dbTypecast(mixed $value): int|string|ExpressionInterface|null { - /** @var int|string|ExpressionInterface|null */ + /** @var ExpressionInterface|int|string|null */ return match (gettype($value)) { GettypeResult::STRING => $value === '' ? null : ( $value <= PHP_INT_MAX && $value >= PHP_INT_MIN From da871f884a33563135d00e83dadf7b9b8777ee3c Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 9 Aug 2024 13:42:13 +0700 Subject: [PATCH 5/8] Add Add lines to CHANGELOG.md, update UPGRADE.md, update PHPdoc [skip ci] --- CHANGELOG.md | 3 ++- UPGRADE.md | 17 ++++++++++++++++- src/Constant/PhpType.php | 2 +- src/Schema/Column/ColumnSchemaInterface.php | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f35dc0ef..88f231001 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,8 @@ - Chg #847: Remove `SchemaInterface::getRawTableName()` and `AbstractSchema::getRawTableName()` methods (@Tigrov) - Enh #852: Add method chaining for column classes (@Tigrov) - Enh #855: Add array and JSON overlaps conditions (@Tigrov) -- Enh #860 Add `bit` abstract type (@Tigrov) +- Enh #860: Add `bit` abstract type (@Tigrov) +- Enh #862: Refactor PHP type of `SchemaInterface` instances (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index 50f5802ba..d8767e8ae 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -65,15 +65,22 @@ The interface and the abstract implementation `AbstractColumnSchema` were moved and the following changes were made: - `getName()` method can return `string` or `null`; +- `getPhpType()` method must return `string` PHP type of the column which used for generating related model properties; - `name(string|null $name)` method is added; - constructor of `AbstractColumnSchema` class is changed to `__construct(string $type, string|null $phpType = null)`; - added method chaining. +### New classes with constants + +- `Yiisoft\Db\Constant\PhpType` with PHP types constants; +- `Yiisoft\Db\Constant\GettypeResult` with `gettype()` function results constants. + ### New classes for table columns Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace according to the data type: - `BooleanColumnSchema` for columns with boolean type; +- `BitColumnSchema` for columns with bit type; - `IntegerColumnSchema` for columns with integer type (tinyint, smallint, integer, bigint); - `BigIntColumnSchema` for columns with integer type with range outside `PHP_INT_MIN` and `PHP_INT_MAX`; - `DoubleColumnSchema` for columns with fractional number type (float, double, decimal, money); @@ -96,6 +103,7 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace - `AbstractSchema::normalizeRowKeyCase()` - `Quoter::unquoteParts()` - `AbstractPdoCommand::logQuery()` +- `ColumnSchemaInterface::phpType()` ### Remove deprecated parameters @@ -105,9 +113,16 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace - `$rawSql` from `AbstractCommand::internalExecute()` method - `$rawSql` from `AbstractPdoCommand::internalExecute()` method -### Remove deprecated constants +### Remove constants - `SchemaInterface::TYPE_JSONB` +- `SchemaInterface::PHP_TYPE_INTEGER` +- `SchemaInterface::PHP_TYPE_STRING` +- `SchemaInterface::PHP_TYPE_BOOLEAN` +- `SchemaInterface::PHP_TYPE_DOUBLE` +- `SchemaInterface::PHP_TYPE_RESOURCE` +- `SchemaInterface::PHP_TYPE_ARRAY` +- `SchemaInterface::PHP_TYPE_NULL` ### Other changes diff --git a/src/Constant/PhpType.php b/src/Constant/PhpType.php index f2596d4e0..560da3725 100644 --- a/src/Constant/PhpType.php +++ b/src/Constant/PhpType.php @@ -8,7 +8,7 @@ /** * Defines the available PHP types. - * Used to generate properties of Active Record model or other user defined models. + * Used to generate properties of a related model class. * * @see ColumnSchemaInterface::getPhpType() * @see https://www.php.net/manual/en/language.types.type-system.php diff --git a/src/Schema/Column/ColumnSchemaInterface.php b/src/Schema/Column/ColumnSchemaInterface.php index 8ff70d62f..8a98cfd64 100644 --- a/src/Schema/Column/ColumnSchemaInterface.php +++ b/src/Schema/Column/ColumnSchemaInterface.php @@ -181,7 +181,7 @@ public function getName(): string|null; public function getPrecision(): int|null; /** - * Returns the PHP type of the column. Used for generating Active Record model properties. + * Returns the PHP type of the column. Used for generating properties of a related model class. * * @return string The PHP type of the column. * From f43c39c74552054c44b6d4ddae0aeb2dc4c9371d Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 9 Aug 2024 16:51:59 +0700 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Sergei Predvoditelev --- src/Constant/GettypeResult.php | 2 +- src/Constant/PhpType.php | 2 +- src/Schema/Column/ColumnSchemaInterface.php | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Constant/GettypeResult.php b/src/Constant/GettypeResult.php index 1efd34918..ad5ec397c 100644 --- a/src/Constant/GettypeResult.php +++ b/src/Constant/GettypeResult.php @@ -9,7 +9,7 @@ * * @link https://www.php.net/manual/en/function.gettype.php */ -class GettypeResult +final class GettypeResult { /** * Define the php type as `array`. diff --git a/src/Constant/PhpType.php b/src/Constant/PhpType.php index 560da3725..3825d2c7a 100644 --- a/src/Constant/PhpType.php +++ b/src/Constant/PhpType.php @@ -13,7 +13,7 @@ * @see ColumnSchemaInterface::getPhpType() * @see https://www.php.net/manual/en/language.types.type-system.php */ -class PhpType +final class PhpType { /** * Define the php type as `array`. diff --git a/src/Schema/Column/ColumnSchemaInterface.php b/src/Schema/Column/ColumnSchemaInterface.php index 8a98cfd64..b1d9e2af5 100644 --- a/src/Schema/Column/ColumnSchemaInterface.php +++ b/src/Schema/Column/ColumnSchemaInterface.php @@ -184,6 +184,7 @@ public function getPrecision(): int|null; * Returns the PHP type of the column. Used for generating properties of a related model class. * * @return string The PHP type of the column. + * @psalm-return PhpType::* * * @see PhpType */ From 9dd4b81c6ba5d9c953c771bbc3643a8e78672714 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 9 Aug 2024 09:52:11 +0000 Subject: [PATCH 7/8] Apply fixes from StyleCI --- src/Schema/Column/ColumnSchemaInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Column/ColumnSchemaInterface.php b/src/Schema/Column/ColumnSchemaInterface.php index b1d9e2af5..fddf4d117 100644 --- a/src/Schema/Column/ColumnSchemaInterface.php +++ b/src/Schema/Column/ColumnSchemaInterface.php @@ -184,7 +184,7 @@ public function getPrecision(): int|null; * Returns the PHP type of the column. Used for generating properties of a related model class. * * @return string The PHP type of the column. - * @psalm-return PhpType::* + * @psalm-return PhpType::* * * @see PhpType */ From d6af9b6b8c1c47e7c048fb1b3fa84668df5b6881 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 9 Aug 2024 16:57:23 +0700 Subject: [PATCH 8/8] Update line of CHANGELOG.md [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88f231001..d391a4164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ - Enh #852: Add method chaining for column classes (@Tigrov) - Enh #855: Add array and JSON overlaps conditions (@Tigrov) - Enh #860: Add `bit` abstract type (@Tigrov) -- Enh #862: Refactor PHP type of `SchemaInterface` instances (@Tigrov) +- Enh #862: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov) ## 1.3.0 March 21, 2024