@@ -9,8 +9,9 @@ interface ClassBlock {
9
9
blocks : string [ ] ;
10
10
}
11
11
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
+
14
15
export const writeEloquentDocBlocks = (
15
16
models : Eloquent . Models ,
16
17
builderMethods : Eloquent . BuilderMethod [ ] ,
@@ -68,22 +69,35 @@ const getBuilderReturnType = (
68
69
return "mixed" ;
69
70
}
70
71
71
- const returnType = method . return
72
- . replace (
73
- "$this" ,
74
- `\\Illuminate\\Database\\Eloquent\\Builder|${ className } ` ,
75
- )
76
- . replace ( "\\TReturn" , "mixed" )
77
- . replace ( "TReturn" , "mixed" )
78
- . replace ( "\\TValue" , "mixed" )
79
- . replace ( "TValue" , "mixed" ) ;
72
+ if ( method . return === "never" ) {
73
+ return "void" ;
74
+ }
75
+
76
+ if ( [ "when" , "unless" ] . includes ( method . name ) ) {
77
+ return "mixed" ;
78
+ }
80
79
81
80
if ( [ "static" , "self" ] . includes ( method . return ) ) {
82
- return `\\Illuminate\\Database\\Eloquent\\Builder| ${ className } ` ;
81
+ return modelBuilderType ( className ) ;
83
82
}
84
83
85
- if ( method . return === "never" ) {
86
- return "void" ;
84
+ const returnsSingleModel = [
85
+ "sole" ,
86
+ "find" ,
87
+ "first" ,
88
+ "firstOrFail" ,
89
+ ] . includes ( method . name ) ;
90
+
91
+ const returnType = method . return
92
+ . replace ( "$this" , modelBuilderType ( className ) )
93
+ . replace ( "\\TReturn" , "mixed" )
94
+ . replace ( "TReturn" , "mixed" )
95
+ . replace ( "\\TValue" , returnsSingleModel ? className : "mixed" )
96
+ . replace ( "TValue" , returnsSingleModel ? className : "mixed" )
97
+ . replace ( "object" , returnsSingleModel ? className : "mixed" ) ;
98
+
99
+ if ( returnType . includes ( "mixed" ) ) {
100
+ return "mixed" ;
87
101
}
88
102
89
103
return returnType ;
@@ -99,7 +113,7 @@ const getBlocks = (
99
113
. concat (
100
114
[ ...model . scopes , "newModelQuery" , "newQuery" , "query" ] . map (
101
115
( method ) => {
102
- return `@method static \\Illuminate\\Database\\Eloquent\\Builder| ${ className } ${ method } ()` ;
116
+ return `@method static ${ modelBuilderType ( className ) } ${ method } ()` ;
103
117
} ,
104
118
) ,
105
119
)
@@ -191,7 +205,7 @@ const getAttributeBlocks = (
191
205
192
206
if ( ! [ "accessor" , "attribute" ] . includes ( attr . cast || "" ) ) {
193
207
blocks . push (
194
- `@method static \\Illuminate\\Database\\Eloquent\\Builder| ${ className } where${ attr . title_case } ($value)` ,
208
+ `@method static ${ modelBuilderType ( className ) } where${ attr . title_case } ($value)` ,
195
209
) ;
196
210
}
197
211
0 commit comments