summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Util/WeakMap.php
blob: 3b62d6d64aabced0628685a3133ba4f8b2a57902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Util;

use ArrayAccess;
use function assert;
use function class_exists;
use function count;
use Countable;
use Error;
use function get_class;
use function is_object;
use IteratorAggregate;
use const PHP_VERSION_ID;
use function spl_object_id;
use function sprintf;
use Traversable;
use TypeError;
use WeakReference;

/**
 * @internal
 */
final class WeakMap implements ArrayAccess, Countable, IteratorAggregate
{
    private const KEY = '__otel_weak_map';

    /**
     * @var array<int, WeakReference>
     */
    private array $objects = [];

    private function __construct()
    {
    }

    /**
     * @return ArrayAccess&Countable&IteratorAggregate
     */
    public static function create(): ArrayAccess
    {
        if (PHP_VERSION_ID >= 80000) {
            /** @phan-suppress-next-line PhanUndeclaredClassReference */
            assert(class_exists(\WeakMap::class, false));
            /** @phan-suppress-next-line PhanUndeclaredClassMethod */
            $map = new \WeakMap();
            assert($map instanceof ArrayAccess);
            assert($map instanceof Countable);
            assert($map instanceof IteratorAggregate);

            return $map;
        }

        return new self();
    }

    public function offsetExists($offset): bool
    {
        if (!is_object($offset)) {
            throw new TypeError('WeakMap key must be an object');
        }

        return isset($offset->{self::KEY}[spl_object_id($this)]);
    }

    /**
     * @phan-suppress PhanUndeclaredClassAttribute
     */
    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        if (!is_object($offset)) {
            throw new TypeError('WeakMap key must be an object');
        }
        if (!$this->contains($offset)) {
            throw new Error(sprintf('Object %s#%d not contained in WeakMap', get_class($offset), spl_object_id($offset)));
        }

        return $offset->{self::KEY}[spl_object_id($this)];
    }

    public function offsetSet($offset, $value): void
    {
        if ($offset === null) {
            throw new Error('Cannot append to WeakMap');
        }
        if (!is_object($offset)) {
            throw new TypeError('WeakMap key must be an object');
        }
        if (!$this->contains($offset)) {
            $this->expunge();
        }

        $offset->{self::KEY}[spl_object_id($this)] = $value;
        $this->objects[spl_object_id($offset)] = WeakReference::create($offset);
    }

    public function offsetUnset($offset): void
    {
        if (!is_object($offset)) {
            throw new TypeError('WeakMap key must be an object');
        }
        if (!$this->contains($offset)) {
            return;
        }

        unset(
            $offset->{self::KEY}[spl_object_id($this)],
            $this->objects[spl_object_id($offset)],
        );
        if (!$offset->{self::KEY}) {
            unset($offset->{self::KEY});
        }
    }

    public function count(): int
    {
        $this->expunge();

        return count($this->objects);
    }

    public function getIterator(): Traversable
    {
        $this->expunge();

        foreach ($this->objects as $reference) {
            if (($object = $reference->get()) && $this->contains($object)) {
                yield $object => $this[$object];
            }
        }
    }

    public function __debugInfo(): array
    {
        $debugInfo = [];
        foreach ($this as $key => $value) {
            $debugInfo[] = ['key' => $key, 'value' => $value];
        }

        return $debugInfo;
    }

    public function __destruct()
    {
        foreach ($this->objects as $reference) {
            if ($object = $reference->get()) {
                unset($this[$object]);
            }
        }
    }

    private function contains(object $offset): bool
    {
        $reference = $this->objects[spl_object_id($offset)] ?? null;
        if ($reference && $reference->get() === $offset) {
            return true;
        }

        unset($this->objects[spl_object_id($offset)]);

        return false;
    }

    private function expunge(): void
    {
        foreach ($this->objects as $id => $reference) {
            if (!$reference->get()) {
                unset($this->objects[$id]);
            }
        }
    }
}