Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to PHP 8.4 #256

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/Functional/CompareObjectHashOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* Returns a comparison function that can be used with e.g. `usort()`
*
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
* @param callable $keyFunction A function that takes an argument and returns the value that should be compared
* @param callable|null $keyFunction A function that takes an argument and returns the value that should be compared
* @return callable
* @no-named-arguments
*/
function compare_object_hash_on(callable $comparison, callable $keyFunction = null)
function compare_object_hash_on(callable $comparison, ?callable $keyFunction = null)
{
$keyFunction = $keyFunction ? compose($keyFunction, 'spl_object_hash') : 'spl_object_hash';

Expand Down
4 changes: 2 additions & 2 deletions src/Functional/CompareOn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* Returns a comparison function that can be used with e.g. `usort()`
*
* @param callable $comparison A function that compares the two values. Pick e.g. strcmp() or strnatcasecmp()
* @param callable $reducer A function that takes an argument and returns the value that should be compared
* @param callable|null $reducer A function that takes an argument and returns the value that should be compared
* @return callable
* @no-named-arguments
*/
function compare_on(callable $comparison, callable $reducer = null)
function compare_on(callable $comparison, ?callable $reducer = null)
{
if ($reducer === null) {
return static function ($left, $right) use ($comparison) {
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @return bool
* @no-named-arguments
*/
function every($collection, callable $callback = null)
function every($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
4 changes: 2 additions & 2 deletions src/Functional/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
* arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @param callable|null $callback
* @return mixed
* @no-named-arguments
*/
function first($collection, callable $callback = null)
function first($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
5 changes: 5 additions & 0 deletions src/Functional/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function group($collection, callable $callback)

InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__);

// Avoid implicit precision-loss from doubles (which cannot be keys)
if (is_numeric($groupKey)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a testcase for this so that we have the behavior explicitly pinned down?

$groupKey = intval($groupKey);
}

if (!isset($groups[$groupKey])) {
$groups[$groupKey] = [];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
* Alias for Functional\first
*
* @param Traversable|array $collection
* @param callable $callback
* @param callable|null $callback
* @return mixed
* @no-named-arguments
*/
function head($collection, callable $callback = null)
function head($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @param callable|null $callback
* @return mixed
* @no-named-arguments
*/
function last($collection, callable $callback = null)
function last($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Memoize.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @return mixed
* @no-named-arguments
*/
function memoize(callable $callback = null, $arguments = [], $key = null)
function memoize(?callable $callback = null, $arguments = [], $key = null)
{
static $storage = [];
if ($callback === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @return bool
* @no-named-arguments
*/
function none($collection, callable $callback = null)
function none($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @return mixed
* @no-named-arguments
*/
function pick($collection, $index, $default = null, callable $callback = null)
function pick($collection, $index, $default = null, ?callable $callback = null)
{
InvalidArgumentException::assertArrayAccess($collection, __FUNCTION__, 1);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @return boolean
* @no-named-arguments
*/
function poll(callable $callback, $timeout, Traversable $delaySequence = null)
function poll(callable $callback, $timeout, ?Traversable $delaySequence = null)
{
InvalidArgumentException::assertIntegerGreaterThanOrEqual($timeout, 0, __FUNCTION__, 2);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @return array
* @no-named-arguments
*/
function reject($collection, callable $callback = null)
function reject($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @return mixed Return value of the function
* @no-named-arguments
*/
function retry(callable $callback, $retries, Traversable $delaySequence = null)
function retry(callable $callback, $retries, ?Traversable $delaySequence = null)
{
InvalidArgumentException::assertIntegerGreaterThanOrEqual($retries, 1, __FUNCTION__, 2);

Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @return array
* @no-named-arguments
*/
function select($collection, callable $callback = null)
function select($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
10 changes: 5 additions & 5 deletions src/Functional/Sequences/ExponentialSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@ public function __construct($start, $percentage)
$this->percentage = $percentage;
}

public function current()
public function current(): int
{
return $this->value;
}

public function next()
public function next(): void
{
$this->value = (int) \round(\pow($this->start * (1 + $this->percentage / 100), $this->times));
$this->times++;
}

public function key()
public function key(): ?int
{
return null;
}

public function valid()
public function valid(): bool
{
return true;
}

public function rewind()
public function rewind(): void
{
$this->times = 1;
$this->value = $this->start;
Expand Down
10 changes: 5 additions & 5 deletions src/Functional/Sequences/LinearSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ public function __construct($start, $amount)
$this->amount = $amount;
}

public function current()
public function current(): int
{
return $this->value;
}

public function next()
public function next(): void
{
$this->value += $this->amount;
}

public function key()
public function key(): int
{
return 0;
}

public function valid()
public function valid(): bool
{
return true;
}

public function rewind()
public function rewind(): void
{
$this->value = $this->start;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @return bool
* @no-named-arguments
*/
function some($collection, callable $callback = null)
function some($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* Takes an optional callback for filtering the collection.
*
* @param Traversable|array $collection
* @param callable $callback
* @param callable|null $callback
* @return array
* @no-named-arguments
*/
function tail($collection, callable $callback = null)
function tail($collection, ?callable $callback = null)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* Returns an array of unique elements
*
* @param Traversable|array $collection
* @param callable $callback
* @param callable|null $callback
* @param bool $strict
* @return array
* @no-named-arguments
*/
function unique($collection, callable $callback = null, $strict = true)
function unique($collection, ?callable $callback = null, $strict = true)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

Expand Down
8 changes: 8 additions & 0 deletions tests/Functional/ErrorToExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class ErrorToExceptionTest extends AbstractTestCase
{
public function testErrorIsThrownAsException(): void
{
if (PHP_VERSION_ID >= 80401) {
// As of PHP 8.4, trigger_error('...', E_USER_ERROR) is deprecated
// This means the actual error thrown is different from the one
// attempted
// See https://3v4l.org/MmFA6.
self::markTestSkipped('Only works with PHP <8.4 due to deprecated E_USER_ERROR');
}

$origFn = function () {
\trigger_error('Some error', E_USER_ERROR);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using E_DEPRECATED instead

};
Expand Down
9 changes: 7 additions & 2 deletions tests/Functional/RepeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ public function test(): void
public function testNegativeRepeatedTimes(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Functional\{closure}() expects parameter 1 to be positive integer, negative integer given'

// See https://3v4l.org/Ms79G for message formats
// See https://regex101.com/r/hTvW3o/1 for regex setup
$this->expectExceptionMessageMatches(
'/(Functional\\\\{closure}' // PHP < 8.4
. '|{closure:Functional\\\\repeat\(\):[0-9]+})' // PHP 8.4+
. '\(\) expects parameter 1 to be positive integer, negative integer given/'
);

repeat([$this->repeated, 'foo'])(-1);
Expand Down