@@ -19,7 +19,7 @@ class LaravelRequestDocs
19
19
* @param bool $showPatch
20
20
* @param bool $showDelete
21
21
* @param bool $showHead
22
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
22
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
23
23
* @throws \ReflectionException
24
24
*/
25
25
public function getDocs (
@@ -29,7 +29,7 @@ public function getDocs(
29
29
bool $ showPatch ,
30
30
bool $ showDelete ,
31
31
bool $ showHead
32
- ): array {
32
+ ): Collection {
33
33
$ filteredMethods = array_filter ([
34
34
Request::METHOD_GET => $ showGet ,
35
35
Request::METHOD_POST => $ showPost ,
@@ -45,46 +45,45 @@ public function getDocs(
45
45
$ docs = $ this ->getControllersInfo ($ methods );
46
46
$ docs = $ this ->appendRequestRules ($ docs );
47
47
48
- return array_filter ( $ docs );
48
+ return $ docs-> filter ( );
49
49
}
50
50
51
51
/**
52
52
* Split {@see \Rakutentech\LaravelRequestDocs\Doc} by {@see \Rakutentech\LaravelRequestDocs\Doc::$methods}
53
53
*
54
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
55
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
54
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
55
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
56
56
*/
57
- public function splitByMethods (array $ docs ): array
57
+ public function splitByMethods (Collection $ docs ): Collection
58
58
{
59
- /** @var \Rakutentech\LaravelRequestDocs\Doc[] $splitDocs */
60
- $ splitDocs = [];
59
+ $ splitDocs = collect ();
60
+ /** @var \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $splitDocs */
61
61
62
62
foreach ($ docs as $ doc ) {
63
63
foreach ($ doc ->getMethods () as $ method ) {
64
64
$ cloned = $ doc ->clone ();
65
65
$ cloned ->setMethods ([$ method ]);
66
66
$ cloned ->setHttpMethod ($ method );
67
- $ splitDocs[] = $ cloned ;
67
+ $ splitDocs-> push ( $ cloned) ;
68
68
}
69
69
}
70
70
71
71
return $ splitDocs ;
72
72
}
73
73
74
74
/**
75
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
75
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
76
76
* @param string|null $sortBy
77
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
77
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
78
78
*/
79
- public function sortDocs (array $ docs , ?string $ sortBy = 'default ' ): array
79
+ public function sortDocs (Collection $ docs , ?string $ sortBy = 'default ' ): Collection
80
80
{
81
81
if (!in_array ($ sortBy , ['route_names ' , 'method_names ' ])) {
82
82
return $ docs ;
83
83
}
84
84
85
85
if ($ sortBy === 'route_names ' ) {
86
- sort ($ docs );
87
- return $ docs ;
86
+ return $ docs ->sort ();
88
87
}
89
88
90
89
// Sort by `method_names`.
@@ -97,18 +96,18 @@ public function sortDocs(array $docs, ?string $sortBy = 'default'): array
97
96
Request::METHOD_HEAD ,
98
97
];
99
98
100
- $ sorted = collect ( $ docs) ->sortBy (function (Doc $ doc ) use ($ methods ) {
99
+ $ sorted = $ docs ->sortBy (function (Doc $ doc ) use ($ methods ) {
101
100
return array_search ($ doc ->getHttpMethod (), $ methods );
102
101
}, SORT_NUMERIC );
103
102
104
- return $ sorted ->values ()-> all () ;
103
+ return $ sorted ->values ();
105
104
}
106
105
107
106
/**
108
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
109
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
107
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
108
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
110
109
*/
111
- public function groupDocs (array $ docs , ?string $ groupBy = 'default ' ): array
110
+ public function groupDocs (Collection $ docs , ?string $ groupBy = 'default ' ): Collection
112
111
{
113
112
if (!in_array ($ groupBy , ['api_uri ' , 'controller_full_path ' ])) {
114
113
return $ docs ;
@@ -122,23 +121,23 @@ public function groupDocs(array $docs, ?string $groupBy = 'default'): array
122
121
$ this ->groupDocsByFQController ($ docs );
123
122
}
124
123
125
- return collect ( $ docs)
124
+ return $ docs
126
125
->sortBy (function (Doc $ doc ) {
127
126
return $ doc ->getGroup () . $ doc ->getGroupIndex ();
128
127
}, SORT_NATURAL )
129
- ->values ()
130
- ->all ();
128
+ ->values ();
131
129
}
132
130
133
131
/**
134
132
* @param string[] $onlyMethods
135
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
133
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
136
134
* @throws \ReflectionException
137
135
*/
138
- public function getControllersInfo (array $ onlyMethods ): array
136
+ public function getControllersInfo (array $ onlyMethods ): Collection
139
137
{
140
- /** @var \Rakutentech\LaravelRequestDocs\Doc[] $docs */
141
- $ docs = [];
138
+ $ docs = collect ();
139
+ /** @var \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs */
140
+
142
141
$ routes = Route::getRoutes ()->getRoutes ();
143
142
144
143
$ onlyRouteStartWith = config ('request-docs.only_route_uri_start_with ' ) ?? '' ;
@@ -185,31 +184,31 @@ public function getControllersInfo(array $onlyMethods): array
185
184
'' ,
186
185
);
187
186
188
- $ docs[] = $ doc ;
187
+ $ docs-> push ( $ doc) ;
189
188
}
190
189
191
190
return $ docs ;
192
191
}
193
192
194
193
/**
195
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $controllersInfo
196
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
194
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
195
+ * @return \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc>
197
196
* @throws \ReflectionException
198
197
*/
199
- public function appendRequestRules (array $ controllersInfo ): array
198
+ public function appendRequestRules (Collection $ docs ): Collection
200
199
{
201
- foreach ($ controllersInfo as $ controllerInfo ) {
202
- if ($ controllerInfo ->isClosure ()) {
200
+ foreach ($ docs as $ doc ) {
201
+ if ($ doc ->isClosure ()) {
203
202
// Skip to next if controller is not exists.
204
203
continue ;
205
204
}
206
205
207
- $ reflectionMethod = new ReflectionMethod ($ controllerInfo ->getControllerFullPath (), $ controllerInfo ->getMethod ());
206
+ $ reflectionMethod = new ReflectionMethod ($ doc ->getControllerFullPath (), $ doc ->getMethod ());
208
207
209
208
$ docComment = $ this ->getDocComment ($ reflectionMethod );
210
209
211
210
$ customRules = $ this ->customParamsDocComment ($ docComment );
212
- $ controllerInfo ->setResponses ($ this ->customResponsesDocComment ($ docComment ));
211
+ $ doc ->setResponses ($ this ->customResponsesDocComment ($ docComment ));
213
212
214
213
foreach ($ reflectionMethod ->getParameters () as $ param ) {
215
214
/** @var \ReflectionNamedType|\ReflectionUnionType|\ReflectionIntersectionType|null $namedType */
@@ -229,21 +228,21 @@ public function appendRequestRules(array $controllersInfo): array
229
228
}
230
229
231
230
try {
232
- $ controllerInfo ->mergeRules ($ this ->flattenRules ($ requestObject ->$ requestMethod ()));
231
+ $ doc ->mergeRules ($ this ->flattenRules ($ requestObject ->$ requestMethod ()));
233
232
} catch (Throwable ) {
234
- $ controllerInfo ->mergeRules ($ this ->rulesByRegex ($ requestClassName , $ requestMethod ));
233
+ $ doc ->mergeRules ($ this ->rulesByRegex ($ requestClassName , $ requestMethod ));
235
234
}
236
235
}
237
236
} catch (Throwable ) {
238
237
// Do nothing.
239
238
}
240
239
241
- $ controllerInfo ->mergeRules ($ customRules );
240
+ $ doc ->mergeRules ($ customRules );
242
241
}
243
242
244
- $ controllerInfo ->setDocBlock ($ this ->lrdDocComment ($ docComment ));
243
+ $ doc ->setDocBlock ($ this ->lrdDocComment ($ docComment ));
245
244
}
246
- return $ controllersInfo ;
245
+ return $ docs ;
247
246
}
248
247
249
248
public function lrdDocComment (string $ docComment ): string
@@ -405,16 +404,17 @@ private function multiExplode(array $delimiters, string $string): array
405
404
/**
406
405
* Parse the `$docs['uri']` and attach `group` and `group_index` details.
407
406
*
408
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
407
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
409
408
*/
410
- private function groupDocsByAPIURI (array $ docs ): void
409
+ private function groupDocsByAPIURI (Collection $ docs ): void
411
410
{
412
411
$ patterns = config ('request-docs.group_by.uri_patterns ' , []);
413
412
414
413
$ regex = count ($ patterns ) > 0 ? '( ' . implode ('| ' , $ patterns ) . ') ' : '' ;
415
414
416
415
// A collection<string, int> to remember indexes with `group` => `index` pair.
417
416
$ groupIndexes = collect ();
417
+ /** @var \Illuminate\Support\Collection<string, int> $groupIndexes */
418
418
419
419
foreach ($ docs as $ doc ) {
420
420
if ($ regex !== '' ) {
@@ -450,12 +450,13 @@ private function getGroupByURI(string $prefix, string $uri): string
450
450
/**
451
451
* Parse the `$docs['controller_full_path']` and attach `group` and `group_index` details.
452
452
*
453
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
453
+ * @param \Illuminate\Support\Collection<int, \ Rakutentech\LaravelRequestDocs\Doc> $docs
454
454
*/
455
- private function groupDocsByFQController (array $ docs ): void
455
+ private function groupDocsByFQController (Collection $ docs ): void
456
456
{
457
457
// To remember group indexes with group => index pair.
458
458
$ groupIndexes = collect ();
459
+ /** @var \Illuminate\Support\Collection<string, int> $groupIndexes */
459
460
460
461
foreach ($ docs as $ doc ) {
461
462
$ group = $ doc ->getControllerFullPath ();
0 commit comments