diff --git a/src/iterator_to_array.php b/src/iterator_to_array.php index 48223cf..191975b 100644 --- a/src/iterator_to_array.php +++ b/src/iterator_to_array.php @@ -8,23 +8,25 @@ namespace Ibexa\PolyfillPhp82; -/** - * @template T - * - * @param iterable $iterator - * - * @return array - */ -function iterator_to_array(iterable $iterator, bool $preserve_keys = true): array -{ - if (\PHP_VERSION_ID > 8_02_00) { - /** @var \Traversable $iterator */ - return \iterator_to_array($iterator, $preserve_keys); - } +if (!function_exists(__NAMESPACE__ . '\\iterator_to_array')) { + /** + * @template T + * + * @param iterable $iterator + * + * @return array + */ + function iterator_to_array(iterable $iterator, bool $preserve_keys = true): array + { + if (\PHP_VERSION_ID > 8_02_00) { + /** @var \Traversable $iterator */ + return \iterator_to_array($iterator, $preserve_keys); + } - if ($iterator instanceof \Traversable) { - return \iterator_to_array($iterator, $preserve_keys); - } + if ($iterator instanceof \Traversable) { + return \iterator_to_array($iterator, $preserve_keys); + } - return $iterator; + return $iterator; + } } diff --git a/tests/Php82Test.php b/tests/Php82Test.php index 8bc961a..a1130f4 100644 --- a/tests/Php82Test.php +++ b/tests/Php82Test.php @@ -28,6 +28,13 @@ public function testGeneratorForIteratorToArray(): void ], $result); } + public function testDeclarationFileCanBeLoadedTwice(): void + { + require dirname(__DIR__) . '/src/iterator_to_array.php'; + + self::assertTrue(function_exists('Ibexa\PolyfillPhp82\iterator_to_array')); + } + public function testArrayForIteratorToArray(): void { $result = iterator_to_array([