php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47031 DualIterator example uses wrong constants
Submitted: 2009-01-07 19:03 UTC Modified: 2009-02-03 12:55 UTC
From: jpetso at gmx dot at Assigned: colder (profile)
Status: Closed Package: SPL related
PHP Version: 5.3.0alpha3 OS: *
Private report: No CVE-ID: None
 [2009-01-07 19:03 UTC] jpetso at gmx dot at
Description:
------------
Regarding the DualIterator example, current version:
http://cvs.php.net/viewvc.cgi/php-

In its API documentation, the key() method claims to use the
DualIterator::KEY_* constants, but the code makes use of CURRENT_*
instead.

Also, does the *_ARRAY option (which is used by default even for
key()) does not make a lot of sense because arrays cannot be used as
keys at least for foreach loops. (Might work with low-level iterator
usage, but then again keys should probably be *real* keys in any
case.)

It would be nice to have these issues fixed (diff attached further
down).

Related feature request, totally not in scope for this bug report
(sorry): it might be worthwhile to extend the flags to
{CURRENT,KEY}_{LHS,RHS}_{KEY,VALUE} in order to make it possible to
e.g. use the left hand side's current value as key for the iterator.
At least, that's what I'm going to use my derived class for.

Thanks for having a look (and for your work on PHP), regards,
  Jakob

Reproduce code:
---------------
$leftSideArray = array('lhs_key' => 'lhs_value');
$rightSideArray = array('rhs_key' => 'rhs_value');

$combined = new DualIterator(
  new ArrayIterator($leftSideArray), new ArrayIterator($rightSideArray),
  DualIterator::KEY_LHS | DualIterator::CURRENT_RHS
);

foreach ($combined as $key => $value) {
  var_dump($key);
  var_dump($value);
}

Expected result:
----------------
string(7) "lhs_key"
string(9) "rhs_value"

Actual result:
--------------
Warning: Illegal type returned from DualIterator::key() in
/home/jakob/dev/www/drupal/6/transformations/sites/all/modules/tf_modu
line 223

Call Stack:
    0.0014     112544   1. {main}()
/home/jakob/dev/www/drupal/6/transformations/sites/all/modules/tf_modu

int(0)
string(9) "rhs_value"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-07 19:04 UTC] jpetso at gmx dot at
The diff to fix the bug.

--- dualiterator.1.4.inc	2009-01-07 19:47:18.000000000 +0100
+++ dualiterator.fixed.inc	2009-01-07 20:01:31.000000000 +0100
@@ -23,10 +23,9 @@ class DualIterator implements Iterator
 
 	const KEY_LHS   = 0x10;
 	const KEY_RHS   = 0x20;
-	const KEY_ARRAY = 0x30;
 	const KEY_0     = 0x00;
 	
-	const DEFAULT_FLAGS = 0x33;
+	const DEFAULT_FLAGS = 0x13;
 	
 	private $lhs;
 	private $rhs;
@@ -114,13 +113,11 @@ class DualIterator implements Iterator
 		switch($this->flags & 0xF0)
 		{
 		default:
-		case self::CURRENT_ARRAY:
-			return array($this->lhs->key(), $this->rhs-
-		case self::CURRENT_LHS:
+		case self::KEY_LHS:
 			return $this->lhs->key();
-		case self::CURRENT_RHS:
+		case self::KEY_RHS:
 			return $this->rhs->key();
-		case self::CURRENT_0:
+		case self::KEY_0:
 			return NULL;
 		}
 	}
 [2009-01-07 19:15 UTC] jpetso at gmx dot at
Oops, forgot to change the default flags for the constructor, and to
fix up the method docs as well. Here's the improved patch.

--- dualiterator.1.4.inc	2009-01-07 19:47:18.000000000 +0100
+++ dualiterator.fixed.inc	2009-01-07 20:12:59.000000000 +0100
@@ -23,10 +23,9 @@ class DualIterator implements Iterator
 
 	const KEY_LHS   = 0x10;
 	const KEY_RHS   = 0x20;
-	const KEY_ARRAY = 0x30;
 	const KEY_0     = 0x00;
 	
-	const DEFAULT_FLAGS = 0x33;
+	const DEFAULT_FLAGS = 0x13;
 	
 	private $lhs;
 	private $rhs;
@@ -38,8 +37,8 @@ class DualIterator implements Iterator
 	 * @param rhs   Right Hand Side Iterator
 	 * @param flags iteration flags
 	 */
-	function __construct(Iterator $lhs, Iterator $rhs, 
-					$flags = 0x33
/*DualIterator::DEFAULT_FLAGS*/)
+	function __construct(Iterator $lhs, Iterator $rhs,
+					$flags = 0x13
/*DualIterator::DEFAULT_FLAGS*/)
 	{
 		$this->lhs   = $lhs;
 		$this->rhs   = $rhs;
@@ -107,20 +106,18 @@ class DualIterator implements Iterator
 		}
 	}
 
-	/** @return current value depending on KEY_* flags
+	/** @return key value depending on KEY_* flags
 	 */	
 	function key()
 	{
 		switch($this->flags & 0xF0)
 		{
 		default:
-		case self::CURRENT_ARRAY:
-			return array($this->lhs->key(), $this->rhs-
-		case self::CURRENT_LHS:
+		case self::KEY_LHS:
 			return $this->lhs->key();
-		case self::CURRENT_RHS:
+		case self::KEY_RHS:
 			return $this->rhs->key();
-		case self::CURRENT_0:
+		case self::KEY_0:
 			return NULL;
 		}
 	}
 [2009-02-03 12:55 UTC] colder@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Thanks for the patch!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 07:01:27 2024 UTC