Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenhaas committed Nov 17, 2022
1 parent 6fee242 commit 801a195
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/GitElephant/Sequence/AbstractSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use OutOfBoundsException;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;

/**
Expand All @@ -44,12 +45,13 @@ public function __construct(array $elements = [])
$this->elements = array_values($elements);
}

public function addSequence(SequenceInterface $seq)
public function addSequence(SequenceInterface $seq): SequenceInterface
{
$this->addAll($seq->all());
return $this;
}

public function indexOf($elem)
public function indexOf($elem): int
{
foreach ($this->elements as $i => $element) {
if ($elem === $element) {
Expand All @@ -71,7 +73,7 @@ public function lastIndexOf($elem): int
return -1;
}

public function reverse(): CollectionInterface
public function reverse(): SequenceInterface
{
return $this->createNew(array_reverse($this->elements));
}
Expand Down Expand Up @@ -181,7 +183,7 @@ public function lastIndexWhere(callable $callable): int
return -1;
}

public function last()
public function last(): Option
{
if (empty($this->elements)) {
return None::create();
Expand All @@ -190,7 +192,7 @@ public function last()
return new Some(end($this->elements));
}

public function first()
public function first(): Option
{
if (empty($this->elements)) {
return None::create();
Expand All @@ -204,11 +206,6 @@ public function indices(): array
return array_keys($this->elements);
}

/**
* Returns an element based on its index (0-based).
*
* @param integer $index
*/
public function get(int $index)
{
if (!isset($this->elements[$index])) {
Expand All @@ -218,13 +215,6 @@ public function get(int $index)
return $this->elements[$index];
}

/**
* Removes the element at the given index, and returns it.
*
* @param int $index
*
* @throws \OutOfBoundsException If there is no element at the given index.
*/
public function remove(int $index)
{
if (!isset($this->elements[$index])) {
Expand All @@ -238,12 +228,6 @@ public function remove(int $index)
return $element;
}

/**
* Updates the element at the given index (0-based).
*
* @param integer $index
* @param mixed $value
*/
public function update(int $index, $value): void
{
if (!isset($this->elements[$index])) {
Expand Down Expand Up @@ -307,7 +291,7 @@ public function takeWhile(callable $callable): CollectionInterface
return $this->createNew($newElements);
}

public function drop(int $number): CollectionInterface
public function drop(int $number): SequenceInterface
{
if ($number <= 0) {
throw new \InvalidArgumentException(sprintf('The number must be greater than 0, but got %d.', $number));
Expand All @@ -316,7 +300,7 @@ public function drop(int $number): CollectionInterface
return $this->createNew(array_slice($this->elements, $number));
}

public function dropRight(int $number): CollectionInterface
public function dropRight(int $number): SequenceInterface
{
if ($number <= 0) {
throw new \InvalidArgumentException(sprintf('The number must be greater than 0, but got %d.', $number));
Expand All @@ -325,7 +309,7 @@ public function dropRight(int $number): CollectionInterface
return $this->createNew(array_slice($this->elements, 0, -1 * $number));
}

public function dropWhile(callable $callable): CollectionInterface
public function dropWhile(callable $callable): SequenceInterface
{
$i = 0;
foreach ($this->elements as $i => $iValue) {
Expand Down Expand Up @@ -358,7 +342,7 @@ public function getIterator(): \ArrayIterator
return new \ArrayIterator($this->elements ?: []);
}

protected function createNew(array $elements): CollectionInterface
protected function createNew(array $elements): SequenceInterface
{
return new static($elements);
}
Expand Down

0 comments on commit 801a195

Please sign in to comment.