Skip to content

Commit 2f3ea33

Browse files
committed
Added int type check to $tries and declared strict types (BC break).
Added FailingTooHardException::getAttempts.
1 parent aae0269 commit 2f3ea33

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^8.5|^9",
20-
"amphp/amp": "^2.1"
20+
"amphp/amp": "^2.2"
2121
},
2222
"autoload": {
2323
"files": [
@@ -27,6 +27,11 @@
2727
"ScriptFUSION\\Retry\\": "src"
2828
}
2929
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"ScriptFUSIONTest\\Retry\\": "test"
33+
}
34+
},
3035
"scripts": {
3136
"test": "phpunit -c test"
3237
}

src/FailingTooHardException.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace ScriptFUSION\Retry;
35

46
/**
57
* The exception that is thrown when an operation fails too many times.
68
*/
79
class FailingTooHardException extends \RuntimeException
810
{
9-
public function __construct($attempts, \Exception $previous)
11+
private $attempts;
12+
13+
public function __construct(int $attempts, \Exception $previous)
1014
{
1115
parent::__construct("Operation failed after $attempts attempt(s).", 0, $previous);
16+
17+
$this->attempts = $attempts;
18+
}
19+
20+
/**
21+
* Gets the number of times the operation was attempted before giving up.
22+
*
23+
* @return int Number of attempts.
24+
*/
25+
public function getAttempts(): int
26+
{
27+
return $this->attempts;
1228
}
1329
}

src/retry.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace ScriptFUSION\Retry;
35

46
use Amp\Coroutine;
@@ -12,19 +14,16 @@
1214
*
1315
* @param int $tries Number of times.
1416
* @param callable $operation Operation.
15-
* @param callable $onError Optional. Exception handler.
17+
* @param callable|null $onError Optional. Exception handler.
1618
*
1719
* @return mixed Result of running the operation if tries is greater than zero, otherwise null.
1820
*
19-
* @throws FailingTooHardException The maximum number of attempts was reached.
20-
* @throws \UnexpectedValueException The operation returned an unsupported type.
2121
*/
22-
function retry($tries, callable $operation, callable $onError = null)
22+
function retry(int $tries, callable $operation, callable $onError = null)
2323
{
24-
/** @var \Generator $generator */
2524
$generator = (static function () use ($tries, $operation, $onError): \Generator {
2625
// Nothing to do if tries less than or equal to zero.
27-
if (($tries |= 0) <= $attempts = 0) {
26+
if ($tries <= $attempts = 0) {
2827
return;
2928
}
3029

@@ -76,15 +75,13 @@ function retry($tries, callable $operation, callable $onError = null)
7675
*
7776
* @param int $tries Number of times.
7877
* @param callable $operation Operation.
79-
* @param callable $onError Optional. Exception handler.
78+
* @param callable|null $onError Optional. Exception handler.
8079
*
8180
* @return Promise Promise that returns the result of running the operation if tries is greater than zero, otherwise
8281
* a promise that yields null.
8382
*
84-
* @throws FailingTooHardException The maximum number of attempts was reached.
85-
* @throws \UnexpectedValueException The operation returned an unsupported type.
8683
*/
87-
function retryAsync($tries, callable $operation, callable $onError = null): Promise
84+
function retryAsync(int $tries, callable $operation, callable $onError = null): Promise
8885
{
8986
$generator = retry($tries, $operation, $onError);
9087

test/FailingTooHardExceptionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSIONTest\Retry;
5+
6+
use ScriptFUSION\Retry\FailingTooHardException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @see FailingTooHardException
11+
*/
12+
final class FailingTooHardExceptionTest extends TestCase
13+
{
14+
public function testGetAttempts(): void
15+
{
16+
self::assertSame($attempts = 123, (new FailingTooHardException($attempts, new \Exception()))->getAttempts());
17+
}
18+
}

test/phpunit.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<phpunit
22
beStrictAboutOutputDuringTests="true"
33
>
4-
<testsuite>
4+
<testsuite name="all">
55
<directory>.</directory>
66
</testsuite>
7-
<filter>
8-
<whitelist processUncoveredFilesFromWhitelist="true">
7+
8+
<coverage processUncoveredFiles="true">
9+
<include>
910
<directory>../src</directory>
10-
</whitelist>
11-
</filter>
11+
</include>
12+
</coverage>
1213
</phpunit>

0 commit comments

Comments
 (0)