Browse files

API Prep ArrayList for immutability in 3.1 per 7673

ArrayList had several methods that should act on a copy and return
that copy, but was instead mutating the existing list.

We cant change this behaviour in the 3.0 line for backwards compt.
reasons, but this makes the desired behavior the default, and
makes disabling the mutation in 3.1 easier
  • Loading branch information...
1 parent c9b3430 commit 1ed41b8d67bc3a785ac22a73ecf86b1e26ac594c Hamish Friedlander committed Jul 20, 2012
Showing with 20 additions and 12 deletions.
  1. +20 −12 model/ArrayList.php
View
32 model/ArrayList.php
@@ -300,9 +300,11 @@ public function canSortBy($by) {
* @return ArrayList
*/
public function reverse() {
- $this->items = array_reverse($this->items);
+ // TODO 3.1: This currently mutates existing array
+ $list = /* clone */ $this;
+ $list->items = array_reverse($this->items);
- return $this;
+ return $list;
}
/**
@@ -362,11 +364,13 @@ public function sort() {
// First argument is the direction to be sorted,
$multisortArgs[] = &$sortDirection[$column];
}
- // As the last argument we pass in a reference to the items that all the sorting will be
- // applied upon
- $multisortArgs[] = &$this->items;
+
+ // TODO 3.1: This currently mutates existing array
+ $list = /* clone */ $this;
+ // As the last argument we pass in a reference to the items that all the sorting will be applied upon
+ $multisortArgs[] = &$list->items;
call_user_func_array('array_multisort', $multisortArgs);
- return $this;
+ return $list;
}
/**
@@ -424,8 +428,10 @@ public function filter() {
}
}
- $this->items = $itemsToKeep;
- return $this;
+ // TODO 3.1: This currently mutates existing array
+ $list = /* clone */ $this;
+ $list->items = $itemsToKeep;
+ return $list;
}
public function byID($id) {
@@ -478,12 +484,14 @@ public function exclude() {
}
$keysToRemove = array_keys($matches,$hitsRequiredToRemove);
+ // TODO 3.1: This currently mutates existing array
+ $list = /* clone */ $this;
+
foreach($keysToRemove as $itemToRemoveIdx){
- $this->remove($this->items[$itemToRemoveIdx]);
+ $list->remove($this->items[$itemToRemoveIdx]);
}
- return;
-
- return $this;
+
+ return $list;
}
protected function shouldExclude($item, $args) {

0 comments on commit 1ed41b8

Please sign in to comment.