Bug #52560 [NEW]: FilterIterator errantly returns null for first element

From: Date: Sat, 07 Aug 2010 02:40:31 +0000
Subject: Bug #52560 [NEW]: FilterIterator errantly returns null for first element
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-153205@lists.php.net to get a copy of this message
From:             
Operating system: Ubuntu 10.04
PHP version:      5.3SVN-2010-08-07 (snap)
Package:          SPL related
Bug Type:         Bug
Bug description:FilterIterator errantly returns null for first element

Description:
------------
Even when a FilterIterator subclass accepts all elements, it returns NULL
for the 
first element in the original array. There are a few methods to correct
this 
behavior, noted in the attached test script.

Test script:
---------------
<?php
class FooIterator extends FilterIterator {
    /*
    // Forces this class to produce expected behavior below
    // (i.e. 1, 2, NULL instead of NULL, 2, NULL)
    public function __construct(Iterator $iterator) {
        parent::__construct($iterator);
        $iterator->rewind();
    }
    */
    // Should cause all elements to be accepted
    public function accept() {
        return true;
    }   
    /* 
    // Another way to force this class to produce expected behavior below
    public function current() {
        return $this->getInnerIterator()->current();
    }
    */
}

$array = array(1, 2); 

/*
// What should happen in subsequent blocks
$iterator1 = new ArrayIterator($array);
var_dump($iterator1->current()); // int(1)
$iterator1->next();
var_dump($iterator1->current()); // int(2)
$iterator1->next();
var_dump($iterator1->current()); // NULL
*/

// The problem
$iterator2 = new FooIterator(new ArrayIterator($array));
var_dump($iterator2->current()); // NULL <-- this should be int(1)
$iterator2->next();
var_dump($iterator2->current()); // int(2)
$iterator2->next();
var_dump($iterator2->current()); // NULL

// Exhibits correct behavior with no changes to FooIterator
$iterator3 = new FooIterator(new ArrayIterator($array));
$iterator3->rewind(); // This line shouldn't be needed, but is to force
expected behavior
var_dump($iterator3->current()); // int(1)
$iterator3->next();
var_dump($iterator3->current()); // int(2)
$iterator3->next();
var_dump($iterator3->current()); // NULL

Expected result:
----------------
int(1)
int(2)
NULL
int(1)
int(2)
NULL

Actual result:
--------------
NULL
int(2)
NULL
int(1)
int(2)
NULL

-- 
Edit bug report at http://bugs.php.net/bug.php?id=52560&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=52560&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=52560&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=52560&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=52560&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52560&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=52560&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=52560&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=52560&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=52560&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=52560&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=52560&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=52560&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=52560&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=52560&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52560&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52560&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=52560&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=52560&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=52560&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=52560&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=52560&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#153205) next »