Skip to content

Commit

Permalink
Fixed vimeo/psalm#7196 and updated pure annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Jan 6, 2022
1 parent 07f93e1 commit 7742dde
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 86 deletions.
14 changes: 7 additions & 7 deletions src/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function set(int $index, mixed $value): void
throw new OffsetNotFoundException($index);
}

/** @psalm-suppress ImpureMethodCall */
return $this->list[$index];
}

Expand All @@ -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)) {
Expand All @@ -163,7 +164,7 @@ public function addAll(iterable $values): void
* @param callable(T): bool $filter
* @return ArrayList<T>
*/
#[Pure] public function where(callable $filter): ArrayList
public function where(callable $filter): ArrayList
{
$result = new ArrayList();

Expand Down Expand Up @@ -196,13 +197,12 @@ public function addAll(iterable $values): void
* @param callable(T): TOut $callback
* @return ArrayList<TOut>
*/
#[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));
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}
Expand Down
21 changes: 10 additions & 11 deletions src/ArrayMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -117,7 +117,7 @@ public function put(mixed $key, mixed $value): void
* @param callable(TValue, TKey): bool $filter
* @return ArrayMap<TKey, TValue>
*/
#[Pure] public function where(callable $filter): ArrayMap
public function where(callable $filter): ArrayMap
{
$result = new ArrayMap();

Expand All @@ -135,7 +135,7 @@ public function put(mixed $key, mixed $value): void
* @param callable(TKey, TValue): bool $filter
* @return ArrayMap<TKey, TValue>
*/
#[Pure] public function whereKey(callable $filter): ArrayMap
public function whereKey(callable $filter): ArrayMap
{
$result = new ArrayMap();

Expand All @@ -160,7 +160,7 @@ public function put(mixed $key, mixed $value): void
* @param callable(TValue, TKey): TOut $callback
* @return ArrayMap<TKey, TOut>
*/
#[Pure] public function map(callable $callback): ArrayMap
public function map(callable $callback): ArrayMap
{
$map = new ArrayMap();

Expand All @@ -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;
}
Expand All @@ -201,7 +201,7 @@ public function put(mixed $key, mixed $value): void
* @param callable(TValue, TKey): TOut $callback
* @return ArrayList<TOut>
*/
#[Pure] public function reduce(callable $callback): ArrayList
public function reduce(callable $callback): ArrayList
{
/** @var ArrayList<TOut> $list */
$list = new ArrayList();
Expand All @@ -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);
}
Expand All @@ -243,14 +243,13 @@ public function toJson(int $flags = 0): string
* @param callable(TKey, TValue): TKeyOut $callback
* @return ArrayMap<TKeyOut, TValue>
*/
#[Pure] public function mapKeys(callable $callback): ArrayMap
public function mapKeys(callable $callback): ArrayMap
{
/** @var ArrayMap<TKeyOut, TValue> $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);
Expand Down
10 changes: 4 additions & 6 deletions src/Contract/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Elephox\Collection\Contract;

use JetBrains\PhpStorm\Pure;

/**
* @template T
*/
Expand All @@ -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<T>
*/
#[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;
}
2 changes: 1 addition & 1 deletion src/Contract/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
* @extends Filterable<T>
* @extends Mappable<T>
*/
interface GenericCollection extends Filterable, Mappable, DeepCloneable
interface GenericCollection extends DeepCloneable
{
}
8 changes: 4 additions & 4 deletions src/Contract/ReadonlyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @extends iterable<T>
* @extends list<T>
*/
interface ReadonlyList extends GenericCollection, Countable, IteratorAggregate, ArrayConvertible
interface ReadonlyList extends GenericCollection, Filterable, Mappable, Countable, IteratorAggregate, ArrayConvertible
{
/**
* @return T
Expand All @@ -29,19 +29,19 @@ interface ReadonlyList extends GenericCollection, Countable, IteratorAggregate,
* @param callable(T): bool $filter
* @return GenericList<T>
*/
#[Pure] public function where(callable $filter): GenericList;
public function where(callable $filter): GenericList;

/**
* @template TOut
*
* @param callable(T): TOut $callback
* @return GenericList<TOut>
*/
#[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<T> Returns this object in its array representation.
Expand Down
26 changes: 13 additions & 13 deletions src/Contract/ReadonlyMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,85 @@
* @extends GenericCollection<TValue>
* @extends IteratorAggregate<TKey, TValue>
*/
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<TKey, TValue>
*/
#[Pure] public function where(callable $filter): GenericMap;
public function where(callable $filter): GenericMap;


/**
* @param callable(TKey, TValue): bool $filter
* @return GenericMap<TKey, TValue>
*/
#[Pure] public function whereKey(callable $filter): GenericMap;
public function whereKey(callable $filter): GenericMap;

/**
* @template TOut
*
* @param callable(TValue, TKey): TOut $callback
* @return GenericMap<TKey, TOut>
*/
#[Pure] public function map(callable $callback): GenericMap;
public function map(callable $callback): GenericMap;

/**
* @template TKeyOut
*
* @param callable(TKey, TValue): TKeyOut $callback
* @return GenericMap<TKeyOut, TValue>
*/
#[Pure] public function mapKeys(callable $callback): GenericMap;
public function mapKeys(callable $callback): GenericMap;

/**
* @template TOut
*
* @param callable(TValue, TKey): TOut $callback
* @return GenericList<TOut>
*/
#[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<TValue>
Expand Down
5 changes: 5 additions & 0 deletions src/Contract/ReadonlySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
*/
interface ReadonlySet extends GenericCollection, IteratorAggregate
{
/**
* @param T $value
* @return bool
*/
public function contains(mixed $value): bool;
}

0 comments on commit 7742dde

Please sign in to comment.