-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
ForAllPromiseInterface
with finally
and `suppress…
…Errors` method Signed-off-by: Maximilian Bösing <[email protected]>
- Loading branch information
Showing
9 changed files
with
237 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Boesing\TypedArrays; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ForAllPromise implements ForAllPromiseInterface | ||
{ | ||
/** @var callable():void|null */ | ||
private $finally; | ||
|
||
/** @var callable */ | ||
private $task; | ||
|
||
/** @var bool */ | ||
private $suppressErrors = false; | ||
|
||
/** @var bool */ | ||
private $executed = false; | ||
|
||
/** | ||
* @param callable():void $task | ||
*/ | ||
public function __construct(callable $task) | ||
{ | ||
$this->task = $task; | ||
} | ||
|
||
public function finally(callable $callback): ForAllPromiseInterface | ||
{ | ||
$this->finally = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function suppressErrors(): ForAllPromiseInterface | ||
{ | ||
$this->suppressErrors = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->executed) { | ||
return; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
|
||
public function execute(): void | ||
{ | ||
$exception = null; | ||
|
||
try { | ||
($this->task)(); | ||
} catch (RuntimeException $exception) { | ||
} finally { | ||
$this->executed = true; | ||
} | ||
|
||
if ($this->finally !== null) { | ||
($this->finally)(); | ||
} | ||
|
||
if ($exception === null || $this->suppressErrors === true) { | ||
return; | ||
} | ||
|
||
throw $exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Boesing\TypedArrays; | ||
|
||
/** | ||
* Promise interface to create post-execution stuff. | ||
* This interface is not meant to be stored in any variable. | ||
*/ | ||
interface ForAllPromiseInterface | ||
{ | ||
/** | ||
* @param callable():void $callback | ||
*/ | ||
public function finally(callable $callback): self; | ||
|
||
public function suppressErrors(): self; | ||
|
||
public function execute(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Boesing\TypedArrays; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ForAllPromiseTest extends TestCase | ||
{ | ||
public function testWillNotExecuteTwice(): void | ||
{ | ||
$executed = false; | ||
$task = static function () use (&$executed): void { | ||
Assert::false($executed); | ||
$executed = true; | ||
}; | ||
|
||
(new ForAllPromise($task))->execute(); | ||
$this->expectNotToPerformAssertions(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters