Skip to content

Commit

Permalink
refactor: Use call_user_func_array() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Mar 31, 2021
1 parent 1e8de4a commit 614a663
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/Operation/AsyncMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function Amp\ParallelFunctions\parallel;
use function Amp\Promise\wait;
use function Amp\Sync\ConcurrentIterator\map;
use function call_user_func_array;
use function function_exists;

// phpcs:disable
Expand Down Expand Up @@ -65,7 +66,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*
* @psalm-return T
*/
static fn ($carry, callable $callback) => $callback($carry, $key);
static fn ($carry, callable $callback) => call_user_func_array($callback, [$carry, $key]);

$callback =
/**
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -60,7 +62,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

$result = true;

Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -52,7 +54,7 @@ static function (callable ...$callbacks): Closure {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

return
/**
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -62,7 +64,7 @@ static function (?callable $callable = null): Closure {
* @psalm-return non-empty-array<TKey, T|list<T>>
*/
static function (array $collect, $value, $key) use ($callback): array {
if (null !== $groupKey = $callback($value, $key)) {
if (null !== $groupKey = call_user_func_array($callback, [$value, $key])) {
$collect[$groupKey][] = $value;
} else {
$collect[$key] = $value;
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -52,7 +54,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*
* @psalm-param Iterator<TKey, T> $iterator
*/
static fn ($value, $key, Iterator $iterator): bool => $callback($value, $key, $iterator) === $value,
static fn ($value, $key, Iterator $iterator): bool => call_user_func_array($callback, [$value, $key, $iterator]) === $value,
$callbacks
);

Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -48,7 +50,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
*
* @psalm-return T
*/
static fn ($carry, callable $callback) => $callback($carry, $key, $iterator);
static fn ($carry, callable $callback) => call_user_func_array($callback, [$carry, $key, $iterator]);

foreach ($iterator as $key => $value) {
yield $key => array_reduce($callbacks, $callbackFactory($key), $value);
Expand Down
6 changes: 4 additions & 2 deletions src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -54,7 +56,7 @@ static function (callable ...$callbacks) use ($matcher): Closure {
*/
static fn ($value, $key, Iterator $iterator): bool => array_reduce(
$callbacks,
static fn (bool $carry, callable $callback): bool => $carry || $callback($value, $key, $iterator),
static fn (bool $carry, callable $callback): bool => $carry || call_user_func_array($callback, [$value, $key, $iterator]),
false
);

Expand All @@ -70,7 +72,7 @@ static function (callable ...$callbacks) use ($matcher): Closure {
*
* @psalm-param Iterator<TKey, T> $iterator
*/
static fn ($value, $key, Iterator $iterator): bool => $matcher($value, $key, $iterator) === $callback($value, $key, $iterator);
static fn ($value, $key, Iterator $iterator): bool => call_user_func_array($matcher, [$value, $key, $iterator]) === call_user_func_array($callback, [$value, $key, $iterator]);

$dropWhileCallback =
/**
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Partition.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -60,7 +62,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

$true = $false = [];

Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -43,7 +45,7 @@ public function __invoke(): Closure
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
foreach ($iterator as $key => $value) {
yield $key => ($initial = $callback($initial, $value, $key, $iterator));
yield $key => ($initial = call_user_func_array($callback, [$initial, $value, $key, $iterator]));
}
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Since.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -60,7 +62,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

$result = false;

Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Iterator;
use loophp\collection\Contract\Operation;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -76,7 +78,7 @@ static function (Iterator $iterator) use ($type, $callback): Iterator {
* @psalm-param array{0:TKey|T, 1:T|TKey} $left
* @psalm-param array{0:TKey|T, 1:T|TKey} $right
*/
static fn (array $left, array $right): int => $callback($left[1], $right[1]);
static fn (array $left, array $right): int => call_user_func_array($callback, [$left[1], $right[1]]);

/** @psalm-var callable(Iterator<TKey, T>): Generator<int, array{0:TKey, 1:T}> | callable(Iterator<TKey, T>): Generator<int, array{0:T, 1:TKey}> $before */
$before = Pipe::of()(...$operations['before']);
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Iterator;
use loophp\collection\Contract\Operation\Splitable;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -67,7 +69,7 @@ static function (Iterator $iterator) use ($type, $callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

foreach ($iterator as $key => $value) {
$callbackReturn = array_reduce(
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -60,7 +62,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

foreach ($iterator as $key => $current) {
$result = array_reduce(
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Times.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Generator;
use Iterator;

use function call_user_func;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -45,7 +47,7 @@ static function (?Iterator $iterator = null) use ($number, $callback): Generator
$callback ??= static fn (int $value): int => $value;

for ($current = 1; $current <= $number; ++$current) {
yield $callback($current);
yield call_user_func($callback, $current);
}
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Unfold.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Closure;
use Generator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -41,7 +43,7 @@ public function __invoke(): Closure
static function () use ($parameters, $callback): Generator {
while (true) {
/** @psalm-var T $parameters */
$parameters = $callback(...array_values((array) $parameters));
$parameters = call_user_func_array($callback, array_values((array) $parameters));

yield $parameters;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Operation/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Generator;
use Iterator;

use function call_user_func_array;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand Down Expand Up @@ -60,7 +62,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);
static fn (bool $carry, callable $callable): bool => $carry || call_user_func_array($callable, [$current, $key, $iterator]);

foreach ($iterator as $key => $current) {
yield $key => $current;
Expand Down

5 comments on commit 614a663

@jdreesen
Copy link
Contributor

Choose a reason for hiding this comment

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

May I ask what's the difference in using call_user_func_array instead of calling it directly?

@drupol
Copy link
Collaborator Author

@drupol drupol commented on 614a663 Apr 4, 2021

Choose a reason for hiding this comment

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

Sure !

Basically because every callable in loophp/collection is typed using callable and not Closure.

A callable in PHP can be expressed with different types: string, array, closure or object. In order to honor the type, I must use call_user_func_* functions.

I quickly drafted a code example: https://3v4l.org/6jDFE play with it.

Try to replace the call to call_user_func_array with $callable($item);...

@drupol
Copy link
Collaborator Author

@drupol drupol commented on 614a663 Apr 4, 2021

Choose a reason for hiding this comment

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

I just noticed that this is invalid in php >= 7 !

I will have to revert that commit and provide a new release.

Thanks for asking @jdreesen, thanks to you I learned something new with PHP today :-)

@drupol
Copy link
Collaborator Author

@drupol drupol commented on 614a663 Apr 4, 2021

Choose a reason for hiding this comment

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

I reverted the commit and tagged 4.0.2.

Thanks @jdreesen !

@jdreesen
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation!

I'm glad I could help you learn something new by asking a question 😁

Please sign in to comment.