Skip to content

Commit

Permalink
[FND-X] Fix functional-php bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Moufle authored and KmeCnin committed Nov 24, 2023
1 parent 5f5ddb6 commit 48f568f
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 108 deletions.
11 changes: 8 additions & 3 deletions src/Functional/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Iterates over a collection of elements, yielding each in turn to a callback function. Each invocation of $callback
* is called with three arguments: (element, index, collection)
*
* @param Traversable|array $collection
* @param callable $callback
* @return null
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):void $callback
*
* @return void
*
* @no-named-arguments
*/
function each($collection, callable $callback)
Expand Down
9 changes: 7 additions & 2 deletions src/Functional/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Returns true if every value in the collection passes the callback truthy test. Opposite of Functional\none().
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return bool
*
* @no-named-arguments
*/
function every($collection, callable $callback = null)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
/**
* Alias of Functional\select()
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return array<K,V>
*
* @no-named-arguments
*/
function filter($collection, callable $callback)
Expand Down
10 changes: 7 additions & 3 deletions src/Functional/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
* function returns as soon as it finds an acceptable element, and doesn't traverse the entire collection. Callback
* arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
* @template T
*
* @param iterable<T> $collection
* @param null|callable(T):bool $callback
*
* @return ($collection is non-empty-array ? ($callback is null ? T : T|null) : T|null)
*
* @no-named-arguments
*/
function first($collection, callable $callback = null)
Expand Down
14 changes: 3 additions & 11 deletions src/Functional/FirstIndexOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ function first_index_of($collection, $value)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);

if (\is_callable($value)) {
foreach ($collection as $index => $element) {
if ($element === $value($element, $index, $collection)) {
return $index;
}
}
} else {
foreach ($collection as $index => $element) {
if ($element === $value) {
return $index;
}
foreach ($collection as $index => $element) {
if ($element === $value) {
return $index;
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Functional/FlatMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
* then flat_map(collection, callback) will return [1,2,3,[4]]
* while flatten(map(collection, callback)) will return [1,2,3,4]
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):(V2|iterable<V2>) $callback
*
* @return array<K,V2>
* @no-named-arguments
*/
function flat_map($collection, callable $callback)
Expand Down
8 changes: 6 additions & 2 deletions src/Functional/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
/**
* Return value itself, without any modifications.
*
* @param mixed $value
* @return mixed
* @template T
*
* @param T $value
*
* @return T
*
* @no-named-arguments
*/
function id($value)
Expand Down
16 changes: 4 additions & 12 deletions src/Functional/IndexesOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* empty array if no values were found.
*
* @param Traversable|array $collection
* @param mixed|callable $value
* @param mixed $value
* @return array
* @no-named-arguments
*/
Expand All @@ -28,17 +28,9 @@ function indexes_of($collection, $value)

$result = [];

if (\is_callable($value)) {
foreach ($collection as $index => $element) {
if ($element === $value($element, $index, $collection)) {
$result[] = $index;
}
}
} else {
foreach ($collection as $index => $element) {
if ($element === $value) {
$result[] = $index;
}
foreach ($collection as $index => $element) {
if ($element === $value) {
$result[] = $index;
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/Functional/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* Looks through each element in the collection, returning the last one that passes a truthy test (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return mixed
* @template T
*
* @param iterable<T> $collection
* @param null|callable(T):bool $callback
*
* @return T|null
*
* @no-named-arguments
*/
function last($collection, callable $callback = null)
Expand Down
14 changes: 3 additions & 11 deletions src/Functional/LastIndexOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@ function last_index_of($collection, $value)

$matchingIndex = false;

if (\is_callable($value)) {
foreach ($collection as $index => $element) {
if ($element === $value($element, $index, $collection)) {
$matchingIndex = $index;
}
}
} else {
foreach ($collection as $index => $element) {
if ($element === $value) {
$matchingIndex = $index;
}
foreach ($collection as $index => $element) {
if ($element === $value) {
$matchingIndex = $index;
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/Functional/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
* Produces a new array of elements by mapping each element in collection through a transformation function (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):V2 $callback
*
* @return array<K,V2>
*
* @no-named-arguments
*/
function map($collection, callable $callback)
Expand Down
14 changes: 10 additions & 4 deletions src/Functional/ReduceLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
use Traversable;

/**
* @param Traversable|array $collection
* @param callable $callback
* @param mixed $initial
* @return mixed
* @template K
* @template V
* @template V2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>,V2):V2 $callback
* @param V2 $initial
*
* @return V2
*
* @no-named-arguments
*/
function reduce_left($collection, callable $callback, $initial = null)
Expand Down
12 changes: 9 additions & 3 deletions src/Functional/Reindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
* Produces a new array of elements by assigning the values to keys generated by a transformation function (callback).
* Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable $callback
* @return array
* @template K
* @template V
* @template K2
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):K2 $callback
*
* @return array<K2,V>
*
* @no-named-arguments
*/
function reindex($collection, callable $callback)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Looks through each element in the list, returning an array of all the elements that pass a truthy test (callback).
* Opposite is Functional\reject(). Callback arguments will be element, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @return array
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return array<K,V>
*
* @no-named-arguments
*/
function select($collection, callable $callback = null)
Expand Down
9 changes: 7 additions & 2 deletions src/Functional/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* Returns true if some of the elements in the collection pass the callback truthy test. Short-circuits and stops
* traversing the collection if a truthy element is found. Callback arguments will be value, index, collection
*
* @param Traversable|array $collection
* @param callable|null $callback
* @template K
* @template V
*
* @param iterable<K,V> $collection
* @param callable(V,K,iterable<K,V>):bool $callback
*
* @return bool
*
* @no-named-arguments
*/
function some($collection, callable $callback = null)
Expand Down
11 changes: 8 additions & 3 deletions src/Functional/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
/**
* Takes a collection and returns the sum of the elements
*
* @param Traversable|array $collection
* @param integer|float $initial
* @return integer|float
* @template K
* @template V of int|float
*
* @param iterable<K,V> $collection
* @param V $initial
*
* @return V
*
* @no-named-arguments
*/
function sum($collection, $initial = 0)
Expand Down
13 changes: 9 additions & 4 deletions src/Functional/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
/**
* Returns an array of unique elements
*
* @param Traversable|array $collection
* @param callable $callback
* @param bool $strict
* @return array
* @template K
* @template V
* @template D
*
* @param iterable<K,V> $collection
* @param null|callable(V,K,iterable<K,V>):D $callback
*
* @return array<K,V>
*
* @no-named-arguments
*/
function unique($collection, callable $callback = null, $strict = true)
Expand Down
23 changes: 11 additions & 12 deletions src/Functional/With.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,25 @@
/**
* Invoke a callback on a value if the value is not null
*
* @param mixed $value
* @param callable $callback
* @param bool $invokeValue Set to false to not invoke $value if it is a callable. Will be removed in 2.0
* @param mixed $default The default value to return if $value is null
* @return mixed
* @template V
* @template V2
* @template V3
*
* @param V|null $value
* @param callable(V):V2 $callback
* @param V3 $default The default value to return if $value is null
*
* @return ($value is null ? V3 : V2)
*
* @no-named-arguments
*/
function with($value, callable $callback, $invokeValue = true, $default = null)
function with($value, callable $callback, $default = null)
{
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);

if ($value === null) {
return $default;
}

if ($invokeValue && \is_callable($value)) {
\trigger_error('Invoking the value is deprecated and will be removed in 2.0', E_USER_DEPRECATED);

$value = $value();
}

return $callback($value);
}
10 changes: 0 additions & 10 deletions tests/Functional/FirstIndexOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public function testIfValueCouldNotBeFoundFalseIsReturned(): void
self::assertFalse(first_index_of($this->hashIterator, 'invalidValue'));
}

public function testPassCallback(): void
{
self::assertSame(
0,
first_index_of($this->list, function ($element) {
return $element;
})
);
}

public function testPassNoCollection(): void
{
$this->expectArgumentError('Functional\first_index_of() expects parameter 1 to be array or instance of Traversable');
Expand Down
10 changes: 0 additions & 10 deletions tests/Functional/LastIndexOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ public function testIfValueCouldNotBeFoundFalseIsReturned(): void
self::assertFalse(last_index_of($this->hashIterator, 'invalidValue'));
}

public function testPassCallback(): void
{
self::assertSame(
3,
last_index_of($this->list, function ($element) {
return $element;
})
);
}

public function testPassNoCollection(): void
{
$this->expectArgumentError('Functional\last_index_of() expects parameter 1 to be array or instance of Traversable');
Expand Down
Loading

0 comments on commit 48f568f

Please sign in to comment.