Skip to content

Commit c60fc00

Browse files
committed
Use collection
1 parent aa58027 commit c60fc00

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/Controllers/LaravelRequestDocsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function api(Request $request): JsonResponse
5757

5858
if ($request->input('openapi')) {
5959
return response()->json(
60-
$this->laravelRequestDocsToOpenApi->openApi($docs)->toArray(),
60+
$this->laravelRequestDocsToOpenApi->openApi($docs->all())->toArray(),
6161
Response::HTTP_OK,
6262
[
6363
'Content-type' => 'application/json; charset=utf-8'
@@ -67,7 +67,7 @@ public function api(Request $request): JsonResponse
6767
}
6868

6969
return response()->json(
70-
collect($docs),
70+
$docs,
7171
Response::HTTP_OK,
7272
[
7373
'Content-type' => 'application/json; charset=utf-8',

src/LaravelRequestDocs.php

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LaravelRequestDocs
1919
* @param bool $showPatch
2020
* @param bool $showDelete
2121
* @param bool $showHead
22-
* @return \Rakutentech\LaravelRequestDocs\Doc[]
22+
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
2323
* @throws \ReflectionException
2424
*/
2525
public function getDocs(
@@ -29,7 +29,7 @@ public function getDocs(
2929
bool $showPatch,
3030
bool $showDelete,
3131
bool $showHead
32-
): array {
32+
): Collection {
3333
$filteredMethods = array_filter([
3434
Request::METHOD_GET => $showGet,
3535
Request::METHOD_POST => $showPost,
@@ -45,46 +45,45 @@ public function getDocs(
4545
$docs = $this->getControllersInfo($methods);
4646
$docs = $this->appendRequestRules($docs);
4747

48-
return array_filter($docs);
48+
return $docs->filter();
4949
}
5050

5151
/**
5252
* Split {@see \Rakutentech\LaravelRequestDocs\Doc} by {@see \Rakutentech\LaravelRequestDocs\Doc::$methods}
5353
*
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>
5656
*/
57-
public function splitByMethods(array $docs): array
57+
public function splitByMethods(Collection $docs): Collection
5858
{
59-
/** @var \Rakutentech\LaravelRequestDocs\Doc[] $splitDocs */
60-
$splitDocs = [];
59+
$splitDocs = collect();
60+
/** @var \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $splitDocs */
6161

6262
foreach ($docs as $doc) {
6363
foreach ($doc->getMethods() as $method) {
6464
$cloned = $doc->clone();
6565
$cloned->setMethods([$method]);
6666
$cloned->setHttpMethod($method);
67-
$splitDocs[] = $cloned;
67+
$splitDocs->push($cloned);
6868
}
6969
}
7070

7171
return $splitDocs;
7272
}
7373

7474
/**
75-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
75+
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
7676
* @param string|null $sortBy
77-
* @return \Rakutentech\LaravelRequestDocs\Doc[]
77+
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
7878
*/
79-
public function sortDocs(array $docs, ?string $sortBy = 'default'): array
79+
public function sortDocs(Collection $docs, ?string $sortBy = 'default'): Collection
8080
{
8181
if (!in_array($sortBy, ['route_names', 'method_names'])) {
8282
return $docs;
8383
}
8484

8585
if ($sortBy === 'route_names') {
86-
sort($docs);
87-
return $docs;
86+
return $docs->sort();
8887
}
8988

9089
// Sort by `method_names`.
@@ -97,18 +96,18 @@ public function sortDocs(array $docs, ?string $sortBy = 'default'): array
9796
Request::METHOD_HEAD,
9897
];
9998

100-
$sorted = collect($docs)->sortBy(function (Doc $doc) use ($methods) {
99+
$sorted = $docs->sortBy(function (Doc $doc) use ($methods) {
101100
return array_search($doc->getHttpMethod(), $methods);
102101
}, SORT_NUMERIC);
103102

104-
return $sorted->values()->all();
103+
return $sorted->values();
105104
}
106105

107106
/**
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>
110109
*/
111-
public function groupDocs(array $docs, ?string $groupBy = 'default'): array
110+
public function groupDocs(Collection $docs, ?string $groupBy = 'default'): Collection
112111
{
113112
if (!in_array($groupBy, ['api_uri', 'controller_full_path'])) {
114113
return $docs;
@@ -122,23 +121,23 @@ public function groupDocs(array $docs, ?string $groupBy = 'default'): array
122121
$this->groupDocsByFQController($docs);
123122
}
124123

125-
return collect($docs)
124+
return $docs
126125
->sortBy(function (Doc $doc) {
127126
return $doc->getGroup() . $doc->getGroupIndex();
128127
}, SORT_NATURAL)
129-
->values()
130-
->all();
128+
->values();
131129
}
132130

133131
/**
134132
* @param string[] $onlyMethods
135-
* @return \Rakutentech\LaravelRequestDocs\Doc[]
133+
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
136134
* @throws \ReflectionException
137135
*/
138-
public function getControllersInfo(array $onlyMethods): array
136+
public function getControllersInfo(array $onlyMethods): Collection
139137
{
140-
/** @var \Rakutentech\LaravelRequestDocs\Doc[] $docs */
141-
$docs = [];
138+
$docs = collect();
139+
/** @var \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs */
140+
142141
$routes = Route::getRoutes()->getRoutes();
143142

144143
$onlyRouteStartWith = config('request-docs.only_route_uri_start_with') ?? '';
@@ -185,31 +184,31 @@ public function getControllersInfo(array $onlyMethods): array
185184
'',
186185
);
187186

188-
$docs[] = $doc;
187+
$docs->push($doc);
189188
}
190189

191190
return $docs;
192191
}
193192

194193
/**
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>
197196
* @throws \ReflectionException
198197
*/
199-
public function appendRequestRules(array $controllersInfo): array
198+
public function appendRequestRules(Collection $docs): Collection
200199
{
201-
foreach ($controllersInfo as $controllerInfo) {
202-
if ($controllerInfo->isClosure()) {
200+
foreach ($docs as $doc) {
201+
if ($doc->isClosure()) {
203202
// Skip to next if controller is not exists.
204203
continue;
205204
}
206205

207-
$reflectionMethod = new ReflectionMethod($controllerInfo->getControllerFullPath(), $controllerInfo->getMethod());
206+
$reflectionMethod = new ReflectionMethod($doc->getControllerFullPath(), $doc->getMethod());
208207

209208
$docComment = $this->getDocComment($reflectionMethod);
210209

211210
$customRules = $this->customParamsDocComment($docComment);
212-
$controllerInfo->setResponses($this->customResponsesDocComment($docComment));
211+
$doc->setResponses($this->customResponsesDocComment($docComment));
213212

214213
foreach ($reflectionMethod->getParameters() as $param) {
215214
/** @var \ReflectionNamedType|\ReflectionUnionType|\ReflectionIntersectionType|null $namedType */
@@ -229,21 +228,21 @@ public function appendRequestRules(array $controllersInfo): array
229228
}
230229

231230
try {
232-
$controllerInfo->mergeRules($this->flattenRules($requestObject->$requestMethod()));
231+
$doc->mergeRules($this->flattenRules($requestObject->$requestMethod()));
233232
} catch (Throwable) {
234-
$controllerInfo->mergeRules($this->rulesByRegex($requestClassName, $requestMethod));
233+
$doc->mergeRules($this->rulesByRegex($requestClassName, $requestMethod));
235234
}
236235
}
237236
} catch (Throwable) {
238237
// Do nothing.
239238
}
240239

241-
$controllerInfo->mergeRules($customRules);
240+
$doc->mergeRules($customRules);
242241
}
243242

244-
$controllerInfo->setDocBlock($this->lrdDocComment($docComment));
243+
$doc->setDocBlock($this->lrdDocComment($docComment));
245244
}
246-
return $controllersInfo;
245+
return $docs;
247246
}
248247

249248
public function lrdDocComment(string $docComment): string
@@ -405,16 +404,17 @@ private function multiExplode(array $delimiters, string $string): array
405404
/**
406405
* Parse the `$docs['uri']` and attach `group` and `group_index` details.
407406
*
408-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
407+
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
409408
*/
410-
private function groupDocsByAPIURI(array $docs): void
409+
private function groupDocsByAPIURI(Collection $docs): void
411410
{
412411
$patterns = config('request-docs.group_by.uri_patterns', []);
413412

414413
$regex = count($patterns) > 0 ? '(' . implode('|', $patterns) . ')' : '';
415414

416415
// A collection<string, int> to remember indexes with `group` => `index` pair.
417416
$groupIndexes = collect();
417+
/** @var \Illuminate\Support\Collection<string, int> $groupIndexes */
418418

419419
foreach ($docs as $doc) {
420420
if ($regex !== '') {
@@ -450,12 +450,13 @@ private function getGroupByURI(string $prefix, string $uri): string
450450
/**
451451
* Parse the `$docs['controller_full_path']` and attach `group` and `group_index` details.
452452
*
453-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
453+
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
454454
*/
455-
private function groupDocsByFQController(array $docs): void
455+
private function groupDocsByFQController(Collection $docs): void
456456
{
457457
// To remember group indexes with group => index pair.
458458
$groupIndexes = collect();
459+
/** @var \Illuminate\Support\Collection<string, int> $groupIndexes */
459460

460461
foreach ($docs as $doc) {
461462
$group = $doc->getControllerFullPath();

0 commit comments

Comments
 (0)