Skip to content

Commit 3bc7eba

Browse files
authored
Merge pull request #43 from YevicT/add-annotation-template-collection
πŸ§‘β€πŸ’» Add template annotation to Collections
2 parents 07d87a6 + 661d8c6 commit 3bc7eba

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

β€Žcomponent/Abstract/Collection.phpβ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44

55
namespace Phant\DataStructure\Abstract;
66

7+
/**
8+
* @template T
9+
*/
710
abstract class Collection
811
{
12+
/**
13+
* @var array<T>
14+
*/
915
protected array $items = [];
1016

1117
public function __construct(
1218
) {
1319
}
1420

21+
/**
22+
* @param T $item
23+
* @return static
24+
*/
1525
protected function addItem(
1626
mixed $item
1727
): static {
@@ -22,6 +32,10 @@ protected function addItem(
2232
return $this;
2333
}
2434

35+
/**
36+
* @param T $item
37+
* @return static
38+
*/
2539
protected function removeItem(
2640
mixed $item
2741
): static {
@@ -33,12 +47,18 @@ protected function removeItem(
3347
return $this;
3448
}
3549

50+
/**
51+
* @param T $item
52+
*/
3653
final public function contains(
3754
mixed $item
3855
): bool {
3956
return array_search($item, $this->items) !== false;
4057
}
4158

59+
/**
60+
* @return \Generator<T>
61+
*/
4262
final public function iterate(
4363
): \Generator {
4464
foreach ($this->items as $item) {
@@ -56,12 +76,19 @@ final public function getNbItems(
5676
return count($this->items);
5777
}
5878

79+
/**
80+
* @return T|null
81+
*/
5982
final public function getByKey(
6083
int $key
6184
): mixed {
6285
return $this->items[ $key ] ?? null;
6386
}
6487

88+
/**
89+
* @param Collection<T> $collection
90+
* @return static
91+
*/
6592
final public function merge(
6693
self $collection
6794
): static {
@@ -72,6 +99,11 @@ final public function merge(
7299
return $this;
73100
}
74101

102+
/**
103+
* @template U
104+
* @param callable(T): U $callback
105+
* @return array<U>
106+
*/
75107
final public function map(
76108
callable $callback
77109
): array {

β€Žtest/Abstract/CollectionTest.phpβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testMap(): void
7676
$collection->add($value1);
7777
$collection->add($value2);
7878

79-
$result = $collection->map(fn(Value $item) => (string)$item . '!');
79+
$result = $collection->map(fn (Value $item) => (string)$item . '!');
8080

8181
$this->assertIsArray($result);
8282
$this->assertCount(2, $result);

0 commit comments

Comments
Β (0)