Optimized the type hint to nullable for schemas which generated by cmd. (#5566)

This commit is contained in:
李铭昕 2023-03-24 09:47:11 +08:00 committed by GitHub
parent cbff13f0d8
commit 1276cd5308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -10,6 +10,7 @@
- [#5544](https://github.com/hyperf/hyperf/pull/5554) Cancel `grpc-server`'s dependency on `hyperf/rpc`.
- [#5550](https://github.com/hyperf/hyperf/pull/5550) Optimized code for crontab parser and coordinator timer.
- [#5566](https://github.com/hyperf/hyperf/pull/5566) Optimized the type hint to `nullable` for schemas which generated by `cmd`.
# v3.0.12 - 2023-03-20

View File

@ -70,7 +70,9 @@ class ModelSchemaVisitor extends NodeVisitorAbstract
return new Node\Stmt\ClassMethod(new Node\Identifier('jsonSerialize'), [
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
'returnType' => new Node\Identifier('mixed'),
'stmts' => [new Node\Stmt\Return_(new Node\Expr\Array_($items))],
'stmts' => [new Node\Stmt\Return_(new Node\Expr\Array_($items, [
'kind' => Node\Expr\Array_::KIND_SHORT,
]))],
]);
}
@ -116,7 +118,7 @@ class ModelSchemaVisitor extends NodeVisitorAbstract
new Node\VarLikeIdentifier(Str::camel($column->getName()))
),
],
type: new Node\Identifier(name: $this->formatDatabaseType($column->getType())),
type: new Node\Identifier(name: $this->formatDatabaseType($column->getType(), true)),
attrGroups: [
new Node\AttributeGroup([
new Node\Attribute(new Node\Name('Property'), [
@ -132,12 +134,12 @@ class ModelSchemaVisitor extends NodeVisitorAbstract
return $result;
}
protected function formatDatabaseType(string $type): ?string
protected function formatDatabaseType(string $type, bool $nullable = false): ?string
{
return match ($type) {
'tinyint', 'smallint', 'mediumint', 'int', 'bigint' => 'int',
'bool', 'boolean' => 'bool',
'varchar', 'char' => 'string',
'tinyint', 'smallint', 'mediumint', 'int', 'bigint' => $nullable ? '?int' : 'int',
'bool', 'boolean' => $nullable ? '?bool' : 'bool',
'varchar', 'char' => $nullable ? '?string' : 'string',
default => 'mixed',
};
}