Skip to content

Commit 3546eab

Browse files
authored
Merge pull request #13 from gameeapp/fully_supported_generics_in_unique_obj_collection
Added full support of generics to UniqueObjectCollection
2 parents f4c633f + d551de6 commit 3546eab

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

src/Collection/UniqueObjectCollection.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44

55
namespace Gamee\Collections\Collection;
66

7-
use Gamee\Collections\Iterator\ObjectIterator;
8-
97
/**
108
* @template IdentifiableObject of object
119
*/
1210
abstract class UniqueObjectCollection implements \Countable, \IteratorAggregate
1311
{
1412

1513
/**
16-
* @var array|mixed[]
14+
* @var array<IdentifiableObject>
1715
*/
1816
protected array $data = [];
1917

18+
19+
/**
20+
* @deprecated
21+
*/
2022
abstract protected function getItemType(): string;
2123

2224

@@ -29,16 +31,13 @@ abstract protected function getIdentifier(object $item);
2931

3032
/**
3133
* Skips items with duplicate key
32-
* @param array<mixed> $data
34+
* @param array<IdentifiableObject> $data
3335
*/
3436
public function __construct(array $data)
3537
{
36-
$classItemName = $this->getItemType();
3738
$uniqueItems = [];
3839

3940
foreach ($data as $item) {
40-
$this->assertItemType($item, $classItemName);
41-
4241
$identifier = $this->getIdentifier($item);
4342

4443
if (!$this->exists($identifier)) {
@@ -52,6 +51,7 @@ public function __construct(array $data)
5251

5352
/**
5453
* @return static
54+
* @deprecated
5555
*/
5656
public static function createFromImmutableObjectCollection(
5757
ImmutableObjectCollection $immutableObjectCollection
@@ -69,9 +69,12 @@ static function (ImmutableObjectCollection $immutableObjectCollection): array {
6969
}
7070

7171

72-
public function getIterator(): ObjectIterator
72+
/**
73+
* @return \ArrayIterator<IdentifiableObject>
74+
*/
75+
public function getIterator(): \ArrayIterator
7376
{
74-
return new ObjectIterator($this->data);
77+
return new \ArrayIterator($this->data);
7578
}
7679

7780

@@ -81,7 +84,7 @@ public function getIterator(): ObjectIterator
8184
*/
8285
public function mergeWith(UniqueObjectCollection $collection)
8386
{
84-
if ($this->getItemType() !== $collection->getItemType()) {
87+
if (get_class($collection) !== static::class) {
8588
throw new \RuntimeException('Can not merge collections with different item type');
8689
}
8790

@@ -147,8 +150,6 @@ public function getScalarIds(): array
147150
*/
148151
public function addItem(object $item)
149152
{
150-
$this->assertItemType($item, $this->getItemType());
151-
152153
$identifier = $this->getIdentifier($item);
153154

154155
if ($this->exists($identifier)) {
@@ -230,15 +231,4 @@ protected function getItems(): array
230231
{
231232
return $this->data;
232233
}
233-
234-
235-
/**
236-
* @param IdentifiableObject $item
237-
*/
238-
private function assertItemType(object $item, string $type): void
239-
{
240-
if (!$item instanceof $type) {
241-
throw new \InvalidArgumentException(static::class . ' only accepts ' . $type);
242-
}
243-
}
244234
}

tests/Collections/Collection/UniqueObjectCollectionTest.phpt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ class UniqueObjectCollectionTest extends TestCase
5757
}
5858

5959

60-
public function testExceptionOnWrongItemTypeDuringCreation(): void
61-
{
62-
Assert::exception(function (): void {
63-
$this->createTestCollection(
64-
[
65-
new ItemClass(1),
66-
new AnotherClass(2),
67-
new ItemClass(3),
68-
]
69-
);
70-
}, \InvalidArgumentException::class);
71-
}
72-
73-
7460
public function testMergeWith(): void
7561
{
7662
$items = [
@@ -351,16 +337,6 @@ class UniqueObjectCollectionTest extends TestCase
351337
}
352338

353339

354-
public function testExceptionOnWrongItemTypeInAddItem(): void
355-
{
356-
$collection = $this->createTestCollection();
357-
358-
Assert::exception(function () use ($collection): void {
359-
$collection->addItem(new AnotherClass(3));
360-
}, \InvalidArgumentException::class);
361-
}
362-
363-
364340
protected function collectionDataProvider(): array
365341
{
366342
return [

0 commit comments

Comments
 (0)