Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions php-templates/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ protected function getParentClass(\ReflectionClass $reflection)
return \Illuminate\Support\Str::start($parent, '\\');
}

protected function defaultToString(mixed $value): string {
return match (true) {
is_null($value) => 'null',
is_numeric($value) => $value,
is_bool($value) => $value ? 'true' : 'false',
is_array($value) => '[...]',
is_object($value) && enum_exists(get_class($value)) => '\\' . get_class($value) . '::' . $value->name,
is_object($value) => '\\' . get_class($value),
default => "'{$value}'",
};
}

protected function typeToString(?ReflectionType $type): string {
if ($type instanceof ReflectionNamedType) {
$name = $type->getName();

if (!$type->isBuiltin()) {
$name = '\\' . ltrim($name, '\\');
}

return ($type->allowsNull() ? '?' : '') . $name;
}

if ($type instanceof ReflectionUnionType) {
$types = array_map(fn(ReflectionNamedType|ReflectionIntersectionType $t) => $this->typeToString($t), $type->getTypes());

return implode('|', $types);
}

if ($type instanceof ReflectionIntersectionType) {
$types = array_map(fn(ReflectionNamedType|ReflectionIntersectionType $t) => $this->typeToString($t), $type->getTypes());

return implode('&', $types);
}

return 'mixed';
}

protected function getInfo($className)
{
if (($data = $this->fromArtisan($className)) === null) {
Expand All @@ -155,8 +193,17 @@ protected function getInfo($className)
->toArray();

$data['scopes'] = collect($reflection->getMethods())
->filter(fn($method) =>!$method->isStatic() && ($method->getAttributes(\Illuminate\Database\Eloquent\Attributes\Scope::class) || ($method->isPublic() && str_starts_with($method->name, 'scope'))))
->map(fn($method) => str($method->name)->replace('scope', '')->lcfirst()->toString())
->filter(fn(\ReflectionMethod $method) => !$method->isStatic() && ($method->getAttributes(\Illuminate\Database\Eloquent\Attributes\Scope::class) || ($method->isPublic() && str_starts_with($method->name, 'scope'))))
->map(fn(\ReflectionMethod $method) => [
"name" => str($method->name)->replace('scope', '')->lcfirst()->toString(),
"parameters" => collect($method->getParameters())->map(fn(\ReflectionParameter $param) => [
"name" => $param->getName(),
"type" => $this->typeToString($param->getType()),
"hasDefault" => $param->isDefaultValueAvailable(),
"default" => $param->isDefaultValueAvailable() ? $this->defaultToString($param->getDefaultValue()) : null,
"isOptional" => $param->isOptional()
]),
])
->values()
->toArray();

Expand Down
15 changes: 14 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ declare namespace Eloquent {
[key: string]: Model;
}

interface ScopeParameter {
name: string;
type: string | null;
hasDefault: boolean;
default: string | null;
isOptional: boolean;
}

interface Scope {
name: string;
parameters: ScopeParameter[];
}

interface Model {
class: string;
database: string;
Expand All @@ -81,7 +94,7 @@ declare namespace Eloquent {
relations: Relation[];
events: Event[];
observers: Observer[];
scopes: string[];
scopes: Scope[];
extends: string | null;
}

Expand Down
42 changes: 35 additions & 7 deletions src/support/docblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,41 @@ const getBlocks = (
return model.attributes
.map((attr) => getAttributeBlocks(attr, className))
.concat(
[...model.scopes, "newModelQuery", "newQuery", "query"].map(
(method) => {
return `@method static ${modelBuilderType(
className,
)} ${method}()`;
},
),
["newModelQuery", "newQuery", "query"].map((method) => {
return `@method static ${modelBuilderType(
className,
)} ${method}()`;
}),
)
.concat(
model.scopes.map((scope) => {
const parameters = scope.parameters
.slice(1)
.map((param) => {
let paramAsString = "";

if (param.type !== null) {
paramAsString += `${param.type} `;
}

if (param.isOptional && !param.hasDefault) {
paramAsString += "...";
}

paramAsString += `$${param.name}`;

if (param.hasDefault) {
paramAsString += ` = ${param.default}`;
}

return paramAsString;
})
.join(", ");

return `@method static ${modelBuilderType(
className,
)} ${scope.name}(${parameters})`;
}),
)
.concat(model.relations.map((relation) => getRelationBlocks(relation)))
.flat()
Expand Down
51 changes: 49 additions & 2 deletions src/templates/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ $models = new class($factory) {
return \\Illuminate\\Support\\Str::start($parent, '\\\\');
}

protected function defaultToString(mixed $value): string {
return match (true) {
is_null($value) => 'null',
is_numeric($value) => $value,
is_bool($value) => $value ? 'true' : 'false',
is_array($value) => '[...]',
is_object($value) && enum_exists(get_class($value)) => '\\\\' . get_class($value) . '::' . $value->name,
is_object($value) => '\\\\' . get_class($value),
default => "'{$value}'",
};
}

protected function typeToString(?ReflectionType $type): string {
if ($type instanceof ReflectionNamedType) {
$name = $type->getName();

if (!$type->isBuiltin()) {
$name = '\\\\' . ltrim($name, '\\\\');
}

return ($type->allowsNull() ? '?' : '') . $name;
}

if ($type instanceof ReflectionUnionType) {
$types = array_map(fn(ReflectionNamedType|ReflectionIntersectionType $t) => $this->typeToString($t), $type->getTypes());

return implode('|', $types);
}

if ($type instanceof ReflectionIntersectionType) {
$types = array_map(fn(ReflectionNamedType|ReflectionIntersectionType $t) => $this->typeToString($t), $type->getTypes());

return implode('&', $types);
}

return 'mixed';
}

protected function getInfo($className)
{
if (($data = $this->fromArtisan($className)) === null) {
Expand All @@ -155,8 +193,17 @@ $models = new class($factory) {
->toArray();

$data['scopes'] = collect($reflection->getMethods())
->filter(fn($method) =>!$method->isStatic() && ($method->getAttributes(\\Illuminate\\Database\\Eloquent\\Attributes\\Scope::class) || ($method->isPublic() && str_starts_with($method->name, 'scope'))))
->map(fn($method) => str($method->name)->replace('scope', '')->lcfirst()->toString())
->filter(fn(\\ReflectionMethod $method) => !$method->isStatic() && ($method->getAttributes(\\Illuminate\\Database\\Eloquent\\Attributes\\Scope::class) || ($method->isPublic() && str_starts_with($method->name, 'scope'))))
->map(fn(\\ReflectionMethod $method) => [
"name" => str($method->name)->replace('scope', '')->lcfirst()->toString(),
"parameters" => collect($method->getParameters())->map(fn(\\ReflectionParameter $param) => [
"name" => $param->getName(),
"type" => $this->typeToString($param->getType()),
"hasDefault" => $param->isDefaultValueAvailable(),
"default" => $param->isDefaultValueAvailable() ? $this->defaultToString($param->getDefaultValue()) : null,
"isOptional" => $param->isOptional()
]),
])
->values()
->toArray();

Expand Down