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
67 changes: 65 additions & 2 deletions php-templates/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ 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($this->getScopeParameterInfo(...)),
])
->values()
->toArray();

Expand All @@ -170,6 +173,66 @@ protected function getInfo($className)
$className => $data,
];
}

protected function getScopeParameterInfo(\ReflectionParameter $parameter): array
{
$result = [
"name" => $parameter->getName(),
"type" => $this->typeToString($parameter->getType()),
"hasDefault" => $parameter->isDefaultValueAvailable(),
"isVariadic" => $parameter->isVariadic(),
];

if ($parameter->isDefaultValueAvailable()) {
$result['default'] = $this->defaultToString($parameter->getDefaultValue());
}

return $result;
}

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

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

if ($name !== 'null' && $type->allowsNull()) {
return '?'.$name;
}

return $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 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}'",
};
}
};

$builder = new class($docblocks) {
Expand Down
15 changes: 14 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ declare namespace Eloquent {
relations: Relation[];
events: Event[];
observers: Observer[];
scopes: string[];
scopes: Scope[];
extends: string | null;
}

Expand Down Expand Up @@ -115,4 +115,17 @@ declare namespace Eloquent {
event: string;
observer: string[];
}

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

interface ScopeParameter {
name: string;
type: string;
default?: string | null;
isOptional: boolean;
isVariadic: boolean;
}
}
27 changes: 20 additions & 7 deletions src/support/docblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ 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) => getScopeBlock(className, scope)))
.concat(model.relations.map((relation) => getRelationBlocks(relation)))
.flat()
.map((block) => ` * ${block}`)
Expand Down Expand Up @@ -175,6 +174,20 @@ const getRelationBlocks = (relation: Eloquent.Relation): string[] => {
return [`@property-read \\${relation.related} $${relation.name}`];
};

const getScopeBlock = (className: string, scope: Eloquent.Scope): string => {
const parameters = scope.parameters.slice(1).map((param) => {
return [
param.type,
param.isVariadic ? ` ...$${param.name}` : ` $${param.name}`,
param.default ? ` = ${param.default}` : "",
].join("");
});

return `@method static ${modelBuilderType(
className,
)} ${scope.name}(${parameters})`;
};

const classToDocBlock = (block: ClassBlock, namespace: string) => {
return [
`/**`,
Expand Down
65 changes: 63 additions & 2 deletions src/templates/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,48 @@ $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, '\\\\');
}
if ($name !== 'null' && $type->allowsNull()) {
return '?'.$name;
}
return $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 @@ -159,8 +201,11 @@ $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($this->getScopeParameterInfo(...)),
])
->values()
->toArray();
Expand All @@ -170,6 +215,22 @@ $models = new class($factory) {
$className => $data,
];
}
protected function getScopeParameterInfo(\\ReflectionParameter $parameter): array
{
$result = [
"name" => $parameter->getName(),
"type" => $this->typeToString($parameter->getType()),
"hasDefault" => $parameter->isDefaultValueAvailable(),
"isVariadic" => $parameter->isVariadic(),
];
if ($parameter->isDefaultValueAvailable()) {
$result['default'] = $this->defaultToString($parameter->getDefaultValue());
}
return $result;
}
};
$builder = new class($docblocks) {
Expand Down