Skip to content

Commit 94a7bc7

Browse files
committed
column: assert nested before delegating so the error names column()
column(null, $indexKey) delegated to indexBy() before asserting, so a flat array threw "indexBy(): Expected a nested array" - a method name the caller never wrote and can't grep for. Same convention as pluckNth(): assert first, then delegate.
1 parent f7d15c7 commit 94a7bc7

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/SmartArrayBase.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,11 +820,13 @@ public function columnAt(int $index): static
820820
*/
821821
public function column(int|string|null $columnKey, int|string|null $indexKey = null): static
822822
{
823+
$this->assertNestedArray(); // assert here so the error names column(), not indexBy()
824+
823825
if ($columnKey === null && $indexKey !== null) {
824826
return $this->indexBy((string)$indexKey);
825827
}
826828

827-
$this->assertNestedArray();
829+
828830
if ($columnKey !== null) {
829831
$this->warnIfMissing($columnKey);
830832
}

‎tests/Unit/ProjectionTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ public function testColumnWithBothArgumentsNullReturnsRenumberedRows(string $cla
150150
$this->assertSame([['id' => 1], ['id' => 2]], $result->toArray());
151151
}
152152

153+
#[DataProvider('modeProvider')]
154+
public function testColumnOnFlatThrowsNamingColumn(string $class): void
155+
{
156+
// Covers the indexBy() delegation shape: the error names the method the
157+
// caller actually wrote, not the internal delegate
158+
$this->expectException(InvalidArgumentException::class);
159+
$this->expectExceptionMessage('column(): Expected a nested array, but got a flat array');
160+
161+
$class::new(['a', 'b'])->column(null, 'x');
162+
}
163+
153164
//endregion
154165
//region indexBy()
155166

0 commit comments

Comments
 (0)