Skip to content

Commit

Permalink
[TypeDeclaration] Fix return type union with null/mixed (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 25, 2021
1 parent 9b51302 commit a5624ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;

final class SkipUnionNullMixed
{
/**
* @var mixed[]
*/
private $values = [];

public function get($key)
{
if (isset($this->values[$key])) {
return $this->values[$key];
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ public function refactor(Node $node): ?Node
return $this->processType($node, $inferedReturnType);
}

if (! $inferedReturnType->isSuperTypeOf(new MixedType())->yes()) {
return $this->processType($node, $inferedReturnType);
foreach ($inferedReturnType->getTypes() as $unionedType) {
// mixed type cannot be joined with another types
if ($unionedType instanceof MixedType) {
return null;
}
}

return null;
return $this->processType($node, $inferedReturnType);
}

public function provideMinPhpVersion(): int
Expand Down

0 comments on commit a5624ca

Please sign in to comment.