From 8dcfa5df06d8944d33fdc0907d33a057a1a6741b Mon Sep 17 00:00:00 2001 From: Navarr Date: Mon, 6 Oct 2025 15:50:17 -0400 Subject: [PATCH 1/4] Modernize library * Update phpstan and infection * Minimum version to 8.2 --- .github/workflows/commit.yml | 8 ++++---- composer.json | 6 +++--- phpstan.neon | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) 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..bb3d4a1 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,13 @@ "MIT" ], "require": { - "php": ">=7.4" + "php": "^8.2" }, "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..19f81a4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 9 + level: max paths: - src ignoreErrors: From 16fda31cea9073444dd61ecd8c01b9b7bd5da010 Mon Sep 17 00:00:00 2001 From: Navarr Date: Mon, 6 Oct 2025 15:51:19 -0400 Subject: [PATCH 2/4] Allow Infection Extension Installer plugin --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index bb3d4a1..cf9b29f 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,11 @@ "require": { "php": "^8.2" }, + "config": { + "allow-plugins": { + "infection/extension-installer": true + } + }, "require-dev": { "phpstan/phpstan": "^2", "roave/security-advisories": "dev-master", From 30e525e6dc4b4738cbb05bff99edecce8ec3a6b7 Mon Sep 17 00:00:00 2001 From: Navarr Date: Mon, 6 Oct 2025 15:54:49 -0400 Subject: [PATCH 3/4] Remove unnecessary forward compatibility, incorrect Pure statement --- src/IterableToArray.php | 38 +---------------------------------- tests/IterableToArrayTest.php | 38 ----------------------------------- 2 files changed, 1 insertion(+), 75 deletions(-) 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'], - ], - ]; - } } From d460b1af33130b6791f0b6f5303c9aeff3a246c4 Mon Sep 17 00:00:00 2001 From: Navarr Date: Mon, 6 Oct 2025 15:55:30 -0400 Subject: [PATCH 4/4] Remove ignored errors --- phpstan.neon | 4 ---- 1 file changed, 4 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 19f81a4..776ccd8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,7 +2,3 @@ parameters: level: max paths: - src - ignoreErrors: - - '#Static method Navarr\\Utils\\IterableToArray::fallbackConvert\(\) is unused.#' - - message: '#Unreachable statement - code above always terminates.#' - path: src/IterableToArray.php