Skip to content

Commit 1fa10da

Browse files
authored
Avoid adding void/never return tag to generator functions (#456)
1 parent 6486e13 commit 1fa10da

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/Visitor.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PhpParser\Comment\Doc;
1616
use PhpParser\ConstExprEvaluator;
1717
use PhpParser\Node;
18+
use PhpParser\Node\Expr\Yield_;
19+
use PhpParser\Node\Expr\YieldFrom;
1820
use PhpParser\NodeFinder;
1921
use PhpParser\Node\Identifier;
2022
use PhpParser\Node\Name;
@@ -813,6 +815,18 @@ private function voidOrNever(Node $node): ?Type
813815
return null;
814816
}
815817

818+
$yields = $this->nodeFinder->findFirst(
819+
$node,
820+
static function (Node $node): bool {
821+
return $node instanceof Yield_ || $node instanceof YieldFrom;
822+
}
823+
) instanceof Node;
824+
825+
if ($yields) {
826+
// Generator functions do not return void or never.
827+
return null;
828+
}
829+
816830
$returnStmts = $this->nodeFinder->findInstanceOf($node, Stmt_Return::class);
817831

818832
// If there is a return statement, it's not return type never.
@@ -852,14 +866,14 @@ static function (Node $node): bool {
852866
if (! ($stmt->expr instanceof FuncCall) || ! ($stmt->expr->name instanceof Name)) {
853867
continue;
854868
}
855-
$name = $stmt->expr->name;
869+
$name = strtolower((string)$stmt->expr->name);
856870
// If a first level statement is a call to wp_send_json(_success/error),
857871
// it's return type never.
858-
if (str_starts_with($name->toString(), 'wp_send_json')) {
872+
if (str_starts_with($name, 'wp_send_json')) {
859873
return $never;
860874
}
861875
// Skip all functions but wp_die().
862-
if (! str_starts_with($name->toString(), 'wp_die')) {
876+
if (! str_starts_with($name, 'wp_die')) {
863877
continue;
864878
}
865879
$args = $stmt->expr->getArgs();

wordpress-stubs.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68704,7 +68704,6 @@ public function paused_at_incomplete_token(): bool
6870468704
* // Outputs: "free <egg> lang-en "
6870568705
*
6870668706
* @since 6.4.0
68707-
* @phpstan-return void
6870868707
*/
6870968708
public function class_list()
6871068709
{

0 commit comments

Comments
 (0)