• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elephox-dev / framework / 4877852653

pending completion
4877852653

push

github

Ricardo Boss
WIP

38 of 38 new or added lines in 6 files covered. (100.0%)

3863 of 5835 relevant lines covered (66.2%)

8.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/modules/Collection/src/ArrayMap.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Elephox\Collection;
5

6
use ArrayIterator;
7
use Elephox\Collection\Contract\GenericArrayMap;
8
use InvalidArgumentException;
9
use Iterator;
10
use Traversable;
11

12
/**
13
 * @template TKey of array-key
14
 * @template TValue
15
 *
16
 * @implements GenericArrayMap<TKey, TValue>
17
 */
18
class ArrayMap implements GenericArrayMap
19
{
20
        // TODO: replace generic enumerable function with array-specific functions where possible
21
        /**
22
         * @use IsKeyedEnumerable<TKey, TValue>
23
         */
24
        use IsKeyedEnumerable {
25
                //                IsKeyedEnumerable::contains as genericContains;
26
                IsKeyedEnumerable::firstOrDefault as genericFirstOrDefault;
27
                IsKeyedEnumerable::count as genericCount;
28
        }
29

30
        /**
31
         * @use IsArrayEnumerable<TKey, TValue>
32
         */
33
        use IsArrayEnumerable {
34
                IsArrayEnumerable::contains as arrayContains;
35
                IsArrayEnumerable::containsKey as arrayContainsKey;
36
                IsArrayEnumerable::count as arrayCount;
37
                //                IsArrayEnumerable::firstOrDefault as arrayFirstOrDefault;
38
        }
39

40
        /**
41
         * @template UKey of array-key
42
         * @template UValue
43
         *
44
         * @param ArrayMap<UKey, UValue>|iterable<UKey, UValue> $value
45
         *
46
         * @return self<UKey, UValue>
47
         */
48
        public static function from(mixed $value): self
49
        {
50
                if ($value instanceof self) {
5✔
51
                        return $value;
1✔
52
                }
53

54
                if (is_array($value)) {
4✔
55
                        return new self($value);
2✔
56
                }
57

58
                if ($value instanceof Iterator) {
2✔
59
                        return new self(iterator_to_array($value));
1✔
60
                }
61

62
                throw new InvalidArgumentException('Cannot create ArrayMap from given value');
1✔
63
        }
64

65
        /**
66
         * @param array<TKey, TValue> $items
67
         */
68
        public function __construct(
69
                protected array $items = [],
70
        ) {
71
        }
320✔
72

73
        public function getIterator(): Traversable
74
        {
75
                return new ArrayIterator($this->items);
180✔
76
        }
77

78
        /**
79
         * @param TKey $key
80
         * @param TValue $value
81
         */
82
        public function put(mixed $key, mixed $value): bool
83
        {
84
                $validKey = $this->validateKey($key);
108✔
85

86
                $existed = $this->has($validKey);
107✔
87

88
                $this->items[$validKey] = $value;
107✔
89

90
                return !$existed;
107✔
91
        }
92

93
        /**
94
         * @param TKey $key
95
         *
96
         * @return TValue
97
         *
98
         * @throws OffsetNotFoundException
99
         */
100
        public function get(mixed $key): mixed
101
        {
102
                $validKey = $this->validateKey($key);
36✔
103

104
                if (!$this->has($validKey)) {
36✔
105
                        throw new OffsetNotFoundException($key);
1✔
106
                }
107

108
                return $this->items[$validKey];
35✔
109
        }
110

111
        /**
112
         * @param TKey $key
113
         */
114
        public function has(mixed $key): bool
115
        {
116
                return array_key_exists($key, $this->items);
160✔
117
        }
118

119
        /**
120
         * @param TKey $key
121
         */
122
        public function remove(mixed $key): bool
123
        {
124
                $validKey = $this->validateKey($key);
11✔
125

126
                if (!$this->has($validKey)) {
11✔
127
                        return false;
2✔
128
                }
129

130
                if (is_int($validKey)) {
10✔
131
                        array_splice($this->items, $validKey, 1);
1✔
132
                } else {
133
                        unset($this->items[$validKey]);
9✔
134
                }
135

136
                return true;
10✔
137
        }
138

139
        public function clear(): void
140
        {
141
                $this->items = [];
4✔
142
        }
143

144
        /**
145
         * @return array-key
146
         */
147
        protected function validateKey(mixed $key): string|int
148
        {
149
                if (!is_string($key) && !is_int($key)) {
123✔
150
                        throw new OffsetNotAllowedException($key);
1✔
151
                }
152

153
                return $key;
122✔
154
        }
155

156
        public function offsetExists(mixed $offset): bool
157
        {
158
                return $this->has($offset);
2✔
159
        }
160

161
        public function offsetGet(mixed $offset): mixed
162
        {
163
                return $this->get($offset);
1✔
164
        }
165

166
        public function offsetSet(mixed $offset, mixed $value): void
167
        {
168
                if ($offset === null) {
9✔
169
                        throw new InvalidArgumentException('Cannot set null offset in ArrayMap');
1✔
170
                }
171

172
                $this->put($offset, $value);
9✔
173
        }
174

175
        public function offsetUnset(mixed $offset): void
176
        {
177
                $this->remove($offset);
4✔
178
        }
179

180
        public function contains(mixed $value, ?callable $comparer = null): bool
181
        {
182
                return $this->arrayContains($value, $comparer);
1✔
183
        }
184

185
        /**
186
         * @param TKey $key
187
         * @param null|callable(TKey, TKey): bool $comparer
188
         */
189
        public function containsKey(mixed $key, ?callable $comparer = null): bool
190
        {
191
                return $this->arrayContainsKey($key, $comparer);
54✔
192
        }
193

194
        /**
195
         * @template TDefault
196
         *
197
         * @param TDefault $defaultValue
198
         * @param null|callable(TValue, TKey): bool $predicate
199
         *
200
         * @return TDefault|TValue
201
         */
202
        public function firstOrDefault(mixed $defaultValue, ?callable $predicate = null): mixed
203
        {
204
                /** @var TValue|TDefault */
205
                return $this->genericFirstOrDefault($defaultValue, $predicate);
1✔
206
        }
207

208
        public function count(?callable $predicate = null): int {
209
                if ($predicate === null) {
13✔
210
                        return $this->arrayCount();
13✔
211
                }
212

213
                return $this->genericCount($predicate);
×
214
        }
215
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc