diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 66da67854..b761685f8 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -11,7 +11,7 @@ ## Optimized -- [#6435](https://github.com/hyperf/hyperf/pull/6435) Optimized model generator which can generate property comments with `use`. +- [#6435](https://github.com/hyperf/hyperf/pull/6435) [#6437](https://github.com/hyperf/hyperf/pull/6437) Optimized model generator which can generate property comments with `use`. # v3.1.4 - 2023-12-29 diff --git a/src/database/src/Commands/Ast/ModelUpdateVisitor.php b/src/database/src/Commands/Ast/ModelUpdateVisitor.php index d176f45cc..c919be2fc 100644 --- a/src/database/src/Commands/Ast/ModelUpdateVisitor.php +++ b/src/database/src/Commands/Ast/ModelUpdateVisitor.php @@ -197,12 +197,17 @@ class ModelUpdateVisitor extends NodeVisitorAbstract } foreach ($this->properties as $name => $property) { $type = $property['type']; - foreach ($type as $i => $item) { - $type[$i] = $this->parsePropertyType($item); + $sorted = []; + foreach ($type as $item) { + if ($item === 'null') { + array_unshift($sorted, $item); + continue; + } + $sorted[] = $this->parsePropertyType($item); } $comment = $property['comment'] ?? ''; - $type = implode('|', $type); + $type = implode('|', $sorted); if ($property['read'] && $property['write']) { $doc .= sprintf(' * @property %s $%s %s', $type, $name, $comment) . PHP_EOL; continue; @@ -221,6 +226,7 @@ class ModelUpdateVisitor extends NodeVisitorAbstract protected function parsePropertyType(string $type): string { + $origin = $type; $isArray = false; if (str_ends_with($type, '[]')) { $isArray = true; @@ -233,9 +239,11 @@ class ModelUpdateVisitor extends NodeVisitorAbstract if ($isArray) { $type .= '[]'; } + + return $type; } - return $type; + return $origin; } protected function initPropertiesFromMethods()