Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce & use custom ObjectStorage class #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
Expand Down
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;

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