Skip to content

Commit

Permalink
fix: make type aliases compatible in diff, intersect and merge (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
error17191 authored and ramsey committed Dec 27, 2022
1 parent daa7a68 commit 77a182e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function merge(CollectionInterface ...$collections): CollectionInterface

// When using generics (Collection.php, Set.php, etc),
// we also need to make sure that the internal types match each other
if ($collection->getType() !== $this->getType()) {
if ($this->getUniformType($collection) !== $this->getUniformType($this)) {
throw new CollectionMismatchException(
sprintf(
'Collection items in collection with index %d must be of type %s',
Expand Down Expand Up @@ -305,7 +305,7 @@ private function compareCollectionTypes(CollectionInterface $other): void

// When using generics (Collection.php, Set.php, etc),
// we also need to make sure that the internal types match each other
if ($other->getType() !== $this->getType()) {
if ($this->getUniformType($other) !== $this->getUniformType($this)) {
throw new CollectionMismatchException('Collection items must be of type ' . $this->getType());
}
}
Expand All @@ -330,4 +330,21 @@ function ($a, $b): int {
return $a === $b ? 0 : ($a < $b ? 1 : -1);
};
}

/**
* @param CollectionInterface<mixed> $collection
*/
private function getUniformType(CollectionInterface $collection): string
{
switch ($collection->getType()) {
case 'integer':
return 'int';
case 'boolean':
return 'bool';
case 'double':
return 'float';
default:
return $collection->getType();
}
}
}
24 changes: 24 additions & 0 deletions tests/CollectionManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,28 @@ public function testMapConvertsValues(): void
$this->assertSame(['Jane', 'John', 'Janice'], $names->toArray());
$this->assertSame([1, 2, 3], $ids->toArray());
}

public function testWorksUniformlyWithTypeAliases(): void
{
$collection1 = new Collection('integer', [1, 2, 3]);
$collection2 = new Collection('int', [1, 2]);

$this->assertEquals([3], $collection1->diff($collection2)->toArray());
$this->assertEquals([1, 2], $collection1->intersect($collection2)->toArray());
$this->assertEquals([1, 2, 3, 1, 2], $collection1->merge($collection2)->toArray());

$collection1 = new Collection('float', [1.5, 2.5, 3.5]);
$collection2 = new Collection('double', [1.5, 2.5]);

$this->assertEquals([3.5], $collection1->diff($collection2)->toArray());
$this->assertEquals([1.5, 2.5], $collection1->intersect($collection2)->toArray());
$this->assertEquals([1.5, 2.5, 3.5, 1.5, 2.5], $collection1->merge($collection2)->toArray());

$collection1 = new Collection('bool', [true]);
$collection2 = new Collection('boolean', [false]);

$this->assertEquals([true, false], $collection1->diff($collection2)->toArray());
$this->assertEquals([], $collection1->intersect($collection2)->toArray());
$this->assertEquals([true, false], $collection1->merge($collection2)->toArray());
}
}

0 comments on commit 77a182e

Please sign in to comment.