Skip to content

Commit 080e403

Browse files
authored
Modernize Library (#2)
* Update compatibility to PHP 8.2+ * Upgrade Infection and PHPStan testing tools * Remove #[Pure] (which was invalid) * Remove theoretical forward-compatibility code that PHPStan knows can't ever be reached
1 parent a36e278 commit 080e403

File tree

5 files changed

+14
-87
lines changed

5 files changed

+14
-87
lines changed

.github/workflows/commit.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
php: [7.4, 8.0, 8.1]
10+
php: [8.2, 8.3, 8.4]
1111
steps:
1212
- name: Setup PHP
1313
uses: shivammathur/setup-php@v2
@@ -37,7 +37,7 @@ jobs:
3737
runs-on: ubuntu-latest
3838
strategy:
3939
matrix:
40-
php: [7.4, 8.0, 8.1]
40+
php: [8.2, 8.3, 8.4]
4141
steps:
4242
- name: Setup PHP
4343
uses: shivammathur/setup-php@v2
@@ -61,7 +61,7 @@ jobs:
6161
- name: Setup PHP
6262
uses: shivammathur/setup-php@v2
6363
with:
64-
php-version: 8.0
64+
php-version: 8.4
6565
tools: composer:v2
6666

6767
- name: Checkout Repository
@@ -78,7 +78,7 @@ jobs:
7878
runs-on: ubuntu-latest
7979
strategy:
8080
matrix:
81-
php: [ 7.4, 8.0, 8.1 ]
81+
php: [ 8.2, 8.3, 8.4 ]
8282
steps:
8383
- name: Setup PHP
8484
uses: shivammathur/setup-php@v2

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
"MIT"
66
],
77
"require": {
8-
"php": ">=7.4"
8+
"php": "^8.2"
9+
},
10+
"config": {
11+
"allow-plugins": {
12+
"infection/extension-installer": true
13+
}
914
},
1015
"require-dev": {
11-
"phpstan/phpstan": "^1",
16+
"phpstan/phpstan": "^2",
1217
"roave/security-advisories": "dev-master",
1318
"phpunit/phpunit": "^9.5",
14-
"infection/infection": "^0.26",
19+
"infection/infection": "^0.31",
1520
"squizlabs/php_codesniffer": "^3.6",
1621
"jetbrains/phpstorm-attributes": "^1.0"
1722
},

phpstan.neon

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
parameters:
2-
level: 9
2+
level: max
33
paths:
44
- src
5-
ignoreErrors:
6-
- '#Static method Navarr\\Utils\\IterableToArray::fallbackConvert\(\) is unused.#'
7-
- message: '#Unreachable statement - code above always terminates.#'
8-
path: src/IterableToArray.php

src/IterableToArray.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
namespace Navarr\Utils;
1111

12-
use JetBrains\PhpStorm\Pure;
13-
use Traversable;
14-
1512
use function is_array;
1613
use function iterator_to_array;
1714

@@ -31,45 +28,12 @@ class IterableToArray
3128
* </p>
3229
* @return array<T> An array containing the elements of the iterable.
3330
*/
34-
#[Pure]
3531
public static function convert(iterable $iterable, bool $preserve_keys = true): array
3632
{
3733
if (is_array($iterable)) {
3834
return $iterable;
3935
}
4036

41-
if ($iterable instanceof Traversable) {
42-
return iterator_to_array($iterable, $preserve_keys);
43-
}
44-
45-
// Fallback for supposedly impossible scenario
46-
return self::fallbackConvert($iterable, $preserve_keys);
47-
}
48-
49-
/**
50-
* Fallback to convert an iterable into an array
51-
*
52-
* This should, theoretically, never be used. However, for the sake of forward-compatibility, it exists and is
53-
* tested.
54-
*
55-
* It is extracted into its own private method to allow for unit testing.
56-
*
57-
* @template T
58-
* @param iterable<T> $iterable
59-
* @param bool $preserve_keys
60-
* @return array<T>
61-
*/
62-
#[Pure]
63-
private static function fallbackConvert(iterable $iterable, bool $preserve_keys): array
64-
{
65-
$result = [];
66-
foreach ($iterable as $key => $value) {
67-
if ($preserve_keys) {
68-
$result[$key] = $value;
69-
continue;
70-
}
71-
$result[] = $value;
72-
}
73-
return $result;
37+
return iterator_to_array($iterable, $preserve_keys);
7438
}
7539
}

tests/IterableToArrayTest.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,4 @@ public function testConvertWithIteratorDoesNotPreserveKeys()
4343
$this->assertEquals(['b'], $result);
4444
$this->assertNotEquals(['a' => 'b'], $result);
4545
}
46-
47-
/**
48-
* @dataProvider fallbackDataProvider
49-
*/
50-
public function testFallback(iterable $iterator, bool $preserve_keys, array $expectedResult)
51-
{
52-
$reflectionMethod = new ReflectionMethod(IterableToArray::class, 'fallbackConvert');
53-
$reflectionMethod->setAccessible(true);
54-
$result = $reflectionMethod->invoke(null, $iterator, $preserve_keys);
55-
56-
$this->assertEquals($expectedResult, $result);
57-
}
58-
59-
public function fallbackDataProvider(): array
60-
{
61-
return [
62-
'Array w/ keys' => [
63-
['a' => 'b', 'b' => 'c'],
64-
true,
65-
['a' => 'b', 'b' => 'c'],
66-
],
67-
'Array w/out keys' => [
68-
['a' => 'b', 'b' => 'c'],
69-
false,
70-
['b', 'c'],
71-
],
72-
'Iterable w/ keys' => [
73-
new ArrayIterator(['a' => 'b', 'b' => 'c']),
74-
true,
75-
['a' => 'b', 'b' => 'c'],
76-
],
77-
'Iterable w/out keys' => [
78-
new ArrayIterator(['a' => 'b', 'b' => 'c']),
79-
false,
80-
['b', 'c'],
81-
],
82-
];
83-
}
8446
}

0 commit comments

Comments
 (0)