Skip to content

Commit

Permalink
Optimized the model generator to generate correct property comments. …
Browse files Browse the repository at this point in the history
…(#5954)


Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
szutoutou and limingxinleo authored Jul 17, 2023
1 parent a966a0d commit 2b61782
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/Commands/Ast/ModelUpdateVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected function initPropertiesFromMethods()
$name = Str::snake(substr($method->getName(), 3, -9));
if (! empty($name)) {
$type = PhpDocReader::getInstance()->getReturnType($method, true);
$this->setProperty($name, $type, true, null, '', false, 1);
$this->setProperty($name, $type, true, false, '', false, 1);
}
continue;
}
Expand All @@ -226,7 +226,7 @@ protected function initPropertiesFromMethods()
// Magic set<name>Attribute
$name = Str::snake(substr($method->getName(), 3, -9));
if (! empty($name)) {
$this->setProperty($name, null, null, true, '', false, 1);
$this->setProperty($name, [], false, true, '', false, 1);
}
continue;
}
Expand Down Expand Up @@ -255,15 +255,15 @@ protected function initPropertiesFromMethods()
if (array_key_exists($name, self::RELATION_METHODS)) {
if ($name === 'morphTo') {
// Model isn't specified because relation is polymorphic
$this->setProperty($method->getName(), ['\\' . Model::class], true);
$this->setProperty($method->getName(), ['\\' . Model::class], true, false, '', true);
} elseif (isset($expr->args[0]) && $expr->args[0]->value instanceof Node\Expr\ClassConstFetch) {
$related = $expr->args[0]->value->class->toCodeString();
if (str_contains($name, 'Many')) {
// Collection or array of models (because Collection is Arrayable)
$this->setProperty($method->getName(), [$this->getCollectionClass($related), $related . '[]'], true);
$this->setProperty($method->getName(), [$this->getCollectionClass($related), $related . '[]'], true, false, '', true);
} else {
// Single model is returned
$this->setProperty($method->getName(), [$related], true);
$this->setProperty($method->getName(), [$related], true, false, '', true);
}
}
}
Expand All @@ -282,7 +282,7 @@ protected function initPropertiesFromMethods()
$method = $ref->getMethod('get');
if ($type = $method->getReturnType()) {
// Get return type which defined in `CastsAttributes::get()`.
$this->setProperty($key, ['\\' . ltrim($type->getName(), '\\')], true, true);
$this->setProperty($key, ['\\' . ltrim($type->getName(), '\\')], true, true, '', true);
}
}
}
Expand All @@ -299,11 +299,11 @@ protected function getMethodRelationName(ReflectionMethod $method): ?string
return null;
}

protected function setProperty(string $name, array $type = null, bool $read = null, bool $write = null, string $comment = '', bool $nullable = false, int $priority = 0)
protected function setProperty(string $name, array $type = [], bool $read = false, bool $write = false, string $comment = '', bool $nullable = false, int $priority = 0)
{
if (! isset($this->properties[$name])) {
$this->properties[$name] = [];
$this->properties[$name]['type'] = 'mixed';
$this->properties[$name]['type'] = implode('|', array_unique($type ?: ['mixed']));
$this->properties[$name]['read'] = false;
$this->properties[$name]['write'] = false;
$this->properties[$name]['comment'] = $comment;
Expand All @@ -312,18 +312,14 @@ protected function setProperty(string $name, array $type = null, bool $read = nu
if ($this->properties[$name]['priority'] > $priority) {
return;
}
if ($type !== null) {
if ($nullable) {
$type[] = 'null';
}
$this->properties[$name]['type'] = implode('|', array_unique($type));
}
if ($read !== null) {
$this->properties[$name]['read'] = $read;
}
if ($write !== null) {
$this->properties[$name]['write'] = $write;

$type = array_merge(explode('|', $this->properties[$name]['type'] ?? []), $type);
if ($nullable) {
$type[] = 'null';
}
$this->properties[$name]['type'] = implode('|', array_unique($type));
$this->properties[$name]['read'] = $this->properties[$name]['read'] || $read;
$this->properties[$name]['write'] = $this->properties[$name]['write'] || $write;
$this->properties[$name]['priority'] = $priority;
}

Expand Down

0 comments on commit 2b61782

Please sign in to comment.