Skip to content

Commit b0694ea

Browse files
committed
Add code comments
1 parent c60fc00 commit b0694ea

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Controllers/LaravelRequestDocsController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function api(Request $request): JsonResponse
4242
$showDelete = !$request->has('showDelete') || $request->input('showDelete') === 'true';
4343
$showHead = !$request->has('showHead') || $request->input('showHead') === 'true';
4444

45+
// Get a list of Doc with route and rules information.
46+
// If user defined `Route::match(['get', 'post'], 'uri', ...)`,
47+
// only a single Doc will be generated.
4548
$docs = $this->laravelRequestDocs->getDocs(
4649
$showGet,
4750
$showPost,
@@ -51,6 +54,8 @@ public function api(Request $request): JsonResponse
5154
$showHead,
5255
);
5356

57+
// Loop and split Doc by the `methods` property.
58+
// `Route::match([...n], 'uri', ...)` will generate n number of Doc.
5459
$docs = $this->laravelRequestDocs->splitByMethods($docs);
5560
$docs = $this->laravelRequestDocs->sortDocs($docs, $request->input('sort'));
5661
$docs = $this->laravelRequestDocs->groupDocs($docs, $request->input('groupby'));

src/LaravelRequestDocs.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class LaravelRequestDocs
1414
{
1515
/**
16+
* Get a collection of {@see \Rakutentech\LaravelRequestDocs\Doc} with route and rules information.
17+
*
1618
* @param bool $showGet
1719
* @param bool $showPost
1820
* @param bool $showPut
@@ -49,7 +51,7 @@ public function getDocs(
4951
}
5052

5153
/**
52-
* Split {@see \Rakutentech\LaravelRequestDocs\Doc} by {@see \Rakutentech\LaravelRequestDocs\Doc::$methods}
54+
* Loop and split {@see \Rakutentech\LaravelRequestDocs\Doc} by {@see \Rakutentech\LaravelRequestDocs\Doc::$methods}.
5355
*
5456
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
5557
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
@@ -72,6 +74,8 @@ public function splitByMethods(Collection $docs): Collection
7274
}
7375

7476
/**
77+
* Sort by `$sortBy`.
78+
*
7579
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
7680
* @param string|null $sortBy
7781
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
@@ -104,6 +108,9 @@ public function sortDocs(Collection $docs, ?string $sortBy = 'default'): Collect
104108
}
105109

106110
/**
111+
* Group by `$groupBy`. {@see \Rakutentech\LaravelRequestDocs\Doc::$group} and {@see \Rakutentech\LaravelRequestDocs\Doc::$groupIndex} will be set.
112+
* The return collection is always sorted by `group`, `group_index`.
113+
*
107114
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
108115
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
109116
*/
@@ -129,6 +136,8 @@ public function groupDocs(Collection $docs, ?string $groupBy = 'default'): Colle
129136
}
130137

131138
/**
139+
* Get controllers and routes information and return a list of {@see \Rakutentech\LaravelRequestDocs\Doc}
140+
*
132141
* @param string[] $onlyMethods
133142
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
134143
* @throws \ReflectionException
@@ -191,6 +200,9 @@ public function getControllersInfo(array $onlyMethods): Collection
191200
}
192201

193202
/**
203+
* Parse from request object and set into {@see \Rakutentech\LaravelRequestDocs\Doc}
204+
* This method also read docBlock and update into {@see \Rakutentech\LaravelRequestDocs\Doc}.
205+
*
194206
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
195207
* @return \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc>
196208
* @throws \ReflectionException
@@ -245,6 +257,12 @@ public function appendRequestRules(Collection $docs): Collection
245257
return $docs;
246258
}
247259

260+
/**
261+
* Get description in between @lrd:start and @lrd:end from the doc block.
262+
*
263+
* @param string $docComment
264+
* @return string
265+
*/
248266
public function lrdDocComment(string $docComment): string
249267
{
250268
$lrdComment = "";
@@ -267,8 +285,10 @@ public function lrdDocComment(string $docComment): string
267285
}
268286

269287
/**
288+
* Parse rules from the request.
289+
*
270290
* @param array<string, \Illuminate\Contracts\Validation\Rule|array|string> $mixedRules
271-
* @return array<string, string[]>
291+
* @return array<string, string[]> Key is attribute, value is a list of rules.
272292
*/
273293
public function flattenRules(array $mixedRules): array
274294
{
@@ -300,7 +320,9 @@ public function flattenRules(array $mixedRules): array
300320
}
301321

302322
/**
303-
* @return array<string, string[]>
323+
* Read the source file and parse rules by regex.
324+
*
325+
* @return array<string, string[]> Key is attribute, value is a list of rules.
304326
* @throws \ReflectionException
305327
*/
306328
public function rulesByRegex(string $requestClassName, string $methodName): array
@@ -341,6 +363,8 @@ public function rulesByRegex(string $requestClassName, string $methodName): arra
341363
}
342364

343365
/**
366+
* Get additional rules by parsing the doc block.
367+
*
344368
* @param string $docComment
345369
* @return array<string, string[]>
346370
*/
@@ -366,8 +390,10 @@ private function customParamsDocComment(string $docComment): array
366390
}
367391

368392
/**
393+
* Get responses by parsing the doc block.
394+
*
369395
* @param string $docComment
370-
* @return string[]
396+
* @return string[] A list of responses. Will overwrite the default responses.
371397
*/
372398
private function customResponsesDocComment(string $docComment): array
373399
{
@@ -402,7 +428,7 @@ private function multiExplode(array $delimiters, string $string): array
402428
}
403429

404430
/**
405-
* Parse the `$docs['uri']` and attach `group` and `group_index` details.
431+
* Group by {@see \Rakutentech\LaravelRequestDocs\Doc::$uri} and attach {@see \Rakutentech\LaravelRequestDocs\Doc::$group} and {@see \Rakutentech\LaravelRequestDocs\Doc::$groupIndex} details.
406432
*
407433
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
408434
*/
@@ -431,7 +457,7 @@ private function groupDocsByAPIURI(Collection $docs): void
431457
}
432458

433459
/**
434-
* Create and return group name by the `$uri`.
460+
* Create and return group name by the {@see \Rakutentech\LaravelRequestDocs\Doc::$uri}.
435461
*/
436462
private function getGroupByURI(string $prefix, string $uri): string
437463
{
@@ -448,7 +474,7 @@ private function getGroupByURI(string $prefix, string $uri): string
448474
}
449475

450476
/**
451-
* Parse the `$docs['controller_full_path']` and attach `group` and `group_index` details.
477+
* Group by {@see \Rakutentech\LaravelRequestDocs\Doc::$controllerFullPath} and attach {@see \Rakutentech\LaravelRequestDocs\Doc::$group} and {@see \Rakutentech\LaravelRequestDocs\Doc::$groupIndex} details.
452478
*
453479
* @param \Illuminate\Support\Collection<int, \Rakutentech\LaravelRequestDocs\Doc> $docs
454480
*/

0 commit comments

Comments
 (0)