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 committed Dec 15, 2022
1 parent 5f5ddb6 commit 68ad0fd
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 63 deletions.
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
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
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
9 changes: 1 addition & 8 deletions src/Functional/With.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@
*
* @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);

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
1 change: 0 additions & 1 deletion tests/Functional/WithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function testDefaultValue(): void
null,
static function () {
},
false,
'foo'
)
);
Expand Down

0 comments on commit 68ad0fd

Please sign in to comment.