Skip to content

Commit

Permalink
VoidType - accept null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 13, 2023
1 parent 71f6c4d commit 40c8fb2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/VoidType.php
Expand Up @@ -66,7 +66,7 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
return $type->isAcceptedWithReasonBy($this, $strictTypes);
}

return AcceptsResult::createFromBoolean($type instanceof self);
return new AcceptsResult($type->isVoid()->or($type->isNull()), []);
}

public function isSuperTypeOf(Type $type): TrinaryLogic
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -988,4 +988,9 @@ public function testBug6653(): void
$this->analyse([__DIR__ . '/data/bug-6653.php'], []);
}

public function testBug10291(): void
{
$this->analyse([__DIR__ . '/data/bug-10291.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10291.php
@@ -0,0 +1,25 @@
<?php

namespace Bug10291;

class HelloWorld
{
/** @return void|int */
public function sayHello()
{
return rand(0, 1) ? 17 : null;
}

/**
* @param bool $something
* @return void|int
*/
public function sayHello2($something)
{
if ($something) {
return rand(0, 1);
} else {
return $this->myrand();
}
}
}

0 comments on commit 40c8fb2

Please sign in to comment.