Skip to content
Merged

Dev #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/RunOpenCode/Component/Dataset/src/Collector/ArrayCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public function __construct(
$this->value = iterable_to_array($this->source);
}

/**
* @return list<TKey>
*/
public function keys(): array
{
return \array_keys($this->value);
}

/**
* @return list<TValue>
*/
public function values(): array
{
return \array_values($this->value);
}

/**
* {@inheritdoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ final class IndexedCollector implements CollectorInterface, \ArrayAccess, \Itera
/**
* Index of values with keys of scalar type.
*
* @var array<TKey, list<TValue>>
* @var array<scalar, list<TValue>>
*/
private array $scalarIndex = [];

/**
* Index of values with keys of object type.
*
* @var \SplObjectStorage<TKey&object, list<TValue>>
* @var \SplObjectStorage<object, list<TValue>>
Comment on lines 52 to +62
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "Currently, the allowed key types are scalar values (int, float, string, bool, null) and objects." is inconsistent with the error messages in the code (lines 98, 120, 140) which state "Only object, string and integer keys are supported." Since the code change now supports all scalar types, the error messages should be updated to match this documentation, or vice versa.

Copilot uses AI. Check for mistakes.
*/
private \SplObjectStorage $objectIndex;

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

if (\is_string($key) || \is_int($key)) {
$this->scalarIndex[$key] = $this->scalarIndex[$key] ?? [];
$this->scalarIndex[$key][] = $value;
if (\is_scalar($key)) {
$this->scalarIndex[$key] = $this->scalarIndex[$key] ?? []; // @phpstan-ignore-line
$this->scalarIndex[$key][] = $value; // @phpstan-ignore-line
Comment on lines +84 to +86
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IndexedCollector now supports all scalar types (float, bool, null) in addition to string and int, but there are no test cases covering these newly supported key types. Consider adding test cases for float and boolean keys to ensure the expanded scalar support works correctly.

Copilot uses AI. Check for mistakes.
continue;
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public function getIterator(): \Traversable
public function offsetExists(mixed $offset): bool
{
return match (true) {
\is_string($offset) || \is_int($offset) => \array_key_exists($offset, $this->scalarIndex),
\is_scalar($offset) => \array_key_exists($offset, $this->scalarIndex), // @phpstan-ignore-line
\is_object($offset) => $this->objectIndex->contains($offset),
default => throw new UnsupportedException('Only object, string and integer keys are supported.'),
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message states "Only object, string and integer keys are supported" but the code now accepts all scalar types (including float and bool). The error message should be updated to reflect the expanded support, such as "Only object and scalar keys are supported" or "Only object, string, integer, float, and boolean keys are supported".

Copilot uses AI. Check for mistakes.
};
Expand All @@ -135,7 +135,7 @@ public function offsetGet(mixed $offset): mixed
}

return match (true) {
\is_string($offset) || \is_int($offset) => $this->scalarIndex[$offset],
\is_scalar($offset) => $this->scalarIndex[$offset], // @phpstan-ignore-line
\is_object($offset) => $this->objectIndex[$offset],
default => throw new UnsupportedException('Only object, string and integer keys are supported.'),
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message states "Only object, string and integer keys are supported" but the code now accepts all scalar types (including float and bool). The error message should be updated to reflect the expanded support, such as "Only object and scalar keys are supported" or "Only object, string, integer, float, and boolean keys are supported".

Copilot uses AI. Check for mistakes.
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public function array_access(): void
$this->assertArrayHasKey('b', $collector);
}

#[Test]
public function keys(): void
{
$dataset = [
'a' => 1,
'b' => 2,
'c' => 3,
];

$collector = collect($dataset, ArrayCollector::class);

$this->assertSame(['a', 'b', 'c'], $collector->keys());
}

#[Test]
public function values(): void
{
$dataset = [
'a' => 1,
'b' => 2,
'c' => 3,
];

$collector = collect($dataset, ArrayCollector::class);

$this->assertSame([1, 2, 3], $collector->values());
}

#[Test]
public function counts(): void
{
Expand Down