Skip to content

Commit

Permalink
Fix return type pass with nullable types and intersection types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Jun 9, 2024
1 parent 2df7c5c commit aee429c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/CodeCleaner/ReturnTypePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -101,15 +102,15 @@ private function typeName(Node $node): string
return \implode('|', \array_map([$this, 'typeName'], $node->types));
}

if ($node instanceof NullableType) {
return \strtolower($node->type->name);
if ($node instanceof IntersectionType) {
return \implode('&', \array_map([$this, 'typeName'], $node->types));
}

if ($node instanceof Identifier) {
return \strtolower($node->name);
if ($node instanceof NullableType) {
return $this->typeName($node->type);
}

if ($node instanceof Name) {
if ($node instanceof Identifier || $node instanceof Name) {
return $node->toLowerString();
}

Expand Down
5 changes: 5 additions & 0 deletions test/CodeCleaner/ReturnTypePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function happyPaths()
{
return [
['$x = function(): DateTime { return new DateTime(); };'],
['$x = function(): ?DateTime { return new DateTime(); };'],
['$x = function(): A|B { return new C(); };'],
['$x = function(): A|DateTime { return new C(); };'],
['$x = function(): A&B { return new C(); };'],
['$x = function(): A&DateTime { return new C(); };'],
];
}

Expand Down

0 comments on commit aee429c

Please sign in to comment.