-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update distinct operation.
- Loading branch information
Showing
7 changed files
with
277 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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 App; | ||
|
||
use Closure; | ||
use loophp\collection\Collection; | ||
|
||
include __DIR__ . '/../../../../vendor/autoload.php'; | ||
|
||
// Example 1 -> Using the default callback, with scalar values | ||
$collection = Collection::fromIterable(['a', 'b', 'a', 'c']) | ||
->distinct(); // [0 => 'a', 1 => 'b', 3 => 'c'] | ||
|
||
// Example 2 -> Using one custom callback, with object values | ||
final class User | ||
{ | ||
private string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
$users = [ | ||
new User('foo'), | ||
new User('bar'), | ||
new User('foo'), | ||
new User('a'), | ||
]; | ||
|
||
$collection = Collection::fromIterable($users) | ||
->distinct( | ||
static fn (User $left): Closure => static fn (User $right): bool => $left->name() === $right->name() | ||
); // [0 => User<foo>, 1 => User<bar>, 3 => User<a>] | ||
|
||
// Example 3 -> Using two custom callbacks, with object values | ||
final class Cat | ||
{ | ||
private string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
$users = [ | ||
new Cat('izumi'), | ||
new Cat('nakano'), | ||
new Cat('booba'), | ||
new Cat('booba'), | ||
]; | ||
|
||
$collection = Collection::fromIterable($users) | ||
->distinct( | ||
static fn (string $left): Closure => static fn (string $right): bool => $left === $right, | ||
static fn (Cat $cat) => $cat->name() | ||
); // [0 => Cat<izumi>, 1 => Cat<nakano>, 2 => Cat<booba>] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
use loophp\collection\Collection; | ||
use loophp\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* @param CollectionInterface<int, int> $collection | ||
*/ | ||
function distinct_checkList(CollectionInterface $collection): void | ||
{ | ||
} | ||
/** | ||
* @param CollectionInterface<string, string> $collection | ||
*/ | ||
function distinct_checkMap(CollectionInterface $collection): void | ||
{ | ||
} | ||
function distinct_checkIntElement(int $value): void | ||
{ | ||
} | ||
function distinct_checkNullableInt(?int $value): void | ||
{ | ||
} | ||
function distinct_checkStringElement(string $value): void | ||
{ | ||
} | ||
function distinct_checkNullableString(?string $value): void | ||
{ | ||
} | ||
|
||
distinct_checkList(Collection::fromIterable([1, 2, 3, 1])->distinct()); | ||
distinct_checkMap(Collection::fromIterable(['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'd' => 'bar'])->distinct()); | ||
|
||
distinct_checkList(Collection::empty()->distinct()); | ||
distinct_checkMap(Collection::empty()->distinct()); | ||
|
||
distinct_checkNullableInt(Collection::fromIterable([1, 2, 3, 1])->distinct()->current()); | ||
distinct_checkNullableString(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->current()); | ||
|
||
// This retrieval method doesn't cause static analysis complaints | ||
// but is not always reliable because of that. | ||
distinct_checkIntElement(Collection::fromIterable([1, 2, 3, 1])->distinct()->all()[0]); | ||
distinct_checkStringElement(Collection::fromIterable(['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'd' => 'bar'])->distinct()->all()['a']); | ||
distinct_checkStringElement(Collection::fromIterable(['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'd' => 'bar'])->distinct()->all()['b']); | ||
|
||
// VALID failures - `current` returns T|null | ||
/** @psalm-suppress PossiblyNullArgument @phpstan-ignore-next-line */ | ||
distinct_checkIntElement(Collection::fromIterable([1, 2, 3, 1])->distinct()->current()); | ||
/** @psalm-suppress PossiblyNullArgument @phpstan-ignore-next-line */ | ||
distinct_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'bar'])->distinct()->current()); | ||
|
||
// VALID failures - these keys don't exist | ||
/** @psalm-suppress InvalidArrayOffset */ | ||
distinct_checkIntElement(Collection::fromIterable([1, 2, 3, 1])->distinct()->all()[4]); | ||
/** @psalm-suppress InvalidArrayOffset @phpstan-ignore-next-line */ | ||
distinct_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->distinct()->all()['plop']); |