diff --git a/src/ArrayList.php b/src/ArrayList.php index fe03e5a..235fcff 100644 --- a/src/ArrayList.php +++ b/src/ArrayList.php @@ -127,6 +127,7 @@ public function set(int $index, mixed $value): void throw new OffsetNotFoundException($index); } + /** @psalm-suppress ImpureMethodCall */ return $this->list[$index]; } @@ -148,7 +149,7 @@ public function addAll(iterable $values): void } } - #[Pure] public function first(?callable $filter = null): mixed + public function first(?callable $filter = null): mixed { foreach ($this->list as $item) { if ($filter === null || $filter($item)) { @@ -163,7 +164,7 @@ public function addAll(iterable $values): void * @param callable(T): bool $filter * @return ArrayList */ - #[Pure] public function where(callable $filter): ArrayList + public function where(callable $filter): ArrayList { $result = new ArrayList(); @@ -196,13 +197,12 @@ public function addAll(iterable $values): void * @param callable(T): TOut $callback * @return ArrayList */ - #[Pure] public function map(callable $callback): ArrayList + public function map(callable $callback): ArrayList { $arr = new ArrayList(); foreach ($this->list as $value) { /** - * @psalm-suppress ImpureMethodCall * @psalm-suppress InvalidArgument Until vimeo/psalm#6821 is fixed */ $arr->add($callback($value)); @@ -211,7 +211,7 @@ public function addAll(iterable $values): void return $arr; } - #[Pure] public function any(?callable $filter = null): bool + public function any(?callable $filter = null): bool { return !$this->isEmpty() && $this->first($filter) !== null; } @@ -275,7 +275,7 @@ public function unshift(mixed $value): void array_unshift($this->list, $value); } - #[Pure] public function contains(mixed $value): bool + public function contains(mixed $value): bool { return $this->any(static fn($item) => $item === $value); } @@ -292,7 +292,7 @@ public function orderBy(callable $callback): ArrayList return $this; } - #[Pure] public function join(Stringable|string $separator): string + public function join(Stringable|string $separator): string { return implode((string)$separator, $this->map(fn($item) => (string)$item)->asArray()); } diff --git a/src/ArrayMap.php b/src/ArrayMap.php index 43a9b77..cd83afd 100644 --- a/src/ArrayMap.php +++ b/src/ArrayMap.php @@ -90,7 +90,7 @@ public function put(mixed $key, mixed $value): void return $this->values[$key]; } - #[Pure] public function first(?callable $filter = null): mixed + public function first(?callable $filter = null): mixed { foreach ($this->values as $key => $value) { if ($filter === null || $filter($value, $key)) { @@ -101,7 +101,7 @@ public function put(mixed $key, mixed $value): void return null; } - #[Pure] public function firstKey(?callable $filter = null): mixed + public function firstKey(?callable $filter = null): mixed { foreach ($this->values as $key => $value) { if ($filter === null || $filter($key, $value)) { @@ -117,7 +117,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): bool $filter * @return ArrayMap */ - #[Pure] public function where(callable $filter): ArrayMap + public function where(callable $filter): ArrayMap { $result = new ArrayMap(); @@ -135,7 +135,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TKey, TValue): bool $filter * @return ArrayMap */ - #[Pure] public function whereKey(callable $filter): ArrayMap + public function whereKey(callable $filter): ArrayMap { $result = new ArrayMap(); @@ -160,7 +160,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return ArrayMap */ - #[Pure] public function map(callable $callback): ArrayMap + public function map(callable $callback): ArrayMap { $map = new ArrayMap(); @@ -175,12 +175,12 @@ public function put(mixed $key, mixed $value): void return $map; } - #[Pure] public function any(?callable $filter = null): bool + public function any(?callable $filter = null): bool { return $this->first($filter) !== null; } - #[Pure] public function anyKey(?callable $filter = null): bool + public function anyKey(?callable $filter = null): bool { return $this->firstKey($filter) !== null; } @@ -201,7 +201,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return ArrayList */ - #[Pure] public function reduce(callable $callback): ArrayList + public function reduce(callable $callback): ArrayList { /** @var ArrayList $list */ $list = new ArrayList(); @@ -219,7 +219,7 @@ public function asArray(): array return $this->values; } - #[Pure] public function contains(mixed $value): bool + public function contains(mixed $value): bool { return $this->any(static fn($item) => $item === $value); } @@ -243,14 +243,13 @@ public function toJson(int $flags = 0): string * @param callable(TKey, TValue): TKeyOut $callback * @return ArrayMap */ - #[Pure] public function mapKeys(callable $callback): ArrayMap + public function mapKeys(callable $callback): ArrayMap { /** @var ArrayMap $map */ $map = new ArrayMap(); foreach ($this->values as $key => $value) { /** - * @psalm-suppress ImpureMethodCall * @psalm-suppress InvalidArgument Until vimeo/psalm#6821 is fixed */ $map->put($callback($key, $value), $value); diff --git a/src/Contract/Filterable.php b/src/Contract/Filterable.php index d73636e..7b3148d 100644 --- a/src/Contract/Filterable.php +++ b/src/Contract/Filterable.php @@ -3,8 +3,6 @@ namespace Elephox\Collection\Contract; -use JetBrains\PhpStorm\Pure; - /** * @template T */ @@ -14,23 +12,23 @@ interface Filterable * @param null|callable(T): bool $filter * @return T|null */ - #[Pure] public function first(?callable $filter = null): mixed; + public function first(?callable $filter = null): mixed; /** * @param null|callable(T): bool $filter * @return bool */ - #[Pure] public function any(?callable $filter = null): bool; + public function any(?callable $filter = null): bool; /** * @param callable(T): bool $filter * @return GenericCollection */ - #[Pure] public function where(callable $filter): GenericCollection; + public function where(callable $filter): GenericCollection; /** * @param T $value * @return bool */ - #[Pure] public function contains(mixed $value): bool; + public function contains(mixed $value): bool; } diff --git a/src/Contract/GenericCollection.php b/src/Contract/GenericCollection.php index 0606bfa..0401c4b 100644 --- a/src/Contract/GenericCollection.php +++ b/src/Contract/GenericCollection.php @@ -11,6 +11,6 @@ * @extends Filterable * @extends Mappable */ -interface GenericCollection extends Filterable, Mappable, DeepCloneable +interface GenericCollection extends DeepCloneable { } diff --git a/src/Contract/ReadonlyList.php b/src/Contract/ReadonlyList.php index e60e066..785d497 100644 --- a/src/Contract/ReadonlyList.php +++ b/src/Contract/ReadonlyList.php @@ -18,7 +18,7 @@ * @extends iterable * @extends list */ -interface ReadonlyList extends GenericCollection, Countable, IteratorAggregate, ArrayConvertible +interface ReadonlyList extends GenericCollection, Filterable, Mappable, Countable, IteratorAggregate, ArrayConvertible { /** * @return T @@ -29,7 +29,7 @@ interface ReadonlyList extends GenericCollection, Countable, IteratorAggregate, * @param callable(T): bool $filter * @return GenericList */ - #[Pure] public function where(callable $filter): GenericList; + public function where(callable $filter): GenericList; /** * @template TOut @@ -37,11 +37,11 @@ interface ReadonlyList extends GenericCollection, Countable, IteratorAggregate, * @param callable(T): TOut $callback * @return GenericList */ - #[Pure] public function map(callable $callback): GenericList; + public function map(callable $callback): GenericList; #[Pure] public function isEmpty(): bool; - #[Pure] public function join(string|Stringable $separator): string; + public function join(string|Stringable $separator): string; /** * @return list Returns this object in its array representation. diff --git a/src/Contract/ReadonlyMap.php b/src/Contract/ReadonlyMap.php index d9d1183..7e2b818 100644 --- a/src/Contract/ReadonlyMap.php +++ b/src/Contract/ReadonlyMap.php @@ -13,31 +13,31 @@ * @extends GenericCollection * @extends IteratorAggregate */ -interface ReadonlyMap extends GenericCollection, IteratorAggregate +interface ReadonlyMap extends GenericCollection, Filterable, Mappable, IteratorAggregate { /** * @param TKey $key * @return TValue */ - #[Pure] public function get(mixed $key): mixed; + public function get(mixed $key): mixed; /** * @param TKey $key */ - #[Pure] public function has(mixed $key): bool; + public function has(mixed $key): bool; /** * @param callable(TValue, TKey): bool $filter * @return GenericMap */ - #[Pure] public function where(callable $filter): GenericMap; + public function where(callable $filter): GenericMap; /** * @param callable(TKey, TValue): bool $filter * @return GenericMap */ - #[Pure] public function whereKey(callable $filter): GenericMap; + public function whereKey(callable $filter): GenericMap; /** * @template TOut @@ -45,7 +45,7 @@ interface ReadonlyMap extends GenericCollection, IteratorAggregate * @param callable(TValue, TKey): TOut $callback * @return GenericMap */ - #[Pure] public function map(callable $callback): GenericMap; + public function map(callable $callback): GenericMap; /** * @template TKeyOut @@ -53,7 +53,7 @@ interface ReadonlyMap extends GenericCollection, IteratorAggregate * @param callable(TKey, TValue): TKeyOut $callback * @return GenericMap */ - #[Pure] public function mapKeys(callable $callback): GenericMap; + public function mapKeys(callable $callback): GenericMap; /** * @template TOut @@ -61,37 +61,37 @@ interface ReadonlyMap extends GenericCollection, IteratorAggregate * @param callable(TValue, TKey): TOut $callback * @return GenericList */ - #[Pure] public function reduce(callable $callback): GenericList; + public function reduce(callable $callback): GenericList; /** * @param null|callable(TValue, TKey): bool $filter * @return TValue|null */ - #[Pure] public function first(?callable $filter = null): mixed; + public function first(?callable $filter = null): mixed; /** * @param null|callable(TKey, TValue): bool $filter * @return TKey|null */ - #[Pure] public function firstKey(?callable $filter = null): mixed; + public function firstKey(?callable $filter = null): mixed; /** * @param null|callable(TValue, TKey): bool $filter * @return bool */ - #[Pure] public function any(?callable $filter = null): bool; + public function any(?callable $filter = null): bool; /** * @param null|callable(TKey, TValue): bool $filter * @return bool */ - #[Pure] public function anyKey(?callable $filter = null): bool; + public function anyKey(?callable $filter = null): bool; /** * @param TValue $value * @return bool */ - #[Pure] public function contains(mixed $value): bool; + public function contains(mixed $value): bool; /** * @return GenericList diff --git a/src/Contract/ReadonlySet.php b/src/Contract/ReadonlySet.php index 4ce4889..de1391b 100644 --- a/src/Contract/ReadonlySet.php +++ b/src/Contract/ReadonlySet.php @@ -13,4 +13,9 @@ */ interface ReadonlySet extends GenericCollection, IteratorAggregate { + /** + * @param T $value + * @return bool + */ + public function contains(mixed $value): bool; } diff --git a/src/GenericWeakMap.php b/src/GenericWeakMap.php index 5d3b3a3..8b829db 100644 --- a/src/GenericWeakMap.php +++ b/src/GenericWeakMap.php @@ -66,7 +66,7 @@ public function put(mixed $key, mixed $value): void return $this->map->offsetGet($key); } - #[Pure] public function first(?callable $filter = null): mixed + public function first(?callable $filter = null): mixed { /** * @psalm-suppress ImpureMethodCall @@ -82,7 +82,7 @@ public function put(mixed $key, mixed $value): void return null; } - #[Pure] public function firstKey(?callable $filter = null): mixed + public function firstKey(?callable $filter = null): mixed { /** * @psalm-suppress ImpureMethodCall @@ -102,7 +102,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): bool $filter * @return GenericWeakMap */ - #[Pure] public function where(callable $filter): GenericWeakMap + public function where(callable $filter): GenericWeakMap { /** @var GenericWeakMap $result */ $result = new GenericWeakMap(); @@ -125,7 +125,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TKey, TValue): bool $filter * @return GenericWeakMap */ - #[Pure] public function whereKey(callable $filter): GenericWeakMap + public function whereKey(callable $filter): GenericWeakMap { /** @var GenericWeakMap $result */ $result = new GenericWeakMap(); @@ -161,7 +161,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return GenericWeakMap */ - #[Pure] public function map(callable $callback): GenericWeakMap + public function map(callable $callback): GenericWeakMap { /** @var GenericWeakMap $map */ $map = new GenericWeakMap(); @@ -185,7 +185,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TKey, TValue): TKeyOut $callback * @return GenericWeakMap */ - #[Pure] public function mapKeys(callable $callback): GenericWeakMap + public function mapKeys(callable $callback): GenericWeakMap { /** @var GenericWeakMap $map */ $map = new GenericWeakMap(); @@ -208,7 +208,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return ArrayList */ - #[Pure] public function reduce(callable $callback): ArrayList + public function reduce(callable $callback): ArrayList { /** @var ArrayList $list */ $list = new ArrayList(); @@ -225,12 +225,12 @@ public function put(mixed $key, mixed $value): void return $list; } - #[Pure] public function any(?callable $filter = null): bool + public function any(?callable $filter = null): bool { return $this->first($filter) !== null; } - #[Pure] public function anyKey(?callable $filter = null): bool + public function anyKey(?callable $filter = null): bool { return $this->firstKey($filter) !== null; } @@ -275,7 +275,7 @@ public function put(mixed $key, mixed $value): void return $list; } - #[Pure] public function contains(mixed $value): bool + public function contains(mixed $value): bool { return $this->any(static fn($item) => $item === $value); } diff --git a/src/ObjectMap.php b/src/ObjectMap.php index 68232b9..00b95d7 100644 --- a/src/ObjectMap.php +++ b/src/ObjectMap.php @@ -51,7 +51,7 @@ public function put(mixed $key, mixed $value): void * @param object $key * @return TValue */ - #[Pure] public function get(mixed $key): mixed + public function get(mixed $key): mixed { /** @psalm-suppress ImpureMethodCall */ if (!$this->map->offsetExists($key)) { @@ -65,7 +65,7 @@ public function put(mixed $key, mixed $value): void return $this->map->offsetGet($key); } - #[Pure] public function first(?callable $filter = null): mixed + public function first(?callable $filter = null): mixed { /** * @psalm-suppress ImpureMethodCall @@ -82,7 +82,7 @@ public function put(mixed $key, mixed $value): void return null; } - #[Pure] public function firstKey(?callable $filter = null): mixed + public function firstKey(?callable $filter = null): mixed { /** * @psalm-suppress ImpureMethodCall @@ -106,7 +106,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): bool $filter * @return ObjectMap */ - #[Pure] public function where(callable $filter): ObjectMap + public function where(callable $filter): ObjectMap { /** @var ObjectMap $result */ $result = new ObjectMap(); @@ -130,7 +130,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TKey, TValue): bool $filter * @return ObjectMap */ - #[Pure] public function whereKey(callable $filter): ObjectMap + public function whereKey(callable $filter): ObjectMap { /** @var ObjectMap $result */ $result = new ObjectMap(); @@ -155,7 +155,7 @@ public function put(mixed $key, mixed $value): void * * @return bool */ - #[Pure] public function has(mixed $key): bool + public function has(mixed $key): bool { /** @psalm-suppress ImpureMethodCall */ return $this->map->offsetExists($key); @@ -167,7 +167,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return ObjectMap */ - #[Pure] public function map(callable $callback): ObjectMap + public function map(callable $callback): ObjectMap { /** @var ObjectMap $map */ $map = new ObjectMap(); @@ -193,7 +193,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TKey, TValue): TKeyOut $callback * @return ObjectMap */ - #[Pure] public function mapKeys(callable $callback): ObjectMap + public function mapKeys(callable $callback): ObjectMap { /** @var ObjectMap $map */ $map = new ObjectMap(); @@ -219,7 +219,7 @@ public function put(mixed $key, mixed $value): void * @param callable(TValue, TKey): TOut $callback * @return ArrayList */ - #[Pure] public function reduce(callable $callback): ArrayList + public function reduce(callable $callback): ArrayList { /** @var ArrayList $list */ $list = new ArrayList(); @@ -238,12 +238,12 @@ public function put(mixed $key, mixed $value): void return $list; } - #[Pure] public function any(?callable $filter = null): bool + public function any(?callable $filter = null): bool { return $this->first($filter) !== null; } - #[Pure] public function anyKey(?callable $filter = null): bool + public function anyKey(?callable $filter = null): bool { return $this->firstKey($filter) !== null; } @@ -283,7 +283,7 @@ public function put(mixed $key, mixed $value): void return $list; } - #[Pure] public function contains(mixed $value): bool + public function contains(mixed $value): bool { return $this->any(static fn($item) => $item === $value); } diff --git a/src/ObjectSet.php b/src/ObjectSet.php index f3e0487..84d458e 100644 --- a/src/ObjectSet.php +++ b/src/ObjectSet.php @@ -4,12 +4,10 @@ namespace Elephox\Collection; use Closure; -use Elephox\Collection\Contract\GenericCollection; use Elephox\Collection\Contract\GenericSet; use Elephox\PIE\DefaultEqualityComparer; use Elephox\Support\DeepCloneable; use InvalidArgumentException; -use JetBrains\PhpStorm\Pure; use SplObjectStorage; /** @@ -36,21 +34,6 @@ public function getIterator(): SplObjectStorageIterator return new SplObjectStorageIterator($this->storage); } - #[Pure] public function first(?callable $filter = null): mixed - { - // TODO: Implement first() method. - } - - #[Pure] public function any(?callable $filter = null): bool - { - // TODO: Implement any() method. - } - - #[Pure] public function where(callable $filter): GenericCollection - { - // TODO: Implement where() method. - } - public function contains(mixed $value): bool { if (!is_object($value)) { @@ -85,9 +68,4 @@ public function remove(mixed $value): bool return $existed; } - - public function map(callable $callback): GenericCollection - { - // TODO: Implement map() method. - } }