Skip to content

Commit da4f096

Browse files
Merge pull request #234 from nextcloud/fix/controllermethod/prevent-missing-return-annotation
2 parents eec3922 + 3d10236 commit da4f096

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

generate-spec.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,6 @@
520520
}
521521

522522
$classMethodInfo = ControllerMethod::parse($routeName, $definitions, $methodFunction, $isAdmin, $isDeprecated, $isPasswordConfirmation, $isCORS);
523-
if ($classMethodInfo->returns !== []) {
524-
Logger::error($routeName, 'Returns an invalid response');
525-
continue;
526-
}
527523
if (count($classMethodInfo->responses) == 0) {
528524
Logger::error($routeName, 'Returns no responses');
529525
continue;

src/ControllerMethod.php

Lines changed: 12 additions & 4 deletions
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;
@@ -30,7 +31,6 @@ class ControllerMethod {
3031
public function __construct(
3132
public array $parameters,
3233
public array $responses,
33-
public array $returns,
3434
public array $responseDescription,
3535
public array $description,
3636
public ?string $summary,
@@ -46,18 +46,20 @@ public static function parse(string $context,
4646
bool $isPasswordConfirmation,
4747
bool $isCORS,
4848
): ControllerMethod {
49-
global $phpDocParser, $lexer, $allowMissingDocs;
49+
global $phpDocParser, $lexer, $nodeFinder, $allowMissingDocs;
5050

5151
$parameters = [];
5252
$responses = [];
5353
$responseDescriptions = [];
54-
$returns = [];
5554

5655
$methodDescription = [];
5756
$methodSummary = null;
5857
$methodParameters = $method->getParams();
5958
$docParameters = [];
6059

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

113115
if ($docNode->value instanceof ReturnTagValueNode) {
116+
$returnTagCount++;
117+
114118
$type = $docNode->value->type;
115119

116120
$responses = array_merge($responses, ResponseType::resolve($context . ': @return', $type));
@@ -138,6 +142,10 @@ public static function parse(string $context,
138142
}
139143
}
140144

145+
if ($returnStmtCount !== 0 && $returnTagCount === 0) {
146+
Logger::error($context, 'Missing @return annotation');
147+
}
148+
141149
if (!$allowMissingDocs) {
142150
foreach (array_unique(array_map(fn (ControllerMethodResponse $response): int => $response->statusCode, array_filter($responses, fn (?ControllerMethodResponse $response): bool => $response != null))) as $statusCode) {
143151
if ($statusCode < 500 && (!array_key_exists($statusCode, $responseDescriptions) || $responseDescriptions[$statusCode] === '')) {
@@ -266,7 +274,7 @@ public static function parse(string $context,
266274
Logger::warning($context, 'Summary ends with a punctuation mark');
267275
}
268276

269-
return new ControllerMethod($parameters, $responses, $returns, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated);
277+
return new ControllerMethod($parameters, $responses, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated);
270278
}
271279

272280
}

0 commit comments

Comments
 (0)