Skip to content

Commit 34c5f65

Browse files
authored
Merge pull request #165 from boesing/feature/orderedlist-removeat
feature: add `OrderedListInterface#removeAt`
2 parents 451e911 + b063e7f commit 34c5f65

4 files changed

+26
-0
lines changed

phpcs.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
2828
<exclude name="Generic.NamingConventions.ConstructorName.OldStyle"/>
2929
<exclude name="Generic.CodeAnalysis.UselessOverridingMethod.Found"/>
30+
<exclude name="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
3031
<exclude name="Generic.Files.LineLength.TooLong"/>
3132
<!-- Can be removed with dropping support for PHP 7.3 -->
3233
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>

src/OrderedList.php

+5
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,9 @@ public function prepend($value): OrderedListInterface
417417

418418
return $instance;
419419
}
420+
421+
public function removeAt(int $index): OrderedListInterface
422+
{
423+
return $this->filter(static fn ($_, int $i) => $i !== $index);
424+
}
420425
}

src/OrderedListInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,12 @@ public function findFirstMatchingIndex(callable $filter): ?int;
256256
* @return OrderedListInterface<TValue>
257257
*/
258258
public function prepend($value): self;
259+
260+
/**
261+
* Removes an element at the given index.
262+
*
263+
* @param 0|positive-int $index
264+
* @return OrderedListInterface<TValue>
265+
*/
266+
public function removeAt(int $index): self;
259267
}

tests/GenericOrderedListTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -1478,4 +1478,16 @@ public function testReduceWillReturnInitialValueOnEmptyList(): void
14781478
$list = new GenericOrderedList([]);
14791479
self::assertSame(PHP_INT_MAX, $list->reduce(static fn () => 1, PHP_INT_MAX));
14801480
}
1481+
1482+
public function testWillRemoveItemAtSpecificPosition(): void
1483+
{
1484+
$list = new GenericOrderedList([
1485+
1,
1486+
2,
1487+
3,
1488+
]);
1489+
1490+
$list = $list->removeAt(1);
1491+
self::assertSame([1, 3], $list->toNativeArray());
1492+
}
14811493
}

0 commit comments

Comments
 (0)