Skip to content

Commit

Permalink
array empty is any
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 8, 2024
1 parent 7232071 commit 386406f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/functions-array.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function assertArray(
ArrayTypeParameterInterface $parameter,
array|ArrayAccess $argument,
): array {
if ($parameter->parameters()->count() === 0) {
return (array) $argument;
}

return arguments($parameter->parameters(), $argument)->toArray();
}

Expand Down
19 changes: 15 additions & 4 deletions tests/FunctionsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,23 @@ public function testArrayp(): void
$this->assertSame(['b'], $parameter->parameters()->optionalKeys()->toArray());
}

public function testArrayRequiredEmpty(): void
public function arrayRequiredEmptyAnyDataProvider(): array
{
return [
[[]],
[[
'a' => 1,
]],
];
}

/**
* @dataProvider arrayRequiredEmptyAnyDataProvider
*/
public function testArrayRequiredEmptyAny(array $test): void
{
$parameter = arrayp();
$this->assertSame([], assertArray($parameter, []));
$this->expectException(ArgumentCountError::class);
assertArray($parameter, [[]]);
$this->assertSame($test, assertArray($parameter, $test));
}

public function testArrayRequired(): void
Expand Down

0 comments on commit 386406f

Please sign in to comment.