From dbd40db59d457e9f893f8db58da877de7dbdd38c Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 13 Nov 2023 16:42:24 +0100 Subject: [PATCH] Introduce & use custom `ObjectStorage` class --- phpstan.neon | 2 ++ src/Common/Promises.php | 4 ++-- src/Common/Timers.php | 4 ++-- src/ObjectStorage.php | 29 +++++++++++++++++++++++++++++ src/Scheduler.php | 11 +++++------ 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/ObjectStorage.php diff --git a/phpstan.neon b/phpstan.neon index 5891e2f..839b2cd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -23,6 +23,8 @@ parameters: - '#Call to an undefined method DateTimeInterface::#' + - '#PHPDoc tag @var for property ipl\\Scheduler\\Scheduler::.* contains unresolvable type#' + - '#Call to an undefined method React\\Promise\\PromiseInterface::#' - '#Method ipl\\Scheduler\\.* should return \$this.* but returns static#' diff --git a/src/Common/Promises.php b/src/Common/Promises.php index b896627..33cede1 100644 --- a/src/Common/Promises.php +++ b/src/Common/Promises.php @@ -4,13 +4,13 @@ use ArrayObject; use InvalidArgumentException; +use ipl\Scheduler\ObjectStorage; use Ramsey\Uuid\UuidInterface; use React\Promise\PromiseInterface; -use SplObjectStorage; trait Promises { - /** @var SplObjectStorage> */ + /** @var ObjectStorage> */ protected $promises; /** diff --git a/src/Common/Timers.php b/src/Common/Timers.php index 2d0641f..719202a 100644 --- a/src/Common/Timers.php +++ b/src/Common/Timers.php @@ -2,13 +2,13 @@ namespace ipl\Scheduler\Common; +use ipl\Scheduler\ObjectStorage; use Ramsey\Uuid\UuidInterface; use React\EventLoop\TimerInterface; -use SplObjectStorage; trait Timers { - /** @var SplObjectStorage */ + /** @var ObjectStorage */ protected $timers; /** diff --git a/src/ObjectStorage.php b/src/ObjectStorage.php new file mode 100644 index 0000000..9553afb --- /dev/null +++ b/src/ObjectStorage.php @@ -0,0 +1,29 @@ + + */ +class ObjectStorage extends SplObjectStorage +{ + public function getHash($object): string + { + if ($object instanceof Task) { + return $object->getUuid()->toString(); + } + + if ($object instanceof UuidInterface) { + return $object->toString(); + } + + return parent::getHash($object); + } +} diff --git a/src/Scheduler.php b/src/Scheduler.php index 25ad3a1..0d6618b 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -12,7 +12,6 @@ use React\EventLoop\Loop; use React\Promise; use React\Promise\ExtendedPromiseInterface; -use SplObjectStorage; use Throwable; class Scheduler @@ -124,15 +123,15 @@ class Scheduler */ public const ON_TASK_EXPIRED = 'task-expired'; - /** @var SplObjectStorage The scheduled tasks of this scheduler */ + /** @var ObjectStorage The scheduled tasks of this scheduler */ protected $tasks; public function __construct() { - $this->tasks = new SplObjectStorage(); + $this->tasks = new ObjectStorage(); - $this->promises = new SplObjectStorage(); - $this->timers = new SplObjectStorage(); + $this->promises = new ObjectStorage(); + $this->timers = new ObjectStorage(); $this->init(); } @@ -177,7 +176,7 @@ public function removeTasks(): self $this->cancelTask($task); } - $this->tasks = new SplObjectStorage(); + $this->tasks = new ObjectStorage(); return $this; }