Skip to content

Commit

Permalink
sa: Fix distinct tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 23, 2022
1 parent 0e12a06 commit edc521f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
56 changes: 27 additions & 29 deletions tests/static-analysis/distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;
use tests\loophp\collection\NamedItem;

/**
* @param CollectionInterface<int, int> $collection
Expand All @@ -19,7 +20,7 @@ function distinct_checkIntList(CollectionInterface $collection): void
{
}
/**
* @param CollectionInterface<int, stdClass> $collection
* @param CollectionInterface<int, NamedItem> $collection
*/
function distinct_checkObjectList(CollectionInterface $collection): void
{
Expand All @@ -30,32 +31,31 @@ function distinct_checkObjectList(CollectionInterface $collection): void
function distinct_checkMap(CollectionInterface $collection): void
{
}

$cat = static function (string $name): stdClass {
$instance = new stdClass();
$instance->name = $name;

return $instance;
};

$cats = [
$cat1 = $cat('izumi'),
$cat2 = $cat('nakano'),
$cat3 = $cat('booba'),
$cat3,
];

$accessor = static fn (stdClass $object): string => $object->name;
// TODO: Replace with a proper typed generator when it will be done.
$catGenerator =
/**
* @return Generator<int, NamedItem>
*/
static function (): Generator {
$names = ['izumi', 'nakano', 'booba'];

// @phpstan-ignore-next-line
while (true) {
yield new NamedItem($names[array_rand($names)]);
}
};

$accessor = static fn (NamedItem $object): string => $object->name();
$stringComparator = static fn (string $left): Closure => static fn (string $right): bool => $left === $right;
$objectComparator = static fn (stdClass $left): Closure => static fn (stdClass $right): bool => $left->name === $right->name;
$objectComparator = static fn (NamedItem $left): Closure => static fn (NamedItem $right): bool => $left->name() === $right->name();

distinct_checkIntList(Collection::fromIterable([11, 12, 11, 13])->distinct());
distinct_checkMap(Collection::fromIterable(['foo' => 'f', 'bar' => 'b', 'baz' => 'f'])->distinct());

distinct_checkObjectList(Collection::fromIterable($cats)->distinct());
distinct_checkObjectList(Collection::fromIterable($cats)->distinct($objectComparator));
distinct_checkObjectList(Collection::fromIterable($cats)->distinct($stringComparator, $accessor));
distinct_checkObjectList(Collection::fromIterable($cats)->distinct(null, $accessor));
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct());
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct($objectComparator));
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct($stringComparator, $accessor));
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct(null, $accessor));

// VALID failures

Expand All @@ -66,17 +66,15 @@ function distinct_checkMap(CollectionInterface $collection): void
distinct_checkMap(Collection::fromIterable(['foo' => 1, 'bar' => 2, 'baz' => 'f'])->distinct());

// mixing object comparator parameter types
$objectComparator = static fn (stdClass $left): Closure => static fn (string $right): bool => $left->name === $right;
$objectComparator = static fn (NamedItem $left): Closure => static fn (string $right): bool => $left->name() === $right;
/** @psalm-suppress InvalidArgument @phpstan-ignore-next-line */
distinct_checkObjectList(Collection::fromIterable($cats)->distinct($objectComparator));
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct($objectComparator));

// using wrong type parameter for accessor callback
$accessor = static fn (string $object): string => $object;
/** @psalm-suppress InvalidArgument */
distinct_checkObjectList(Collection::fromIterable($cats)->distinct(null, $accessor));
/** @psalm-suppress InvalidArgument @phpstan-ignore-next-line */
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct(null, $accessor));

// comparator parameter types need to match accessor return type
$accessor = static fn (stdClass $object): string => $object->name;
$objectComparator = static fn (stdClass $left): Closure => static fn (stdClass $right): bool => $left->name === $right->name;
/** @psalm-suppress InvalidArgument @phpstan-ignore-next-line */
distinct_checkObjectList(Collection::fromIterable($cats)->distinct($objectComparator, $accessor));
distinct_checkObjectList(Collection::fromIterable($catGenerator())->distinct($objectComparator, $accessor));
25 changes: 25 additions & 0 deletions tests/unit/NamedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace tests\loophp\collection;

final class NamedItem
{
private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function name(): string
{
return $this->name;
}
}

0 comments on commit edc521f

Please sign in to comment.