php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76765 iterator_apply doesn't work with current() on DoublyLinkedLists
Submitted: 2018-08-19 20:00 UTC Modified: 2018-08-19 21:44 UTC
From: tezcatl at fedoraproject dot org Assigned:
Status: Verified Package: SPL related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tezcatl at fedoraproject dot org
New email:
PHP Version: OS:

 

 [2018-08-19 20:00 UTC] tezcatl at fedoraproject dot org
Description:
------------
---
From manual page: http://www.php.net/function.iterator-apply
---
Iteration doesn't work as expected on SplDoublyLinkedList, as if next() was not called while using current() inside the callback passed to iterator_apply

https://3v4l.org/ARLUZ


Test script:
---------------
<?php
$ll = new SplDoublyLinkedList();

$ll->push('one');
$ll->push('two');
$ll->push('three');
$ll->push('four');

$ll->rewind();

$iterations_done = iterator_apply($ll, function(Iterator $it) {
    echo $it->key(),"\t->", $it->current(),"\t->", ucfirst($it->current()),".\n";
    return true;
}, array($ll));
echo "Did iterate {$iterations_done} times \n";

foreach($ll as $key => $value) {
    $uppercase = ucfirst($value);
    echo "{$key}\t->{$value}\t->{$uppercase}.\n";
}

Expected result:
----------------
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.
Did iterate 4 times 
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.

Actual result:
--------------
0	->one	->One.
0	->one	->One.
0	->one	->One.
0	->one	->One.
Did iterate 4 times 
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-19 21:44 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2018-08-19 21:44 UTC] cmb@php.net
Automatic iteration (foreach, iterator_apply()) on an
SplDoublyLinkedList happens on an external iterator, but
::current() and friends always work on the list directly. Removing
the ::rewind() call, which shouldn't be necessary for
iterator_apply() anyway, yields:

    0       ->      ->.
    0       ->      ->.
    0       ->      ->.
    0       ->      ->.
    Did iterate 4 times

Calling additionally ::next() in the callback brings the desired
results[1].

[1] <https://3v4l.org/ff5j2>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue May 18 15:01:25 2021 UTC