Skip to content

Commit

Permalink
fix: Equals operation, fix typing information.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 10, 2022
1 parent 5a91d8a commit eff8102
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
* @template-extends DropWhileable<TKey, T>
* @template-extends Dumpable<TKey, T>
* @template-extends Duplicateable<TKey, T>
* @template-extends Equalsable<TKey, T>
* @template-extends Everyable<TKey, T>
* @template-extends Explodeable<TKey, T>
* @template-extends Falsyable<TKey, T>
Expand Down
8 changes: 1 addition & 7 deletions src/Contract/Operation/Equalsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;

/**
* @template TKey
* @template T
*/
interface Equalsable
{
/**
Expand All @@ -27,7 +21,7 @@ interface Equalsable
*
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#equals
*
* @param iterable<TKey, T> $other
* @param iterable<mixed, mixed> $other
*/
public function equals(iterable $other): bool;
}
15 changes: 6 additions & 9 deletions src/Operation/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,26 @@
/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Equals extends AbstractOperation
{
/**
* @return Closure(iterable<TKey, T>): Closure(iterable<TKey, T>): Generator<int|TKey, bool>
* @return Closure(iterable<mixed, mixed>): Closure(iterable<mixed, mixed>): Generator<int, bool, mixed, false|mixed>
*/
public function __invoke(): Closure
{
return
/**
* @param iterable<TKey, T> $other
* @param iterable<mixed, mixed> $other
*
* @return Closure(iterable<TKey, T>): Generator<int|TKey, bool>
* @return Closure(iterable<mixed, mixed>): Generator<int, bool, mixed, false|mixed>
*/
static function (iterable $other): Closure {
/**
* @param iterable<TKey, T> $iterable
* @param iterable<mixed, mixed> $iterable
*
* @return Generator<int|TKey, bool>
* @return Generator<int, bool, mixed, false|mixed>
*/
return static function (iterable $iterable) use ($other): Generator {
$otherAggregate = new IterableIteratorAggregate($other);
Expand All @@ -58,7 +55,7 @@ static function (iterable $other): Closure {

$containsCallback =
/**
* @param T $current
* @param mixed $current
*/
static fn (int $index, $current): bool => (new Contains())()($current)($otherAggregate)->current();

Expand Down
2 changes: 1 addition & 1 deletion tests/static-analysis/equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function equals_check(bool $value): void

/** @psalm-suppress InvalidArgument -> Psalm narrows the types to 1|2|3 and 4|5 and knows these cannot work */
equals_check(Collection::fromIterable([1, 2, 3])->equals(Collection::fromIterable([4, 5])));
/** @psalm-suppress InvalidArgument @phpstan-ignore-next-line */
/** @psalm-suppress InvalidArgument */
equals_check(Collection::fromIterable([1, 2, 3])->equals(Collection::fromIterable(['a', 'b'])));
/** @psalm-suppress InvalidArgument -> Psalm sees the keys and values are completely different */
equals_check(Collection::fromIterable(['foo' => 'f', 'bar' => 'b'])->equals(Collection::fromIterable(['other' => 'x'])));
Expand Down

0 comments on commit eff8102

Please sign in to comment.