From 68ad0fd5224ff7e172902ea1da35d269c9addd28 Mon Sep 17 00:00:00 2001 From: Etienne LE PETIT Date: Thu, 15 Dec 2022 17:55:36 +0100 Subject: [PATCH] [FND-X] Fix functional-php bugs --- src/Functional/FirstIndexOf.php | 14 +++----------- src/Functional/IndexesOf.php | 16 ++++------------ src/Functional/LastIndexOf.php | 14 +++----------- src/Functional/With.php | 9 +-------- tests/Functional/FirstIndexOfTest.php | 10 ---------- tests/Functional/LastIndexOfTest.php | 10 ---------- tests/Functional/WithTest.php | 1 - 7 files changed, 11 insertions(+), 63 deletions(-) diff --git a/src/Functional/FirstIndexOf.php b/src/Functional/FirstIndexOf.php index c341942e..0fb5a0ad 100644 --- a/src/Functional/FirstIndexOf.php +++ b/src/Functional/FirstIndexOf.php @@ -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; } } diff --git a/src/Functional/IndexesOf.php b/src/Functional/IndexesOf.php index 3045df6b..74f05b77 100644 --- a/src/Functional/IndexesOf.php +++ b/src/Functional/IndexesOf.php @@ -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 */ @@ -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; } } diff --git a/src/Functional/LastIndexOf.php b/src/Functional/LastIndexOf.php index afca8fc5..fc540193 100644 --- a/src/Functional/LastIndexOf.php +++ b/src/Functional/LastIndexOf.php @@ -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; } } diff --git a/src/Functional/With.php b/src/Functional/With.php index 9e4532e5..0c00ddd3 100644 --- a/src/Functional/With.php +++ b/src/Functional/With.php @@ -17,12 +17,11 @@ * * @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 * @no-named-arguments */ -function with($value, callable $callback, $invokeValue = true, $default = null) +function with($value, callable $callback, $default = null) { InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2); @@ -30,11 +29,5 @@ function with($value, callable $callback, $invokeValue = true, $default = 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); } diff --git a/tests/Functional/FirstIndexOfTest.php b/tests/Functional/FirstIndexOfTest.php index 1b8b075b..0bbec5af 100644 --- a/tests/Functional/FirstIndexOfTest.php +++ b/tests/Functional/FirstIndexOfTest.php @@ -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'); diff --git a/tests/Functional/LastIndexOfTest.php b/tests/Functional/LastIndexOfTest.php index df2a1d8c..5e71521f 100644 --- a/tests/Functional/LastIndexOfTest.php +++ b/tests/Functional/LastIndexOfTest.php @@ -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'); diff --git a/tests/Functional/WithTest.php b/tests/Functional/WithTest.php index 86723dae..7045ea4a 100644 --- a/tests/Functional/WithTest.php +++ b/tests/Functional/WithTest.php @@ -50,7 +50,6 @@ public function testDefaultValue(): void null, static function () { }, - false, 'foo' ) );