Skip to content

Commit d36e962

Browse files
committed
consolidate model builder type, clarify variable names
1 parent a3f3716 commit d36e962

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/support/docblocks.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ interface ClassBlock {
99
blocks: string[];
1010
}
1111

12-
// TODO: Chunk into several files if we have a lot of models?
13-
// TODO: Check if doc block is already in model file, skip if it is
12+
const modelBuilderType = (className: string) =>
13+
`\\Illuminate\\Database\\Eloquent\\Builder<${className}>|${className}`;
14+
1415
export const writeEloquentDocBlocks = (
1516
models: Eloquent.Models,
1617
builderMethods: Eloquent.BuilderMethod[],
@@ -77,25 +78,27 @@ const getBuilderReturnType = (
7778
}
7879

7980
if (["static", "self"].includes(method.return)) {
80-
return `\\Illuminate\\Database\\Eloquent\\Builder<${className}>|${className}`;
81+
return modelBuilderType(className);
8182
}
8283

83-
const certainOfType = ["sole", "find", "first", "firstOrFail"].includes(method.name);
84+
const returnsSingleModel = [
85+
"sole",
86+
"find",
87+
"first",
88+
"firstOrFail",
89+
].includes(method.name);
8490

8591
const returnType = method.return
86-
.replace(
87-
"$this",
88-
`\\Illuminate\\Database\\Eloquent\\Builder<${className}>|${className}`,
89-
)
92+
.replace("$this", modelBuilderType(className))
9093
.replace("\\TReturn", "mixed")
9194
.replace("TReturn", "mixed")
92-
.replace("\\TValue", certainOfType ? className : "mixed")
93-
.replace("TValue", certainOfType ? className : "mixed")
94-
.replace("object", certainOfType ? className : "mixed");
95-
95+
.replace("\\TValue", returnsSingleModel ? className : "mixed")
96+
.replace("TValue", returnsSingleModel ? className : "mixed")
97+
.replace("object", returnsSingleModel ? className : "mixed");
98+
9699
if (returnType.includes("mixed")) {
97100
return "mixed";
98-
};
101+
}
99102

100103
return returnType;
101104
};
@@ -110,7 +113,7 @@ const getBlocks = (
110113
.concat(
111114
[...model.scopes, "newModelQuery", "newQuery", "query"].map(
112115
(method) => {
113-
return `@method static \\Illuminate\\Database\\Eloquent\\Builder<${className}>|${className} ${method}()`;
116+
return `@method static ${modelBuilderType(className)} ${method}()`;
114117
},
115118
),
116119
)
@@ -202,7 +205,7 @@ const getAttributeBlocks = (
202205

203206
if (!["accessor", "attribute"].includes(attr.cast || "")) {
204207
blocks.push(
205-
`@method static \\Illuminate\\Database\\Eloquent\\Builder<${className}>|${className} where${attr.title_case}($value)`,
208+
`@method static ${modelBuilderType(className)} where${attr.title_case}($value)`,
206209
);
207210
}
208211

0 commit comments

Comments
 (0)