Skip to content

Commit

Permalink
Merge pull request bocharsky-bw#24 from znck:master
Browse files Browse the repository at this point in the history
Fixes bocharsky-bw#21: Immutable array could be modified with some methods
  • Loading branch information
znck authored and bocharsky-bw committed Oct 11, 2015
1 parent 52dcaf0 commit c8382fa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
58 changes: 58 additions & 0 deletions src/ImmutableArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ public function mergeTo(array $array, $recursively = false)
return new static(array_merge($array, $this->elements));
}

/**
* Push one or more values onto the end of array at once.
*
* @param mixed $element The pushed element
* @param mixed $_ [optional] Multiple arguments allowed
*
* @return static The new instance with pushed elements to the end of array
*/
public function push($element, $_ = null)
{
$elements = $this->elements;
if (func_num_args()) {
$args = array_merge([&$elements], func_get_args());
call_user_func_array('array_push', $args);
}

return new static($elements);
}

/**
* Pop a specified value off the end of array.
*
* @return mixed The popped element
*/
public function pop()
{
return $this->count() ? $this->last() : null;
}

/**
* Replace array with given one
*
Expand Down Expand Up @@ -197,6 +226,35 @@ public function diffWith(array $array)
return new static(array_diff($this->elements, $array));
}

/**
* Unshift array
*
* @param mixed $element The element for prepend
* @param mixed $_ [optional] Multiple arguments allowed
*
* @return static The new instance with prepended elements to the beginning of array
*/
public function unshift($element, $_ = null)
{
$elements = $this->elements;
if (func_num_args()) {
$args = array_merge([&$elements], func_get_args());
call_user_func_array('array_unshift', $args);
}

return new static($elements);
}

/**
* Shifts a specified value off the beginning of array.
*
* @return mixed The shifted element
*/
public function shift()
{
return $this->count() ? $this->first() : null;
}

/**
* Shuffle array
*
Expand Down
10 changes: 6 additions & 4 deletions tests/ImmutableArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,11 @@ public function testReduce(array $array)
public function testShift(array $array)
{
$ma = new ImmutableArray($array);
$copyMa = new ImmutableArray($array);
$shiftedValue = $ma->shift();
$shiftedArrayValue = array_shift($array);

$this->assertTrue($array === $ma->toArray());
$this->assertTrue($copyMa->toArray() === $ma->toArray());
$this->assertTrue($shiftedArrayValue === $shiftedValue);
}

Expand All @@ -575,7 +576,7 @@ public function testUnshift(array $array)
$copiedMa = $ma->unshift($newElement1, $newElement2);
array_unshift($array, $newElement1, $newElement2);

$this->assertTrue($copiedMa === $ma);
$this->assertTrue($copiedMa->toArray() !== $ma->toArray());
$this->assertTrue($array === $copiedMa->toArray());
}

Expand All @@ -585,10 +586,11 @@ public function testUnshift(array $array)
public function testPop(array $array)
{
$ma = new ImmutableArray($array);
$copyMa = new ImmutableArray($array);
$poppedValue = $ma->pop();
$poppedArrayValue = array_pop($array);

$this->assertTrue($array === $ma->toArray());
$this->assertTrue($copyMa->toArray() === $ma->toArray());
$this->assertTrue($poppedArrayValue === $poppedValue);
}

Expand All @@ -603,7 +605,7 @@ public function testPush(array $array)
$copiedMa = $ma->push($newElement1, $newElement2);
array_push($array, $newElement1, $newElement2);

$this->assertTrue($copiedMa === $ma);
$this->assertTrue($copiedMa->toArray() !== $ma->toArray());
$this->assertTrue($array === $copiedMa->toArray());
}

Expand Down

0 comments on commit c8382fa

Please sign in to comment.