Skip to content

Commit e2bbe59

Browse files
authored
Merge pull request #14 from RunOpenCode/dev
Dev
2 parents c51eb89 + 7a7128d commit e2bbe59

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function __construct(
5151
$this->value = iterable_to_array($this->source);
5252
}
5353

54+
/**
55+
* @return list<TKey>
56+
*/
57+
public function keys(): array
58+
{
59+
return \array_keys($this->value);
60+
}
61+
62+
/**
63+
* @return list<TValue>
64+
*/
65+
public function values(): array
66+
{
67+
return \array_values($this->value);
68+
}
69+
5470
/**
5571
* {@inheritdoc}
5672
*

src/RunOpenCode/Component/Dataset/src/Collector/IndexedCollector.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ final class IndexedCollector implements CollectorInterface, \ArrayAccess, \Itera
5252
/**
5353
* Index of values with keys of scalar type.
5454
*
55-
* @var array<TKey, list<TValue>>
55+
* @var array<scalar, list<TValue>>
5656
*/
5757
private array $scalarIndex = [];
5858

5959
/**
6060
* Index of values with keys of object type.
6161
*
62-
* @var \SplObjectStorage<TKey&object, list<TValue>>
62+
* @var \SplObjectStorage<object, list<TValue>>
6363
*/
6464
private \SplObjectStorage $objectIndex;
6565

@@ -81,9 +81,9 @@ public function __construct(
8181
foreach ($this->source as $key => $value) {
8282
$this->collected[] = [$key, $value];
8383

84-
if (\is_string($key) || \is_int($key)) {
85-
$this->scalarIndex[$key] = $this->scalarIndex[$key] ?? [];
86-
$this->scalarIndex[$key][] = $value;
84+
if (\is_scalar($key)) {
85+
$this->scalarIndex[$key] = $this->scalarIndex[$key] ?? []; // @phpstan-ignore-line
86+
$this->scalarIndex[$key][] = $value; // @phpstan-ignore-line
8787
continue;
8888
}
8989

@@ -115,7 +115,7 @@ public function getIterator(): \Traversable
115115
public function offsetExists(mixed $offset): bool
116116
{
117117
return match (true) {
118-
\is_string($offset) || \is_int($offset) => \array_key_exists($offset, $this->scalarIndex),
118+
\is_scalar($offset) => \array_key_exists($offset, $this->scalarIndex), // @phpstan-ignore-line
119119
\is_object($offset) => $this->objectIndex->contains($offset),
120120
default => throw new UnsupportedException('Only object and scalar keys are supported.'),
121121
};
@@ -135,7 +135,7 @@ public function offsetGet(mixed $offset): mixed
135135
}
136136

137137
return match (true) {
138-
\is_string($offset) || \is_int($offset) => $this->scalarIndex[$offset],
138+
\is_scalar($offset) => $this->scalarIndex[$offset], // @phpstan-ignore-line
139139
\is_object($offset) => $this->objectIndex[$offset],
140140
default => throw new UnsupportedException('Only object and scalar keys are supported.'),
141141
};

src/RunOpenCode/Component/Dataset/tests/Collector/ArrayCollectorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ public function array_access(): void
4949
$this->assertArrayHasKey('b', $collector);
5050
}
5151

52+
#[Test]
53+
public function keys(): void
54+
{
55+
$dataset = [
56+
'a' => 1,
57+
'b' => 2,
58+
'c' => 3,
59+
];
60+
61+
$collector = collect($dataset, ArrayCollector::class);
62+
63+
$this->assertSame(['a', 'b', 'c'], $collector->keys());
64+
}
65+
66+
#[Test]
67+
public function values(): void
68+
{
69+
$dataset = [
70+
'a' => 1,
71+
'b' => 2,
72+
'c' => 3,
73+
];
74+
75+
$collector = collect($dataset, ArrayCollector::class);
76+
77+
$this->assertSame([1, 2, 3], $collector->values());
78+
}
79+
5280
#[Test]
5381
public function counts(): void
5482
{

0 commit comments

Comments
 (0)