|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bug10924; |
| 6 | + |
| 7 | +use function array_key_exists; |
| 8 | +use function spl_object_id; |
| 9 | + |
| 10 | +/** |
| 11 | + * @phpstan-template T of object |
| 12 | + * @phpstan-implements \IteratorAggregate<int, T> |
| 13 | + */ |
| 14 | +final class ObjectSet implements \IteratorAggregate |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var object[] |
| 18 | + * @phpstan-var array<int, T> |
| 19 | + */ |
| 20 | + private array $objects = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * @phpstan-param T ...$objects |
| 24 | + */ |
| 25 | + public function __construct(object ...$objects) |
| 26 | + { |
| 27 | + foreach ($objects as $object) { |
| 28 | + $this->objects[spl_object_id($object)] = $object; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** @phpstan-param T ...$objects */ |
| 33 | + public function add(object ...$objects): void |
| 34 | + { |
| 35 | + foreach ($objects as $object) { |
| 36 | + $this->objects[spl_object_id($object)] = $object; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** @phpstan-param T ...$objects */ |
| 41 | + public function remove(object ...$objects): void |
| 42 | + { |
| 43 | + foreach ($objects as $object) { |
| 44 | + unset($this->objects[spl_object_id($object)]); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function clear(): void |
| 49 | + { |
| 50 | + $this->objects = []; |
| 51 | + } |
| 52 | + |
| 53 | + public function contains(object $object): bool |
| 54 | + { |
| 55 | + return array_key_exists(spl_object_id($object), $this->objects); |
| 56 | + } |
| 57 | + |
| 58 | + /** @phpstan-return \ArrayIterator<int, T> */ |
| 59 | + public function getIterator(): \ArrayIterator |
| 60 | + { |
| 61 | + return new \ArrayIterator($this->objects); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return object[] |
| 66 | + * @phpstan-return array<int, T> |
| 67 | + */ |
| 68 | + public function toArray(): array |
| 69 | + { |
| 70 | + return $this->objects; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @phpstan-type CollectPromise object |
| 76 | + */ |
| 77 | +class TimingsHandler |
| 78 | +{ |
| 79 | + /** @phpstan-var ObjectSet<\Closure(bool $enable) : void> */ |
| 80 | + private static ?ObjectSet $toggleCallbacks = null; |
| 81 | + /** @phpstan-var ObjectSet<\Closure() : void> */ |
| 82 | + private static ?ObjectSet $resetCallbacks = null; |
| 83 | + /** @phpstan-var ObjectSet<\Closure() : list<CollectPromise>> */ |
| 84 | + private static ?ObjectSet $collectCallbacks = null; |
| 85 | + |
| 86 | + /** |
| 87 | + * @phpstan-return ObjectSet<\Closure(bool $enable) : void> |
| 88 | + */ |
| 89 | + public static function getToggleCallbacks(): ObjectSet |
| 90 | + { |
| 91 | + return self::$toggleCallbacks ??= new ObjectSet(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @phpstan-return ObjectSet<\Closure() : void> |
| 96 | + */ |
| 97 | + public static function getResetCallbacks(): ObjectSet |
| 98 | + { |
| 99 | + return self::$resetCallbacks ??= new ObjectSet(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @phpstan-return ObjectSet<\Closure() : list<CollectPromise>> |
| 104 | + */ |
| 105 | + public static function getCollectCallbacks(): ObjectSet |
| 106 | + { |
| 107 | + return self::$collectCallbacks ??= new ObjectSet(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments