Skip to content

Commit 1484a81

Browse files
authored
Remove some funky string juggling for the never/void return type handling. (#245)
1 parent e7d2598 commit 1484a81

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/Visitor.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ public function enterNode(Node $node)
9494
$this->additionalTagStrings[$symbolName] = $additions;
9595
}
9696

97-
if ($voidOrNever !== '') {
97+
if ($voidOrNever instanceof Type) {
9898
$addition = sprintf(
9999
'@phpstan-return %s',
100-
$voidOrNever === 'never'
101-
? (new Never_())->__toString()
102-
: (new Void_())->__toString()
100+
$voidOrNever->__toString()
103101
);
104102
if (
105103
! isset($this->additionalTagStrings[$symbolName])
@@ -786,15 +784,18 @@ private static function isOptional(string $description): bool
786784
|| (stripos($description, 'Defaults to ') !== false);
787785
}
788786

789-
private function voidOrNever(Node $node): string
787+
private function voidOrNever(Node $node): ?Type
790788
{
789+
$never = new Never_();
790+
$void = new Void_();
791+
791792
if (! ($node instanceof Function_) && ! ($node instanceof ClassMethod)) {
792-
return '';
793+
return null;
793794
}
794795

795796
if (! isset($node->stmts) || count($node->stmts) === 0) {
796797
// Interfaces and abstract methods.
797-
return '';
798+
return null;
798799
}
799800

800801
$returnStmts = $this->nodeFinder->findInstanceOf($node, Stmt_Return::class);
@@ -811,11 +812,11 @@ static function (Node $node): bool {
811812
}
812813
) instanceof Node
813814
) {
814-
return '';
815+
return null;
815816
}
816817
// If there is no return statement that is not void,
817818
// it's return type void.
818-
return 'void';
819+
return $void;
819820
}
820821

821822
// Check for never return type.
@@ -826,12 +827,12 @@ static function (Node $node): bool {
826827
// If a first level statement is exit/die, it's return type never.
827828
if ($stmt->expr instanceof Exit_) {
828829
if (! $stmt->expr->expr instanceof String_) {
829-
return 'never';
830+
return $never;
830831
}
831832
if (strpos($stmt->expr->expr->value, 'must be overridden') !== false) {
832-
return '';
833+
return null;
833834
}
834-
return 'never';
835+
return $never;
835836
}
836837
if (! ($stmt->expr instanceof FuncCall) || ! ($stmt->expr->name instanceof Name)) {
837838
continue;
@@ -840,7 +841,7 @@ static function (Node $node): bool {
840841
// If a first level statement is a call to wp_send_json(_success/error),
841842
// it's return type never.
842843
if (strpos($name->toString(), 'wp_send_json') === 0) {
843-
return 'never';
844+
return $never;
844845
}
845846
// Skip all functions but wp_die().
846847
if (strpos($name->toString(), 'wp_die') !== 0) {
@@ -849,7 +850,7 @@ static function (Node $node): bool {
849850
$args = $stmt->expr->getArgs();
850851
// If wp_die is called without 3rd parameter, it's return type never.
851852
if (count($args) < 3) {
852-
return 'never';
853+
return $never;
853854
}
854855
// If wp_die is called with 3rd parameter, we need additional checks.
855856
try {
@@ -860,18 +861,18 @@ static function (Node $node): bool {
860861
}
861862

862863
if (is_int($arg)) {
863-
return 'never';
864+
return $never;
864865
}
865866

866867
if (! is_array($arg)) {
867868
continue;
868869
}
869870

870871
if (! array_key_exists('exit', $arg) || (bool)$arg['exit']) {
871-
return 'never';
872+
return $never;
872873
}
873874
}
874-
return '';
875+
return null;
875876
}
876877

877878
private function getCleanCommentsNode(Node $node): Node

0 commit comments

Comments
 (0)