Fix slice and sliceSelf when collection is empty

master 3.5.1
Julien Rosset 2 years ago
parent 66fe43fef5
commit 281885a2d5

@ -126,9 +126,13 @@ class Collection extends ImmutableCollection implements ICollection {
* @inheritDoc * @inheritDoc
*/ */
public function sliceSelf (int $offset, ?int $length = null): static { public function sliceSelf (int $offset, ?int $length = null): static {
if ($this->count() === 0) {
$this->elements = [];
}
else {
$this->_checkOffset($offset); $this->_checkOffset($offset);
$this->elements = array_slice($this->elements, $offset, $length, true); $this->elements = array_slice($this->elements, $offset, $length, true);
}
return $this; return $this;
} }

@ -289,9 +289,12 @@ class ImmutableCollection implements IImmutableCollection {
* @inheritDoc * @inheritDoc
*/ */
public function slice (int $offset, ?int $length = null): static { public function slice (int $offset, ?int $length = null): static {
$this->_checkOffset($offset);
$output = new static(); $output = new static();
if ($this->count() === 0) {
return $output;
}
$this->_checkOffset($offset);
$currentIndex = 0; $currentIndex = 0;
foreach ($this->elements as $key => $value) { foreach ($this->elements as $key => $value) {

Loading…
Cancel
Save