diff --git a/docs/pages/api.rst b/docs/pages/api.rst index 00ed84cc3..83f418c9a 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -1163,16 +1163,16 @@ Signature: ``Collection::split(callable ...$callbacks);`` tail ~~~~ -Get last collection items of a collection. +Get the collection items except the first. Interface: `Tailable`_ -Signature: ``Collection::tail(int $length = 1);`` +Signature: ``Collection::tail();`` .. code-block:: php - $collection = Collection::with(['a', 'b', 'c']) - ->tail(2); + Collection::with(['a', 'b', 'c']) + ->tail(); // [1 => 'b', 2 => 'c'] transpose ~~~~~~~~~ diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index f5fe67fae..8b32173d7 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -15,7 +15,6 @@ use loophp\collection\Contract\Operation; use loophp\collection\Contract\Transformation; use loophp\collection\Operation\AbstractOperation; -use OutOfRangeException; use PhpSpec\ObjectBehavior; use stdClass; @@ -1785,27 +1784,7 @@ public function it_can_tail(): void { $this::fromIterable(range('A', 'F')) ->tail() - ->shouldIterateAs([5 => 'F']); - - $this::fromIterable(range('A', 'F')) - ->tail(3) - ->shouldIterateAs([3 => 'D', 4 => 'E', 5 => 'F']); - - $this::fromIterable(range('A', 'F')) - ->tail(-5) - ->shouldThrow(OutOfRangeException::class) - ->during('all'); - - $this::fromIterable(range('A', 'F')) - ->tail(100) - ->shouldIterateAs(range('A', 'F')); - - $this::fromIterable(['a', 'b', 'c', 'd', 'a']) - ->flip() - ->flip() - ->tail(2) - ->all() - ->shouldIterateAs([3 => 'd', 4 => 'a']); + ->shouldIterateAs([1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F']); } public function it_can_transpose(): void diff --git a/src/Collection.php b/src/Collection.php index 0a75da544..2fa69bf90 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -617,9 +617,9 @@ public function split(callable ...$callbacks): CollectionInterface return $this->run(new Split(...$callbacks)); } - public function tail(?int $length = null): CollectionInterface + public function tail(): CollectionInterface { - return $this->run(new Tail($length)); + return $this->run(new Tail()); } public static function times(int $number = 0, ?callable $callback = null): CollectionInterface diff --git a/src/Contract/Operation/Tailable.php b/src/Contract/Operation/Tailable.php index 05d0fb784..bbf38c1fe 100644 --- a/src/Contract/Operation/Tailable.php +++ b/src/Contract/Operation/Tailable.php @@ -14,9 +14,7 @@ interface Tailable { /** - * Get last collection items of a collection. - * * @psalm-return \loophp\collection\Contract\Collection */ - public function tail(?int $length = null): Collection; + public function tail(): Collection; } diff --git a/src/Operation/Tail.php b/src/Operation/Tail.php index 9ec2e2951..9f19f6efd 100644 --- a/src/Operation/Tail.php +++ b/src/Operation/Tail.php @@ -17,11 +17,6 @@ */ final class Tail extends AbstractOperation implements Operation { - public function __construct(?int $length = null) - { - $this->storage['length'] = $length ?? 1; - } - public function __invoke(): Closure { return @@ -30,13 +25,8 @@ public function __invoke(): Closure * * @psalm-return \Generator */ - static function (Iterator $iterator, int $length): Generator { - return yield from ( - new Run( - new Skip(iterator_count($iterator) - $length), - new Limit($length) - ) - )($iterator); + static function (Iterator $iterator): Generator { + return yield from (new Run(new Skip(1)))($iterator); }; } }