Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ abstract class AbstractCollection extends AbstractArray implements CollectionInt
use ValueToStringTrait;
use ValueExtractorTrait;

/**
* @var class-string<CollectionInterface<T>>
*/
protected string $collection = Collection::class;

/**
* @throws InvalidArgumentException if $element is of the wrong type.
*/
Expand Down Expand Up @@ -213,7 +218,8 @@ public function where(?string $propertyOrMethod, mixed $value): CollectionInterf
*/
public function map(callable $callback): CollectionInterface
{
return new Collection('mixed', array_map($callback, $this->data));
/** @var CollectionInterface<TCallbackReturn> */
return new $this->collection('mixed', array_map($callback, $this->data));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
*/
class Collection extends AbstractCollection
{
/**
* @psalm-suppress NonInvariantDocblockPropertyType
*/
protected string $collection = self::class;

/**
* Constructs a collection object of the specified type, optionally with the
* specified data.
Expand Down
5 changes: 5 additions & 0 deletions src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
*/
class Set extends AbstractSet
{
/**
* @psalm-suppress NonInvariantDocblockPropertyType
*/
protected string $collection = self::class;

/**
* Constructs a set object of the specified type, optionally with the
* specified data.
Expand Down
7 changes: 7 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public function testAdd(): void
$this->assertTrue($collection->add($this->faker->numberBetween()));
}

public function testMapWillReturnANewCollection(): void
{
$collection = (new Collection('integer', [4, 2]))->map(fn ($value) => $value);

$this->assertInstanceOf(Collection::class, $collection);
}

public function testAddMayAddSameObjectMultipleTimes(): void
{
$expectedCount = 4;
Expand Down
20 changes: 20 additions & 0 deletions tests/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ public function testMergingSets(): void
$this->assertSame(['X', 'Y', 'Z'], $set3->toArray());
}

public function testDuplicatesViaMap(): void
{
$this->assertTrue($this->set->add(100));
$this->assertTrue($this->set->add(200));

$set = $this->set->map(fn () => 200);

$this->assertSame([200], $set->toArray());
}

public function testMapReturnsANewSet(): void
{
$this->assertTrue($this->set->add(100));
$this->assertTrue($this->set->add(200));

$set = $this->set->map(fn ($value) => $value);

$this->assertInstanceOf(Set::class, $set);
}

public function testMergingSetsOfObjects(): void
{
$obj1 = new Foo();
Expand Down