diff --git a/.github/workflows/commit.yml b/.github/workflows/commit.yml index 25bddc8..50fd976 100644 --- a/.github/workflows/commit.yml +++ b/.github/workflows/commit.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [7.4, 8.0, 8.1] + php: [8.2, 8.3, 8.4] steps: - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -37,7 +37,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [7.4, 8.0, 8.1] + php: [8.2, 8.3, 8.4] steps: - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -61,7 +61,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.4 tools: composer:v2 - name: Checkout Repository @@ -78,7 +78,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ 7.4, 8.0, 8.1 ] + php: [ 8.2, 8.3, 8.4 ] steps: - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/composer.json b/composer.json index 29cb47f..cf9b29f 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,18 @@ "MIT" ], "require": { - "php": ">=7.4" + "php": "^8.2" + }, + "config": { + "allow-plugins": { + "infection/extension-installer": true + } }, "require-dev": { - "phpstan/phpstan": "^1", + "phpstan/phpstan": "^2", "roave/security-advisories": "dev-master", "phpunit/phpunit": "^9.5", - "infection/infection": "^0.26", + "infection/infection": "^0.31", "squizlabs/php_codesniffer": "^3.6", "jetbrains/phpstorm-attributes": "^1.0" }, diff --git a/phpstan.neon b/phpstan.neon index 84493e4..776ccd8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,4 @@ parameters: - level: 9 + level: max paths: - src - ignoreErrors: - - '#Static method Navarr\\Utils\\IterableToArray::fallbackConvert\(\) is unused.#' - - message: '#Unreachable statement - code above always terminates.#' - path: src/IterableToArray.php diff --git a/src/IterableToArray.php b/src/IterableToArray.php index 24f6c2d..2b0aef8 100644 --- a/src/IterableToArray.php +++ b/src/IterableToArray.php @@ -9,9 +9,6 @@ namespace Navarr\Utils; -use JetBrains\PhpStorm\Pure; -use Traversable; - use function is_array; use function iterator_to_array; @@ -31,45 +28,12 @@ class IterableToArray *

* @return array An array containing the elements of the iterable. */ - #[Pure] public static function convert(iterable $iterable, bool $preserve_keys = true): array { if (is_array($iterable)) { return $iterable; } - if ($iterable instanceof Traversable) { - return iterator_to_array($iterable, $preserve_keys); - } - - // Fallback for supposedly impossible scenario - return self::fallbackConvert($iterable, $preserve_keys); - } - - /** - * Fallback to convert an iterable into an array - * - * This should, theoretically, never be used. However, for the sake of forward-compatibility, it exists and is - * tested. - * - * It is extracted into its own private method to allow for unit testing. - * - * @template T - * @param iterable $iterable - * @param bool $preserve_keys - * @return array - */ - #[Pure] - private static function fallbackConvert(iterable $iterable, bool $preserve_keys): array - { - $result = []; - foreach ($iterable as $key => $value) { - if ($preserve_keys) { - $result[$key] = $value; - continue; - } - $result[] = $value; - } - return $result; + return iterator_to_array($iterable, $preserve_keys); } } diff --git a/tests/IterableToArrayTest.php b/tests/IterableToArrayTest.php index 06b60bd..c5264b8 100644 --- a/tests/IterableToArrayTest.php +++ b/tests/IterableToArrayTest.php @@ -43,42 +43,4 @@ public function testConvertWithIteratorDoesNotPreserveKeys() $this->assertEquals(['b'], $result); $this->assertNotEquals(['a' => 'b'], $result); } - - /** - * @dataProvider fallbackDataProvider - */ - public function testFallback(iterable $iterator, bool $preserve_keys, array $expectedResult) - { - $reflectionMethod = new ReflectionMethod(IterableToArray::class, 'fallbackConvert'); - $reflectionMethod->setAccessible(true); - $result = $reflectionMethod->invoke(null, $iterator, $preserve_keys); - - $this->assertEquals($expectedResult, $result); - } - - public function fallbackDataProvider(): array - { - return [ - 'Array w/ keys' => [ - ['a' => 'b', 'b' => 'c'], - true, - ['a' => 'b', 'b' => 'c'], - ], - 'Array w/out keys' => [ - ['a' => 'b', 'b' => 'c'], - false, - ['b', 'c'], - ], - 'Iterable w/ keys' => [ - new ArrayIterator(['a' => 'b', 'b' => 'c']), - true, - ['a' => 'b', 'b' => 'c'], - ], - 'Iterable w/out keys' => [ - new ArrayIterator(['a' => 'b', 'b' => 'c']), - false, - ['b', 'c'], - ], - ]; - } }