Skip to content

Commit

Permalink
feat adds rescue helper stub (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 13, 2021
1 parent 74e8d7f commit dfd4687
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
13 changes: 13 additions & 0 deletions stubs/Helpers.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php

/**
* @template TValue
* @template TDefault
*
* @param callable(): TValue $callback
* @param TDefault|(callable(): TDefault) $rescue
* @param bool $report
* @return TValue|TDefault
*/
function rescue(callable $callback, $rescue = null, $report = true)
{
}

/**
* @template TValue
* @param int $times
Expand Down
65 changes: 65 additions & 0 deletions tests/Features/ReturnTypes/Helpers/RescueStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Tests\Features\ReturnTypes\Helpers;

use Exception;
use function PHPStan\Testing\assertType;

class RescueStub
{
public function testRescueWithNullDefault(): void
{
$rescued = rescue(function () {
if (mt_rand(0, 1)) {
throw new Exception();
}

return 'ok';
});

assertType('string|null', $rescued);
}

public function testRescueWithScalarDefault(): void
{
$rescued = rescue(function () {
if (mt_rand(0, 1)) {
throw new Exception();
}

return 'ok';
}, 'failed');

assertType('string', $rescued);
}

public function testRescueWithClosureDefault(): void
{
$rescued = rescue(function () {
if (mt_rand(0, 1)) {
throw new Exception();
}

return 'ok';
}, function () {
return 0;
});

assertType('int|string', $rescued);
}

public function testRetryWithoutReporting(): void
{
$rescued = rescue(function () {
if (mt_rand(0, 1)) {
throw new Exception();
}

return 'ok';
}, 'failed', false);

assertType('string', $rescued);
}
}

0 comments on commit dfd4687

Please sign in to comment.