Skip to content
This repository
Browse code

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...
commit 1ed41b8d67bc3a785ac22a73ecf86b1e26ac594c 1 parent c9b3430
Hamish Friedlander authored July 20, 2012

Showing 1 changed file with 20 additions and 12 deletions. Show diff stats Hide diff stats

  1. 32  model/ArrayList.php
32  model/ArrayList.php
@@ -300,9 +300,11 @@ public function canSortBy($by) {
300 300
 	 * @return ArrayList
301 301
 	 */
302 302
 	public function reverse() {
303  
-		$this->items = array_reverse($this->items);
  303
+		// TODO 3.1: This currently mutates existing array
  304
+		$list = /* clone */ $this;
  305
+		$list->items = array_reverse($this->items);
304 306
 		
305  
-		return $this;
  307
+		return $list;
306 308
 	}
307 309
 	
308 310
 	/**
@@ -362,11 +364,13 @@ public function sort() {
362 364
 			// First argument is the direction to be sorted, 
363 365
 			$multisortArgs[] = &$sortDirection[$column];
364 366
 		}
365  
-		// As the last argument we pass in a reference to the items that all the sorting will be 
366  
-		// applied upon
367  
-		$multisortArgs[] = &$this->items;
  367
+
  368
+		// TODO 3.1: This currently mutates existing array
  369
+		$list = /* clone */ $this;
  370
+		// As the last argument we pass in a reference to the items that all the sorting will be applied upon
  371
+		$multisortArgs[] = &$list->items;
368 372
 		call_user_func_array('array_multisort', $multisortArgs);
369  
-		return $this;
  373
+		return $list;
370 374
 	}
371 375
 	
372 376
 	/**
@@ -424,8 +428,10 @@ public function filter() {
424 428
 			}
425 429
 		}
426 430
 
427  
-		$this->items = $itemsToKeep;
428  
-		return $this;
  431
+		// TODO 3.1: This currently mutates existing array
  432
+		$list = /* clone */ $this;
  433
+		$list->items = $itemsToKeep;
  434
+		return $list;
429 435
 	}
430 436
 	
431 437
 	public function byID($id) {
@@ -478,12 +484,14 @@ public function exclude() {
478 484
 		}
479 485
 
480 486
 		$keysToRemove = array_keys($matches,$hitsRequiredToRemove);
  487
+		// TODO 3.1: This currently mutates existing array
  488
+		$list = /* clone */ $this;
  489
+
481 490
 		foreach($keysToRemove as $itemToRemoveIdx){
482  
-			$this->remove($this->items[$itemToRemoveIdx]);
  491
+			$list->remove($this->items[$itemToRemoveIdx]);
483 492
 		}
484  
-		return;
485  
-		
486  
-		return $this;
  493
+
  494
+		return $list;
487 495
 	}
488 496
 
489 497
 	protected function shouldExclude($item, $args) {

0 notes on commit 1ed41b8

Please sign in to comment.
Something went wrong with that request. Please try again.