mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Optimized the type hint to nullable
for schemas which generated by cmd
. (#5566)
This commit is contained in:
parent
cbff13f0d8
commit
1276cd5308
@ -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
|
||||
|
||||
|
@ -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',
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user