Skip to content

Commit 6cea81c

Browse files
Merge pull request #314 from Ken-vdE/comments-on-rules-method
Allow comments on the rules method of the Request class
2 parents 61b0851 + bf5dd5e commit 6cea81c

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed

src/LaravelRequestDocs.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,17 @@ public function appendRequestRules(Collection $docs): Collection
231231
continue;
232232
}
233233

234-
$reflectionMethod = new ReflectionMethod($doc->getControllerFullPath(), $doc->getMethod());
234+
$controllerReflectionMethod = new ReflectionMethod($doc->getControllerFullPath(), $doc->getMethod());
235235

236-
$docComment = $this->getDocComment($reflectionMethod);
236+
$controllerMethodDocComment = $this->getDocComment($controllerReflectionMethod);
237237

238-
$customRules = $this->customParamsDocComment($docComment);
239-
$doc->setResponses($this->customResponsesDocComment($docComment));
238+
$controllerMethodLrdComment = $this->lrdDocComment($controllerMethodDocComment);
239+
$controllerMethodDocRules = $this->customParamsDocComment($controllerMethodDocComment);
240240

241-
foreach ($reflectionMethod->getParameters() as $param) {
241+
$doc->setResponses($this->customResponsesDocComment($controllerMethodDocComment));
242+
243+
$lrdDocComments = [];
244+
foreach ($controllerReflectionMethod->getParameters() as $param) {
242245
/** @var \ReflectionNamedType|\ReflectionUnionType|\ReflectionIntersectionType|null $namedType */
243246
$namedType = $param->getType();
244247
if (!$namedType) {
@@ -261,18 +264,29 @@ public function appendRequestRules(Collection $docs): Collection
261264

262265
try {
263266
$doc->mergeRules($this->flattenRules($requestObject->$requestMethod()));
267+
$requestReflectionMethod = new ReflectionMethod($requestObject, $requestMethod);
264268
} catch (Throwable $e) {
265269
$doc->mergeRules($this->rulesByRegex($requestClassName, $requestMethod));
270+
$requestReflectionMethod = new ReflectionMethod($requestClassName, $requestMethod);
266271
}
272+
273+
$requestMethodDocComment = $this->getDocComment($requestReflectionMethod);
274+
275+
$requestMethodLrdComment = $this->lrdDocComment($requestMethodDocComment);
276+
$requestMethodDocRules = $this->customParamsDocComment($requestMethodDocComment);
277+
278+
$lrdDocComments[] = $requestMethodLrdComment;
279+
$doc->mergeRules($requestMethodDocRules);
267280
}
268281
} catch (Throwable $e) {
269282
// Do nothing.
270283
}
271-
272-
$doc->mergeRules($customRules);
273284
}
274285

275-
$doc->setDocBlock($this->lrdDocComment($docComment));
286+
$lrdDocComments[] = $controllerMethodLrdComment;
287+
$lrdDocComments = array_filter($lrdDocComments, fn($s) => $s !== '');
288+
$doc->setDocBlock(join("\n", $lrdDocComments));
289+
$doc->mergeRules($controllerMethodDocRules);
276290
}
277291
return $docs;
278292
}

tests/Controllers/LaravelRequestDocsControllerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testApi()
2727
);
2828

2929
/** {@see \Rakutentech\LaravelRequestDocs\Tests\TestCase::registerRoutes()} */
30-
$this->assertCount(28, $response->json());
30+
$this->assertCount(29, $response->json());
3131

3232
$this->assertSame($expected, $response->json());
3333
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\Stubs\TestControllers;
4+
5+
use Rakutentech\LaravelRequestDocs\Tests\Stubs\TestRequests\CommentsOnRequestRulesMethodRequest;
6+
7+
class CommentsOnRequestRulesMethodController
8+
{
9+
/**
10+
* Before
11+
*
12+
* @lrd:start
13+
* # Controller
14+
* ## Index Method Comment
15+
* @lrd:end
16+
*
17+
* @LRDparam extra_index_field_1 string|max:32
18+
* // either space or pipe
19+
* @LRDparam extra_index_field_2 string|nullable|max:32
20+
* // duplicate param in controller
21+
* @LRDparam this_is_a_duplicate_param controller description
22+
* // override the default response codes
23+
* @LRDresponses 201|244
24+
*
25+
* After
26+
*/
27+
public function index(CommentsOnRequestRulesMethodRequest $request)
28+
{
29+
return 1;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\Stubs\TestRequests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class CommentsOnRequestRulesMethodRequest extends FormRequest
8+
{
9+
/**
10+
* Get the validation rules that apply to the request.
11+
*
12+
* @lrd:start
13+
* # Request
14+
* ## Rules Method Comment
15+
* @lrd:end
16+
*
17+
* @LRDparam extra_rules_field_1 string|max:32
18+
* // either space or pipe
19+
* @LRDparam extra_rules_field_2 string|nullable|max:32
20+
* // duplicate param in controller
21+
* @LRDparam this_is_a_duplicate_param request description
22+
*
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
'message_param' => 'nullable|string',
29+
];
30+
}
31+
}

tests/TestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function registerRoutes()
4343
Route::get('health', [TestControllers\WelcomeController::class, 'health']);
4444
Route::get('single', TestControllers\SingleActionController::class);
4545
Route::delete('welcome/no-rules', [TestControllers\WelcomeController::class, 'noRules']);
46+
Route::post('comments-on-request-rules-method', [TestControllers\CommentsOnRequestRulesMethodController::class, 'index']);
4647

4748
Route::get('closure', function () {
4849
return true;

tests/mocks/lrd-response.json

+42-1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,47 @@
456456
"503"
457457
]
458458
},
459+
{
460+
"uri": "comments-on-request-rules-method",
461+
"middlewares": [],
462+
"controller": "CommentsOnRequestRulesMethodController",
463+
"controller_full_path": "Rakutentech\\LaravelRequestDocs\\Tests\\Stubs\\TestControllers\\CommentsOnRequestRulesMethodController",
464+
"method": "index",
465+
"http_method": "POST",
466+
"path_parameters": [],
467+
"rules": {
468+
"message_param": [
469+
"nullable|string"
470+
],
471+
"extra_rules_field_1": [
472+
"string",
473+
"max:32"
474+
],
475+
"extra_rules_field_2": [
476+
"string",
477+
"nullable",
478+
"max:32"
479+
],
480+
"this_is_a_duplicate_param": [
481+
"controller",
482+
"description"
483+
],
484+
"extra_index_field_1": [
485+
"string",
486+
"max:32"
487+
],
488+
"extra_index_field_2": [
489+
"string",
490+
"nullable",
491+
"max:32"
492+
]
493+
},
494+
"doc_block": " # Request\n ## Rules Method Comment\n\n # Controller\n ## Index Method Comment\n",
495+
"responses": [
496+
"201",
497+
"244"
498+
]
499+
},
459500
{
460501
"uri": "closure",
461502
"middlewares": [],
@@ -743,4 +784,4 @@
743784
"503"
744785
]
745786
}
746-
]
787+
]

0 commit comments

Comments
 (0)