From 1276cd53087a3a81eef77fb8d2f217d2a4778773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 24 Mar 2023 09:47:11 +0800 Subject: [PATCH] Optimized the type hint to `nullable` for schemas which generated by `cmd`. (#5566) --- CHANGELOG-3.0.md | 1 + src/swagger/src/Command/Ast/ModelSchemaVisitor.php | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index 57fcbab34..54c5a7006 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -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 diff --git a/src/swagger/src/Command/Ast/ModelSchemaVisitor.php b/src/swagger/src/Command/Ast/ModelSchemaVisitor.php index 05c519ac3..9ceda89ae 100644 --- a/src/swagger/src/Command/Ast/ModelSchemaVisitor.php +++ b/src/swagger/src/Command/Ast/ModelSchemaVisitor.php @@ -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', }; }