Skip to content

Commit c1667dc

Browse files
PHP 8.4 migration (#257)
* Explicitly mark all nullable parameters This removes the deprecations introduced in PHP 8.4 * Make iterator return types match Iterator PHP 8.4 makes not doing so a deprecation. * Skip tesetErrorIsThrownAsException for PHP >= 8.4 PHP 8.4 deprecates the use of E_USER_ERROR. This can mangle the exception thrown. So, skip the test in PHP 8.4. It may be possible to fix up what's being thrown, but I haven't found anything. Since using trigger_error for user errors is now deprecated, error_to_exception() should probably be deprecated as well. But that's a greater decision than making sure tests work correctly. * Fix message matching in RepeatTest::testNegativeRepeatedtimes PHP 8.4 has changed the format of __FUNCTION__ for closures, at least. * Force doubles to ints in group() PHP 8.4 deprecates the implicit conversion from doubles to ints when doing so would lose precision. However, disallowing doubles as array keys in Functional would be a BC break. * update CI matrix and actions * use E_USER_DEPRECATED instead of E_USER_ERROR in test * cs fixes * update workflows * add test for float keys * update test workflow * use the right method for phpunit < 9 * cs-fixer ignore env * use the right method for phpunit < 9 * run pcov step only for php version 7.1 which uses older version of phpunit --------- Co-authored-by: Michael D Johnson <mjohnson@pitsco.com>
1 parent 5f5ddb6 commit c1667dc

25 files changed

Lines changed: 108 additions & 45 deletions

.github/workflows/test.yaml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
name: Test
22

3-
on: [push, pull_request]
3+
on:
4+
pull_request:
5+
branches: ["main", "master"]
6+
paths:
7+
- "src/**"
8+
- "tests/**"
9+
- "composer.json"
10+
- ".github/workflows/test.yaml"
11+
push:
12+
branches: ["main", "master"]
13+
paths:
14+
- "src/**"
15+
- "tests/**"
16+
- "composer.json"
17+
- ".github/workflows/test.yaml"
418

519
jobs:
620
test:
721
name: PHP ${{ matrix.php-version }} (${{ matrix.experimental && 'experimental' || 'full support' }})
822

9-
runs-on: ubuntu-18.04
23+
runs-on: ubuntu-22.04
1024

1125
strategy:
1226
fail-fast: false
@@ -17,15 +31,19 @@ jobs:
1731
- 7.3
1832
- 7.4
1933
- 8.0
34+
- 8.1
35+
- 8.2
36+
- 8.3
37+
- 8.4
2038
experimental: [false]
2139
include:
22-
- php-version: 8.1
40+
- php-version: 8.5
2341
experimental: true
2442

2543
continue-on-error: ${{ matrix.experimental }}
2644
steps:
2745
- name: Checkout
28-
uses: actions/checkout@v2
46+
uses: actions/checkout@v5
2947

3048
- name: Install PHP with extensions
3149
uses: shivammathur/setup-php@v2
@@ -35,7 +53,7 @@ jobs:
3553
tools: composer:v2
3654

3755
- name: Install Composer dependencies
38-
uses: ramsey/composer-install@v1
56+
uses: ramsey/composer-install@v3
3957
with:
4058
composer-options: --prefer-dist
4159
continue-on-error: ${{ matrix.experimental }}
@@ -45,6 +63,7 @@ jobs:
4563
composer require pcov/clobber
4664
vendor/bin/pcov clobber
4765
continue-on-error: true
66+
if: ${{ matrix.php-version == '7.1' }}
4867

4968
- name: Run Tests
5069
run: composer tests
@@ -53,3 +72,5 @@ jobs:
5372
- name: Check coding style
5473
run: composer coding-style
5574
continue-on-error: ${{ matrix.experimental }}
75+
env:
76+
PHP_CS_FIXER_IGNORE_ENV: 1

src/Functional/CompareObjectHashOn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* Returns a comparison function that can be used with e.g. `usort()`
1515
*
1616
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
17-
* @param callable $keyFunction A function that takes an argument and returns the value that should be compared
17+
* @param callable|null $keyFunction A function that takes an argument and returns the value that should be compared
1818
* @return callable
1919
* @no-named-arguments
2020
*/
21-
function compare_object_hash_on(callable $comparison, callable $keyFunction = null)
21+
function compare_object_hash_on(callable $comparison, ?callable $keyFunction = null)
2222
{
2323
$keyFunction = $keyFunction ? compose($keyFunction, 'spl_object_hash') : 'spl_object_hash';
2424

src/Functional/CompareOn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* Returns a comparison function that can be used with e.g. `usort()`
1515
*
1616
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
17-
* @param callable $reducer A function that takes an argument and returns the value that should be compared
17+
* @param callable|null $reducer A function that takes an argument and returns the value that should be compared
1818
* @return callable
1919
* @no-named-arguments
2020
*/
21-
function compare_on(callable $comparison, callable $reducer = null)
21+
function compare_on(callable $comparison, ?callable $reducer = null)
2222
{
2323
if ($reducer === null) {
2424
return static function ($left, $right) use ($comparison) {

src/Functional/Every.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @return bool
2323
* @no-named-arguments
2424
*/
25-
function every($collection, callable $callback = null)
25+
function every($collection, ?callable $callback = null)
2626
{
2727
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2828

src/Functional/First.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
* arguments will be element, index, collection
2020
*
2121
* @param Traversable|array $collection
22-
* @param callable $callback
22+
* @param callable|null $callback
2323
* @return mixed
2424
* @no-named-arguments
2525
*/
26-
function first($collection, callable $callback = null)
26+
function first($collection, ?callable $callback = null)
2727
{
2828
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2929

src/Functional/Functional.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
final class Functional
1414
{
15-
1615
/**
1716
* @see \Function\ary
1817
*/

src/Functional/Group.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @return array
2222
* @no-named-arguments
2323
*/
24-
function group($collection, callable $callback)
24+
function group($collection, callable $callback): array
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

@@ -32,6 +32,11 @@ function group($collection, callable $callback)
3232

3333
InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__);
3434

35+
// Avoid implicit precision-loss from doubles (which cannot be keys)
36+
if (\is_numeric($groupKey)) {
37+
$groupKey = (int) $groupKey;
38+
}
39+
3540
if (!isset($groups[$groupKey])) {
3641
$groups[$groupKey] = [];
3742
}

src/Functional/Head.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* Alias for Functional\first
1818
*
1919
* @param Traversable|array $collection
20-
* @param callable $callback
20+
* @param callable|null $callback
2121
* @return mixed
2222
* @no-named-arguments
2323
*/
24-
function head($collection, callable $callback = null)
24+
function head($collection, ?callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

src/Functional/Last.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
* Callback arguments will be element, index, collection
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return mixed
2323
* @no-named-arguments
2424
*/
25-
function last($collection, callable $callback = null)
25+
function last($collection, ?callable $callback = null)
2626
{
2727
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2828

src/Functional/Memoize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @return mixed
2222
* @no-named-arguments
2323
*/
24-
function memoize(callable $callback = null, $arguments = [], $key = null)
24+
function memoize(?callable $callback = null, $arguments = [], $key = null)
2525
{
2626
static $storage = [];
2727
if ($callback === null) {

0 commit comments

Comments
 (0)