Skip to content
Merged
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
85 changes: 83 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,84 @@ 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(),
"isPassedByReference" => $parameter->isPassedByReference(),
];

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

return $result;
}

protected function typeToString(?\ReflectionType $type): string
{
return match (true) {
$type instanceof \ReflectionNamedType => $this->namedTypeToString($type),
$type instanceof \ReflectionUnionType => $this->unionTypeToString($type),
$type instanceof \ReflectionIntersectionType => $this->intersectionTypeToString($type),
default => 'mixed',
};
}

protected function namedTypeToString(\ReflectionNamedType $type): string
{
$name = $type->getName();

if (! $type->isBuiltin() && ! in_array($name, ['self', 'parent', 'static'])) {
$name = '\\'.$name;
}

if ($type->allowsNull() && ! in_array($name, ['null', 'mixed', 'void'])) {
$name = '?'.$name;
}

return $name;
}

protected function unionTypeToString(\ReflectionUnionType $type): string
{
return implode('|', array_map(function (\ReflectionType $type) {
$result = $this->typeToString($type);

if ($type instanceof \ReflectionIntersectionType) {
return "({$result})";
}

return $result;
}, $type->getTypes()));
}

protected function intersectionTypeToString(\ReflectionIntersectionType $type): string
{
return implode('&', array_map($this->typeToString(...), $type->getTypes()));
}

protected function defaultValueToString(\ReflectionParameter $param): string
{
if ($param->isDefaultValueConstant()) {
return '\\'.$param->getDefaultValueConstantName();
}

$value = $param->getDefaultValue();

return match (true) {
is_null($value) => 'null',
is_numeric($value) => $value,
is_bool($value) => $value ? 'true' : 'false',
is_array($value) => '[]',
is_object($value) => 'new \\'.get_class($value),
default => "'{$value}'",
};
}
};

$builder = new class($docblocks) {
Expand Down
16 changes: 15 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,18 @@ 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;
isPassedByReference: boolean;
}
}
32 changes: 25 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,25 @@ 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.isPassedByReference ? "&" : "",
`$${param.name}`,
param.default ? ` = ${param.default}` : "",
].join("");
})
.join(", ");

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

const classToDocBlock = (block: ClassBlock, namespace: string) => {
return [
`/**`,
Expand Down
85 changes: 83 additions & 2 deletions src/templates/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,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 +173,84 @@ $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(),
"isPassedByReference" => $parameter->isPassedByReference(),
];

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

return $result;
}

protected function typeToString(?\\ReflectionType $type): string
{
return match (true) {
$type instanceof \\ReflectionNamedType => $this->namedTypeToString($type),
$type instanceof \\ReflectionUnionType => $this->unionTypeToString($type),
$type instanceof \\ReflectionIntersectionType => $this->intersectionTypeToString($type),
default => 'mixed',
};
}

protected function namedTypeToString(\\ReflectionNamedType $type): string
{
$name = $type->getName();

if (! $type->isBuiltin() && ! in_array($name, ['self', 'parent', 'static'])) {
$name = '\\\\'.$name;
}

if ($type->allowsNull() && ! in_array($name, ['null', 'mixed', 'void'])) {
$name = '?'.$name;
}

return $name;
}

protected function unionTypeToString(\\ReflectionUnionType $type): string
{
return implode('|', array_map(function (\\ReflectionType $type) {
$result = $this->typeToString($type);

if ($type instanceof \\ReflectionIntersectionType) {
return "({$result})";
}

return $result;
}, $type->getTypes()));
}

protected function intersectionTypeToString(\\ReflectionIntersectionType $type): string
{
return implode('&', array_map($this->typeToString(...), $type->getTypes()));
}

protected function defaultValueToString(\\ReflectionParameter $param): string
{
if ($param->isDefaultValueConstant()) {
return '\\\\'.$param->getDefaultValueConstantName();
}

$value = $param->getDefaultValue();

return match (true) {
is_null($value) => 'null',
is_numeric($value) => $value,
is_bool($value) => $value ? 'true' : 'false',
is_array($value) => '[]',
is_object($value) => 'new \\\\'.get_class($value),
default => "'{$value}'",
};
}
};

$builder = new class($docblocks) {
Expand Down