Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 1 addition & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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
38 changes: 1 addition & 37 deletions src/IterableToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

namespace Navarr\Utils;

use JetBrains\PhpStorm\Pure;
use Traversable;

use function is_array;
use function iterator_to_array;

Expand All @@ -31,45 +28,12 @@
* </p>
* @return array<T> 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;

Check warning on line 34 in src/IterableToArray.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public static function convert(iterable $iterable, bool $preserve_keys = true): array { if (is_array($iterable)) { - return $iterable; + } return iterator_to_array($iterable, $preserve_keys); } }

Check warning on line 34 in src/IterableToArray.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public static function convert(iterable $iterable, bool $preserve_keys = true): array { if (is_array($iterable)) { - return $iterable; + } return iterator_to_array($iterable, $preserve_keys); } }

Check warning on line 34 in src/IterableToArray.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public static function convert(iterable $iterable, bool $preserve_keys = true): array { if (is_array($iterable)) { - return $iterable; + } return iterator_to_array($iterable, $preserve_keys); } }
}

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<T> $iterable
* @param bool $preserve_keys
* @return array<T>
*/
#[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);
}
}
38 changes: 0 additions & 38 deletions tests/IterableToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
];
}
}