Skip to content

Commit

Permalink
Merge pull request #165 from boesing/feature/orderedlist-removeat
Browse files Browse the repository at this point in the history
feature: add `OrderedListInterface#removeAt`
  • Loading branch information
boesing committed May 30, 2022
2 parents 451e911 + b063e7f commit 34c5f65
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
<exclude name="Generic.NamingConventions.ConstructorName.OldStyle"/>
<exclude name="Generic.CodeAnalysis.UselessOverridingMethod.Found"/>
<exclude name="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
<exclude name="Generic.Files.LineLength.TooLong"/>
<!-- Can be removed with dropping support for PHP 7.3 -->
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
Expand Down
5 changes: 5 additions & 0 deletions src/OrderedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,9 @@ public function prepend($value): OrderedListInterface

return $instance;
}

public function removeAt(int $index): OrderedListInterface
{
return $this->filter(static fn ($_, int $i) => $i !== $index);
}
}
8 changes: 8 additions & 0 deletions src/OrderedListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,12 @@ public function findFirstMatchingIndex(callable $filter): ?int;
* @return OrderedListInterface<TValue>
*/
public function prepend($value): self;

/**
* Removes an element at the given index.
*
* @param 0|positive-int $index
* @return OrderedListInterface<TValue>
*/
public function removeAt(int $index): self;
}
12 changes: 12 additions & 0 deletions tests/GenericOrderedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1478,4 +1478,16 @@ public function testReduceWillReturnInitialValueOnEmptyList(): void
$list = new GenericOrderedList([]);
self::assertSame(PHP_INT_MAX, $list->reduce(static fn () => 1, PHP_INT_MAX));
}

public function testWillRemoveItemAtSpecificPosition(): void
{
$list = new GenericOrderedList([
1,
2,
3,
]);

$list = $list->removeAt(1);
self::assertSame([1, 3], $list->toNativeArray());
}
}

0 comments on commit 34c5f65

Please sign in to comment.