Skip to content

Commit

Permalink
Added int type check to $tries and declared strict types (BC break).
Browse files Browse the repository at this point in the history
Added FailingTooHardException::getAttempts.
  • Loading branch information
Bilge committed Jul 12, 2021
1 parent aae0269 commit 2f3ea33
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9",
"amphp/amp": "^2.1"
"amphp/amp": "^2.2"
},
"autoload": {
"files": [
Expand All @@ -27,6 +27,11 @@
"ScriptFUSION\\Retry\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ScriptFUSIONTest\\Retry\\": "test"
}
},
"scripts": {
"test": "phpunit -c test"
}
Expand Down
18 changes: 17 additions & 1 deletion src/FailingTooHardException.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Retry;

/**
* The exception that is thrown when an operation fails too many times.
*/
class FailingTooHardException extends \RuntimeException
{
public function __construct($attempts, \Exception $previous)
private $attempts;

public function __construct(int $attempts, \Exception $previous)
{
parent::__construct("Operation failed after $attempts attempt(s).", 0, $previous);

$this->attempts = $attempts;
}

/**
* Gets the number of times the operation was attempted before giving up.
*
* @return int Number of attempts.
*/
public function getAttempts(): int
{
return $this->attempts;
}
}
17 changes: 7 additions & 10 deletions src/retry.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Retry;

use Amp\Coroutine;
Expand All @@ -12,19 +14,16 @@
*
* @param int $tries Number of times.
* @param callable $operation Operation.
* @param callable $onError Optional. Exception handler.
* @param callable|null $onError Optional. Exception handler.
*
* @return mixed Result of running the operation if tries is greater than zero, otherwise null.
*
* @throws FailingTooHardException The maximum number of attempts was reached.
* @throws \UnexpectedValueException The operation returned an unsupported type.
*/
function retry($tries, callable $operation, callable $onError = null)
function retry(int $tries, callable $operation, callable $onError = null)
{
/** @var \Generator $generator */
$generator = (static function () use ($tries, $operation, $onError): \Generator {
// Nothing to do if tries less than or equal to zero.
if (($tries |= 0) <= $attempts = 0) {
if ($tries <= $attempts = 0) {
return;
}

Expand Down Expand Up @@ -76,15 +75,13 @@ function retry($tries, callable $operation, callable $onError = null)
*
* @param int $tries Number of times.
* @param callable $operation Operation.
* @param callable $onError Optional. Exception handler.
* @param callable|null $onError Optional. Exception handler.
*
* @return Promise Promise that returns the result of running the operation if tries is greater than zero, otherwise
* a promise that yields null.
*
* @throws FailingTooHardException The maximum number of attempts was reached.
* @throws \UnexpectedValueException The operation returned an unsupported type.
*/
function retryAsync($tries, callable $operation, callable $onError = null): Promise
function retryAsync(int $tries, callable $operation, callable $onError = null): Promise
{
$generator = retry($tries, $operation, $onError);

Expand Down
18 changes: 18 additions & 0 deletions test/FailingTooHardExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace ScriptFUSIONTest\Retry;

use ScriptFUSION\Retry\FailingTooHardException;
use PHPUnit\Framework\TestCase;

/**
* @see FailingTooHardException
*/
final class FailingTooHardExceptionTest extends TestCase
{
public function testGetAttempts(): void
{
self::assertSame($attempts = 123, (new FailingTooHardException($attempts, new \Exception()))->getAttempts());
}
}
11 changes: 6 additions & 5 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<phpunit
beStrictAboutOutputDuringTests="true"
>
<testsuite>
<testsuite name="all">
<directory>.</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">

<coverage processUncoveredFiles="true">
<include>
<directory>../src</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>

0 comments on commit 2f3ea33

Please sign in to comment.