Skip to content

Commit

Permalink
Introduce & use custom ObjectStorage class
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 13, 2023
1 parent 6119afd commit f0a0fcc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Common/Promises.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<UuidInterface, ArrayObject<int, PromiseInterface>> */
/** @var ObjectStorage<UuidInterface, ArrayObject<int, PromiseInterface>> */
protected $promises;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<UuidInterface, TimerInterface> */
/** @var ObjectStorage<UuidInterface, TimerInterface> */
protected $timers;

/**
Expand Down
29 changes: 29 additions & 0 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ipl\Scheduler;

use ipl\Scheduler\Contract\Task;
use Ramsey\Uuid\UuidInterface;
use SplObjectStorage;

/**
* ObjectStorage provides custom implementation of the internal PHP hash method and doesn't depend on the
* `spl_object_hash()` function used by `SplObjectStorage` class.
*
* @extends SplObjectStorage<object, object|mixed>
*/
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);
}
}
11 changes: 5 additions & 6 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use React\EventLoop\Loop;
use React\Promise;
use React\Promise\ExtendedPromiseInterface;
use SplObjectStorage;
use Throwable;

class Scheduler
Expand Down Expand Up @@ -124,15 +123,15 @@ class Scheduler
*/
public const ON_TASK_EXPIRED = 'task-expired';

/** @var SplObjectStorage<Task, null> The scheduled tasks of this scheduler */
/** @var ObjectStorage<Task, null> The scheduled tasks of this scheduler */
protected $tasks;

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 7.2 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 7.3 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 7.4 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 8.0 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 8.1 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

Check failure on line 127 in src/Scheduler.php

View workflow job for this annotation

GitHub Actions / Static analysis for PHP 8.2 on ubuntu-latest

PHPDoc tag @var for property ipl\Scheduler\Scheduler::$tasks contains unresolvable type.

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();
}
Expand Down Expand Up @@ -177,7 +176,7 @@ public function removeTasks(): self
$this->cancelTask($task);
}

$this->tasks = new SplObjectStorage();
$this->tasks = new ObjectStorage();

return $this;
}
Expand Down

0 comments on commit f0a0fcc

Please sign in to comment.