44
55namespace Gamee \Collections \Collection ;
66
7- use Gamee \Collections \Iterator \ObjectIterator ;
8-
97/**
108 * @template IdentifiableObject of object
119 */
1210abstract 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}
0 commit comments