Skip to content

Commit 3d10236

Browse files
committed
fix(ControllerMethod): Log an error if return statements are detected but no @return annotation is present
Signed-off-by: provokateurin <[email protected]>
1 parent 5d90738 commit 3d10236

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/ControllerMethod.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OpenAPIExtractor;
99

1010
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Return_;
1112
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
@@ -45,7 +46,7 @@ public static function parse(string $context,
4546
bool $isPasswordConfirmation,
4647
bool $isCORS,
4748
): ControllerMethod {
48-
global $phpDocParser, $lexer, $allowMissingDocs;
49+
global $phpDocParser, $lexer, $nodeFinder, $allowMissingDocs;
4950

5051
$parameters = [];
5152
$responses = [];
@@ -56,6 +57,9 @@ public static function parse(string $context,
5657
$methodParameters = $method->getParams();
5758
$docParameters = [];
5859

60+
$returnStmtCount = count($nodeFinder->findInstanceOf($method->getStmts(), Return_::class));
61+
$returnTagCount = 0;
62+
5963
$doc = $method->getDocComment()?->getText();
6064
if ($doc !== null) {
6165
$docNodes = $phpDocParser->parse(new TokenIterator($lexer->tokenize($doc)))->children;
@@ -109,6 +113,8 @@ public static function parse(string $context,
109113
}
110114

111115
if ($docNode->value instanceof ReturnTagValueNode) {
116+
$returnTagCount++;
117+
112118
$type = $docNode->value->type;
113119

114120
$responses = array_merge($responses, ResponseType::resolve($context . ': @return', $type));
@@ -136,6 +142,10 @@ public static function parse(string $context,
136142
}
137143
}
138144

145+
if ($returnStmtCount !== 0 && $returnTagCount === 0) {
146+
Logger::error($context, 'Missing @return annotation');
147+
}
148+
139149
if (!$allowMissingDocs) {
140150
foreach (array_unique(array_map(fn (ControllerMethodResponse $response): int => $response->statusCode, array_filter($responses, fn (?ControllerMethodResponse $response): bool => $response != null))) as $statusCode) {
141151
if ($statusCode < 500 && (!array_key_exists($statusCode, $responseDescriptions) || $responseDescriptions[$statusCode] === '')) {

0 commit comments

Comments
 (0)