Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some funky string juggling for the never/void return type handling #245

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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