Skip to content

Commit

Permalink
Remove some funky string juggling for the never/void return type hand…
Browse files Browse the repository at this point in the history
…ling. (#245)
  • Loading branch information
johnbillion authored Oct 5, 2024
1 parent e7d2598 commit 1484a81
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ public function enterNode(Node $node)
$this->additionalTagStrings[$symbolName] = $additions;
}

if ($voidOrNever !== '') {
if ($voidOrNever instanceof Type) {
$addition = sprintf(
'@phpstan-return %s',
$voidOrNever === 'never'
? (new Never_())->__toString()
: (new Void_())->__toString()
$voidOrNever->__toString()
);
if (
! isset($this->additionalTagStrings[$symbolName])
Expand Down Expand Up @@ -786,15 +784,18 @@ private static function isOptional(string $description): bool
|| (stripos($description, 'Defaults to ') !== false);
}

private function voidOrNever(Node $node): string
private function voidOrNever(Node $node): ?Type
{
$never = new Never_();
$void = new Void_();

if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod)) {
return '';
return null;
}

if (! isset($node->stmts) || count($node->stmts) === 0) {
// Interfaces and abstract methods.
return '';
return null;
}

$returnStmts = $this->nodeFinder->findInstanceOf($node, Stmt_Return::class);
Expand All @@ -811,11 +812,11 @@ static function (Node $node): bool {
}
) instanceof Node
) {
return '';
return null;
}
// If there is no return statement that is not void,
// it's return type void.
return 'void';
return $void;
}

// Check for never return type.
Expand All @@ -826,12 +827,12 @@ static function (Node $node): bool {
// If a first level statement is exit/die, it's return type never.
if ($stmt->expr instanceof Exit_) {
if (! $stmt->expr->expr instanceof String_) {
return 'never';
return $never;
}
if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) {
return '';
return null;
}
return 'never';
return $never;
}
if (! ($stmt->expr instanceof FuncCall) || ! ($stmt->expr->name instanceof Name)) {
continue;
Expand All @@ -840,7 +841,7 @@ static function (Node $node): bool {
// If a first level statement is a call to wp_send_json(_success/error),
// it's return type never.
if (strpos($name->toString(), 'wp_send_json') === 0) {
return 'never';
return $never;
}
// Skip all functions but wp_die().
if (strpos($name->toString(), 'wp_die') !== 0) {
Expand All @@ -849,7 +850,7 @@ static function (Node $node): bool {
$args = $stmt->expr->getArgs();
// If wp_die is called without 3rd parameter, it's return type never.
if (count($args) < 3) {
return 'never';
return $never;
}
// If wp_die is called with 3rd parameter, we need additional checks.
try {
Expand All @@ -860,18 +861,18 @@ static function (Node $node): bool {
}

if (is_int($arg)) {
return 'never';
return $never;
}

if (! is_array($arg)) {
continue;
}

if (! array_key_exists('exit', $arg) || (bool)$arg['exit']) {
return 'never';
return $never;
}
}
return '';
return null;
}

private function getCleanCommentsNode(Node $node): Node
Expand Down

0 comments on commit 1484a81

Please sign in to comment.