diff --git a/src/CodeCleaner/ReturnTypePass.php b/src/CodeCleaner/ReturnTypePass.php index 29b69895..cef7b6c3 100644 --- a/src/CodeCleaner/ReturnTypePass.php +++ b/src/CodeCleaner/ReturnTypePass.php @@ -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_; @@ -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(); } diff --git a/test/CodeCleaner/ReturnTypePassTest.php b/test/CodeCleaner/ReturnTypePassTest.php index f74b7fe4..0eddb60f 100644 --- a/test/CodeCleaner/ReturnTypePassTest.php +++ b/test/CodeCleaner/ReturnTypePassTest.php @@ -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(); };'], ]; }