diff --git a/src/Psl/Arr/count_values.php b/src/Psl/Arr/count_values.php index 67123d3..fc99488 100644 --- a/src/Psl/Arr/count_values.php +++ b/src/Psl/Arr/count_values.php @@ -27,6 +27,7 @@ function count_values(iterable $values): array $result = []; foreach ($values as $value) { + Psl\invariant(is_arraykey($value), 'Expected all values to be of type array-key, value of type (%s) provided.', gettype($value)); /** @psalm-var int $count */ $count = idx($result, $value, 0); $result[$value] = $count + 1; diff --git a/src/Psl/Arr/flip.php b/src/Psl/Arr/flip.php index bb712e7..ddf1a62 100644 --- a/src/Psl/Arr/flip.php +++ b/src/Psl/Arr/flip.php @@ -26,6 +26,7 @@ function flip(iterable $iterable): array { $result = []; foreach ($iterable as $k => $v) { + Psl\invariant(is_arraykey($v), 'Expected all values to be of type array-key, value of type (%s) provided.', gettype($v)); $result[$v] = $k; } diff --git a/src/Psl/Arr/is_arraykey.php b/src/Psl/Arr/is_arraykey.php new file mode 100644 index 0000000..70311ab --- /dev/null +++ b/src/Psl/Arr/is_arraykey.php @@ -0,0 +1,19 @@ +expectException(Exception\InvariantViolationException::class); - $this->expectExceptionMessage('Expected all values to be of type array-key, value of type (resource) provided.'); - - Arr\flip([STDOUT]); - } }