From 4081ff0bd5798c93375cb7b7f0810cef396a0ebd Mon Sep 17 00:00:00 2001 From: azjezz Date: Mon, 6 Jul 2020 19:19:36 +0100 Subject: [PATCH] [Arr] flip: throw for non-arraykey values --- src/Psl/Arr/count_values.php | 1 + src/Psl/Arr/flip.php | 1 + src/Psl/Arr/is_arraykey.php | 19 +++++++++++++++++++ tests/Psl/Arr/FlipTest.php | 8 -------- 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 src/Psl/Arr/is_arraykey.php 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]); - } }