Innmind /
Immutable
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Innmind\Immutable\Map; |
||
| 5 | |||
| 6 | use Innmind\Immutable\{ |
||
| 7 | Map, |
||
| 8 | Str, |
||
| 9 | Sequence, |
||
| 10 | Set, |
||
| 11 | Pair, |
||
| 12 | Maybe, |
||
| 13 | SideEffect, |
||
| 14 | }; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @template T |
||
| 18 | * @template S |
||
| 19 | * @psalm-immutable |
||
| 20 | */ |
||
| 21 | final class ObjectKeys implements Implementation |
||
| 22 | { |
||
| 23 | private \SplObjectStorage $values; |
||
| 24 | |||
| 25 | public function __construct(\SplObjectStorage $values = null) |
||
| 26 | { |
||
| 27 | $this->values = $values ?? new \SplObjectStorage; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param T $key |
||
|
0 ignored issues
–
show
|
|||
| 32 | 30 | * @param S $value |
|
| 33 | * |
||
| 34 | 30 | * @return Implementation<T, S> |
|
| 35 | */ |
||
| 36 | 30 | public function __invoke($key, $value): Implementation |
|
| 37 | 1 | { |
|
| 38 | if (!\is_object($key)) { |
||
| 39 | return (new DoubleIndex)->merge($this)($key, $value); |
||
| 40 | 29 | } |
|
| 41 | 29 | ||
| 42 | 29 | /** @var \SplObjectStorage<object, mixed> */ |
|
| 43 | 29 | $values = clone $this->values; |
|
| 44 | 29 | /** @psalm-suppress ImpureMethodCall */ |
|
| 45 | $values[$key] = $value; |
||
| 46 | 9 | ||
| 47 | return new self($values); |
||
| 48 | 9 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | 8 | * @template A |
|
| 52 | * @template B |
||
| 53 | 8 | * @psalm-pure |
|
| 54 | * |
||
| 55 | * @param A $key |
||
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\A was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 56 | 7 | * @param B $value |
|
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\B was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 57 | * |
||
| 58 | 7 | * @return Maybe<Implementation<A, B>> |
|
| 59 | */ |
||
| 60 | public static function of($key, $value): Maybe |
||
| 61 | { |
||
| 62 | if (\is_object($key)) { |
||
| 63 | /** @var self<A, B> */ |
||
| 64 | $self = new self; |
||
| 65 | |||
| 66 | /** @var Maybe<Implementation<A, B>> */ |
||
| 67 | return Maybe::just(($self)($key, $value)); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** @var Maybe<Implementation<A, B>> */ |
||
| 71 | return Maybe::nothing(); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function size(): int |
||
| 75 | 25 | { |
|
| 76 | /** @psalm-suppress ImpureMethodCall */ |
||
| 77 | 25 | return $this->values->count(); |
|
| 78 | 24 | } |
|
| 79 | |||
| 80 | 23 | public function count(): int |
|
| 81 | 23 | { |
|
| 82 | return $this->size(); |
||
| 83 | 23 | } |
|
| 84 | |||
| 85 | 23 | /** |
|
| 86 | * @param T $key |
||
| 87 | * |
||
| 88 | * @return Maybe<S> |
||
| 89 | */ |
||
| 90 | public function get($key): Maybe |
||
| 91 | { |
||
| 92 | if (!$this->contains($key)) { |
||
| 93 | /** @var Maybe<S> */ |
||
| 94 | return Maybe::nothing(); |
||
| 95 | 7 | } |
|
| 96 | |||
| 97 | 7 | /** |
|
| 98 | 1 | * @psalm-suppress MixedArgumentTypeCoercion |
|
| 99 | * @psalm-suppress ImpureMethodCall |
||
| 100 | * @var Maybe<S> |
||
| 101 | */ |
||
| 102 | return Maybe::just($this->values->offsetGet($key)); |
||
| 103 | } |
||
| 104 | |||
| 105 | 6 | /** |
|
| 106 | * @param T $key |
||
| 107 | */ |
||
| 108 | public function contains($key): bool |
||
| 109 | { |
||
| 110 | if (!\is_object($key)) { |
||
| 111 | 10 | return false; |
|
| 112 | } |
||
| 113 | 10 | ||
| 114 | 1 | /** |
|
| 115 | * @psalm-suppress MixedArgumentTypeCoercion |
||
| 116 | * @psalm-suppress ImpureMethodCall |
||
| 117 | */ |
||
| 118 | 10 | return $this->values->offsetExists($key); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return self<T, S> |
||
| 123 | */ |
||
| 124 | 5 | public function clear(): self |
|
| 125 | { |
||
| 126 | 5 | return new self; |
|
| 127 | 5 | } |
|
| 128 | |||
| 129 | 5 | /** |
|
| 130 | * @param Implementation<T, S> $map |
||
| 131 | */ |
||
| 132 | public function equals(Implementation $map): bool |
||
| 133 | { |
||
| 134 | if (!$map->keys()->equals($this->keys())) { |
||
| 135 | 3 | return false; |
|
| 136 | } |
||
| 137 | 3 | ||
| 138 | 1 | /** @psalm-suppress ImpureMethodCall */ |
|
| 139 | foreach ($this->values as $k) { |
||
| 140 | /** @var T $key */ |
||
| 141 | 3 | $key = $k; |
|
| 142 | /** @var S $v */ |
||
| 143 | 3 | $v = $this->values[$k]; |
|
| 144 | $equals = $map |
||
| 145 | 3 | ->get($key) |
|
| 146 | ->filter(static fn($value) => $value === $v) |
||
| 147 | 3 | ->match( |
|
| 148 | static fn() => true, |
||
| 149 | static fn() => false, |
||
| 150 | ); |
||
| 151 | 3 | ||
| 152 | 3 | if (!$equals) { |
|
| 153 | return false; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | 1 | ||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param callable(T, S): bool $predicate |
||
| 162 | * |
||
| 163 | * @return self<T, S> |
||
| 164 | 1 | */ |
|
| 165 | public function filter(callable $predicate): self |
||
| 166 | 1 | { |
|
| 167 | /** @var \SplObjectStorage<object, mixed> */ |
||
| 168 | 1 | $values = new \SplObjectStorage; |
|
| 169 | |||
| 170 | 1 | /** @psalm-suppress ImpureMethodCall */ |
|
| 171 | foreach ($this->values as $k) { |
||
| 172 | 1 | /** @var T $key */ |
|
| 173 | $key = $k; |
||
| 174 | 1 | /** @var S $v */ |
|
| 175 | 1 | $v = $this->values[$k]; |
|
| 176 | |||
| 177 | if ($predicate($key, $v) === true) { |
||
| 178 | $values[$k] = $v; |
||
| 179 | 1 | } |
|
| 180 | } |
||
| 181 | |||
| 182 | return new self($values); |
||
| 183 | } |
||
| 184 | |||
| 185 | 1 | /** |
|
| 186 | * @param callable(T, S): void $function |
||
| 187 | 1 | */ |
|
| 188 | public function foreach(callable $function): SideEffect |
||
| 189 | 1 | { |
|
| 190 | /** @psalm-suppress ImpureMethodCall */ |
||
| 191 | 1 | foreach ($this->values as $k) { |
|
| 192 | /** @var T $key */ |
||
| 193 | 1 | $key = $k; |
|
| 194 | /** |
||
| 195 | 1 | * @psalm-suppress ImpureMethodCall |
|
| 196 | * @var S $v |
||
| 197 | */ |
||
| 198 | $v = $this->values[$k]; |
||
| 199 | |||
| 200 | $function($key, $v); |
||
| 201 | } |
||
| 202 | |||
| 203 | return new SideEffect; |
||
| 204 | } |
||
| 205 | 2 | ||
| 206 | /** |
||
| 207 | 2 | * @template D |
|
| 208 | 1 | * |
|
| 209 | * @param callable(T, S): D $discriminator |
||
| 210 | * |
||
| 211 | 1 | * @return Map<D, Map<T, S>> |
|
| 212 | */ |
||
| 213 | 1 | public function groupBy(callable $discriminator): Map |
|
| 214 | { |
||
| 215 | 1 | /** @var Map<D, Map<T, S>> */ |
|
| 216 | $groups = Map::of(); |
||
| 217 | 1 | ||
| 218 | /** @psalm-suppress ImpureMethodCall */ |
||
| 219 | 1 | foreach ($this->values as $k) { |
|
| 220 | /** @var T $key */ |
||
| 221 | 1 | $key = $k; |
|
| 222 | /** |
||
| 223 | 1 | * @psalm-suppress ImpureMethodCall |
|
| 224 | 1 | * @var S |
|
| 225 | 1 | */ |
|
| 226 | $v = $this->values[$k]; |
||
| 227 | |||
| 228 | $discriminant = $discriminator($key, $v); |
||
| 229 | 1 | ||
| 230 | $group = $groups->get($discriminant)->match( |
||
| 231 | 1 | static fn($group) => $group, |
|
| 232 | fn() => $this->clearMap(), |
||
| 233 | 1 | ); |
|
| 234 | $groups = ($groups)($discriminant, ($group)($key, $v)); |
||
| 235 | 1 | } |
|
| 236 | |||
| 237 | /** @var Map<D, Map<T, S>> */ |
||
| 238 | 1 | return $groups; |
|
| 239 | } |
||
| 240 | 1 | ||
| 241 | /** |
||
| 242 | * @return Set<T> |
||
| 243 | */ |
||
| 244 | public function keys(): Set |
||
| 245 | 1 | { |
|
| 246 | return $this->reduce( |
||
| 247 | Set::of(), |
||
| 248 | static fn(Set $keys, $key): Set => ($keys)($key), |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | 7 | ||
| 252 | /** |
||
| 253 | 7 | * @return Sequence<S> |
|
| 254 | 7 | */ |
|
| 255 | public function values(): Sequence |
||
| 256 | 7 | { |
|
| 257 | 7 | return $this->reduce( |
|
| 258 | Sequence::of(), |
||
| 259 | static fn(Sequence $values, $_, $value): Sequence => ($values)($value), |
||
| 260 | ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | 7 | * @template B |
|
| 265 | * |
||
| 266 | 7 | * @param callable(T, S): B $function |
|
| 267 | 7 | * |
|
| 268 | * @return self<T, B> |
||
| 269 | 7 | */ |
|
| 270 | 7 | public function map(callable $function): self |
|
| 271 | { |
||
| 272 | /** @var \SplObjectStorage<object, mixed> */ |
||
| 273 | $values = new \SplObjectStorage; |
||
| 274 | |||
| 275 | /** @psalm-suppress ImpureMethodCall */ |
||
| 276 | foreach ($this->values as $k) { |
||
| 277 | /** @var T */ |
||
| 278 | $key = $k; |
||
| 279 | 3 | /** |
|
| 280 | * @psalm-suppress ImpureMethodCall |
||
| 281 | 3 | * @var S |
|
| 282 | */ |
||
| 283 | 3 | $v = $this->values[$k]; |
|
| 284 | |||
| 285 | 3 | /** @psalm-suppress ImpureMethodCall */ |
|
| 286 | $values[$k] = $function($key, $v); |
||
| 287 | 3 | } |
|
| 288 | |||
| 289 | 3 | return new self($values); |
|
| 290 | } |
||
| 291 | 3 | ||
| 292 | 2 | /** |
|
| 293 | * @param T $key |
||
| 294 | * |
||
| 295 | 1 | * @return self<T, S> |
|
| 296 | */ |
||
| 297 | 1 | public function remove($key): self |
|
| 298 | { |
||
| 299 | 2 | if (!$this->contains($key)) { |
|
| 300 | 2 | return $this; |
|
| 301 | } |
||
| 302 | |||
| 303 | 2 | /** @var \SplObjectStorage<object, mixed> */ |
|
| 304 | $values = clone $this->values; |
||
| 305 | 1 | /** |
|
| 306 | * @psalm-suppress MixedArgumentTypeCoercion |
||
| 307 | * @psalm-suppress ImpureMethodCall |
||
| 308 | 1 | */ |
|
| 309 | $values->detach($key); |
||
| 310 | /** @psalm-suppress ImpureMethodCall */ |
||
| 311 | $values->rewind(); |
||
| 312 | |||
| 313 | return new self($values); |
||
| 314 | } |
||
| 315 | |||
| 316 | 1 | /** |
|
| 317 | * @param Implementation<T, S> $map |
||
| 318 | 1 | * |
|
| 319 | 1 | * @return Implementation<T, S> |
|
| 320 | */ |
||
| 321 | public function merge(Implementation $map): Implementation |
||
| 322 | 1 | { |
|
| 323 | 1 | return $map->reduce( |
|
| 324 | $this, |
||
| 325 | 1 | static fn(Implementation $carry, $key, $value): Implementation => ($carry)($key, $value), |
|
| 326 | 1 | ); |
|
| 327 | } |
||
| 328 | 1 | ||
| 329 | /** |
||
| 330 | * @param callable(T, S): bool $predicate |
||
| 331 | * |
||
| 332 | * @return Map<bool, Map<T, S>> |
||
| 333 | */ |
||
| 334 | public function partition(callable $predicate): Map |
||
| 335 | { |
||
| 336 | 2 | $truthy = $this->clearMap(); |
|
| 337 | $falsy = $this->clearMap(); |
||
| 338 | |||
| 339 | 2 | /** @psalm-suppress ImpureMethodCall */ |
|
| 340 | 2 | foreach ($this->values as $k) { |
|
| 341 | /** @var T $key */ |
||
| 342 | 2 | $key = $k; |
|
| 343 | 2 | /** |
|
| 344 | * @psalm-suppress ImpureMethodCall |
||
| 345 | * @var S $v |
||
| 346 | */ |
||
| 347 | $v = $this->values[$k]; |
||
| 348 | |||
| 349 | $return = $predicate($key, $v); |
||
| 350 | |||
| 351 | if ($return === true) { |
||
| 352 | 1 | $truthy = ($truthy)($key, $v); |
|
| 353 | } else { |
||
| 354 | 1 | $falsy = ($falsy)($key, $v); |
|
| 355 | 1 | } |
|
| 356 | } |
||
| 357 | 1 | ||
| 358 | return Map::of([true, $truthy], [false, $falsy]); |
||
| 359 | 1 | } |
|
| 360 | |||
| 361 | 1 | /** |
|
| 362 | * @template R |
||
| 363 | 1 | * @param R $carry |
|
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\R was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 364 | * @param callable(R, T, S): R $reducer |
||
| 365 | 1 | * |
|
| 366 | 1 | * @return R |
|
| 367 | */ |
||
| 368 | 1 | public function reduce($carry, callable $reducer) |
|
| 369 | { |
||
| 370 | /** @psalm-suppress ImpureMethodCall */ |
||
| 371 | foreach ($this->values as $k) { |
||
| 372 | /** @var T $key */ |
||
| 373 | $key = $k; |
||
| 374 | /** |
||
| 375 | * @psalm-suppress ImpureMethodCall |
||
| 376 | 1 | * @var S $v |
|
| 377 | 1 | */ |
|
| 378 | 1 | $v = $this->values[$k]; |
|
| 379 | |||
| 380 | $carry = $reducer($carry, $key, $v); |
||
| 381 | } |
||
| 382 | |||
| 383 | return $carry; |
||
| 384 | } |
||
| 385 | |||
| 386 | public function empty(): bool |
||
| 387 | { |
||
| 388 | 8 | /** @psalm-suppress ImpureMethodCall */ |
|
| 389 | $this->values->rewind(); |
||
| 390 | 8 | ||
| 391 | /** @psalm-suppress ImpureMethodCall */ |
||
| 392 | 8 | return !$this->values->valid(); |
|
| 393 | } |
||
| 394 | 8 | ||
| 395 | public function find(callable $predicate): Maybe |
||
| 396 | 8 | { |
|
| 397 | /** @psalm-suppress ImpureMethodCall */ |
||
| 398 | foreach ($this->values as $k) { |
||
| 399 | 8 | /** @var T $key */ |
|
| 400 | $key = $k; |
||
| 401 | /** |
||
| 402 | 3 | * @psalm-suppress ImpureMethodCall |
|
| 403 | * @var S $v |
||
| 404 | 3 | */ |
|
| 405 | $v = $this->values[$k]; |
||
| 406 | 3 | ||
| 407 | if ($predicate($key, $v)) { |
||
| 408 | return Maybe::just(new Pair($key, $v)); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /** @var Maybe<Pair<T, S>> */ |
||
| 413 | return Maybe::nothing(); |
||
| 414 | } |
||
| 415 | |||
| 416 | 1 | /** |
|
| 417 | * @return Map<T, S> |
||
| 418 | */ |
||
| 419 | 1 | private function clearMap(): Map |
|
| 420 | { |
||
| 421 | 1 | return Map::of(); |
|
| 422 | } |
||
| 423 | } |
||
| 424 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths