Skip to content

Commit

Permalink
Merge pull request #1184 from ghostwriter/bugfix/support-never-return…
Browse files Browse the repository at this point in the history
…-type

[8.1] Support `never` return type
  • Loading branch information
davedevelopment committed Jul 21, 2022
2 parents 33cb226 + ec2443a commit 750ce17
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function renderMethodBody($method, $config)

$body .= "\$ret = {$invoke}(__FUNCTION__, \$argv);\n";

if ($method->getReturnType() !== "void") {
if (! in_array($method->getReturnType(), ['never','void'], true)) {
$body .= "return \$ret;\n";
}

Expand Down
49 changes: 49 additions & 0 deletions tests/PHP81/Php81LanguageFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use ReturnTypeWillChange;
use RuntimeException;
use Serializable;
use function pcntl_fork;
use function pcntl_waitpid;
use function pcntl_wexitstatus;

/**
* @requires PHP 8.1.0-dev
Expand Down Expand Up @@ -82,6 +86,39 @@ public function it_can_mock_a_class_with_an_intersection_argument_type_hint()

$mock->foo($object);
}

/** @test */
public function it_can_mock_a_class_with_a_never_returning_type_hint()
{
$mock = Mockery::mock(NeverReturningTypehintClass::class)->makePartial();

$this->expectException(RuntimeException::class);
$mock->throws();
}

/**
* @requires extension pcntl
* @test
*/
public function it_can_mock_a_class_with_a_never_returning_type_hint_with_exit()
{
$mock = Mockery::mock(NeverReturningTypehintClass::class)->makePartial();

$pid = pcntl_fork();

if (-1 === $pid) {
$this->markTestSkipped("Couldn't fork for exit test");

return;
} elseif ($pid) {
pcntl_waitpid($pid, $status);
$this->assertEquals(123, pcntl_wexitstatus($status));

return;
}

$mock->exits();
}
}

interface LoggerInterface
Expand Down Expand Up @@ -135,6 +172,18 @@ public function getTimestamp(): float
}
}

class NeverReturningTypehintClass
{
public function throws(): never
{
throw new RuntimeException('Never!');
}

public function exits(): never
{
exit(123);
}
}
class IntersectionTypeHelperClass
{
}
Expand Down

0 comments on commit 750ce17

Please sign in to comment.