Bug #79203 [Com]: Inconsistent behaviour of ArrayIterator
| From: | mail at dabla dot ch | Date: | Fri, 31 Jan 2020 14:29:50 +0000 |
| Subject: | Bug #79203 [Com]: Inconsistent behaviour of ArrayIterator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225276@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79203&edit=1
ID: 79203
Comment by: mail at dabla dot ch
Reported by: mail at dabla dot ch
Summary: Inconsistent behaviour of ArrayIterator
Status: Open
Type: Bug
Package: SPL related
Operating System: Linux
PHP Version: 7.4.2
Block user comment: N
Private report: N
New Comment:
The test script does not do exactly the described thing. This would be more accurate:
$a = new ArrayObject(['item']);
$i = $a->getIterator();
for($c = 0; $c < 20; $c++) {
echo ($i->valid() ? 'valid' : 'invalid') . PHP_EOL;
if(!$i->valid()) {
$i->rewind();
}
$a->offsetUnset($i->key());
$a->append('item');
}
Previous Comments:
------------------------------------------------------------------------
[2020-01-31 13:59:57] mail at dabla dot ch
Description:
------------
When calling offsetUnset for the only element in an ArrayObject and then append one item, the
resulting position of an associated ArrayIterator doesn't seem to be well defined.
Steps to reproduce:
- Create an ArrayObject with one item.
- Get an ArrayIterator for the ArrayObject.
- Repeat:
- Call offsetUnset with the key of the one element in the array.
Note: Calling offsetUnset on the array or the iterator does not change the
behaviour.
- Append an item to the ArrayObject.
- check if the iterator is valid
Until php 7.2 the iterator did always point to the appended element.
Test script:
---------------
<?php
$a = new ArrayObject(['item']);
$i = $a->getIterator();
for($c = 0; $c < 100; $c++) {
echo ($i->valid() ? 'valid' : 'invalid') . PHP_EOL;
if($i->valid()) {
$a->offsetUnset($i->key());
}
$a->append('item');
}
Expected result:
----------------
The behaviour should at least be consistent. It would be best to have the same behaviour as with
php7.2, meaning the test script would always output "valid".
Actual result:
--------------
The behaviour is inconsistent. The test script outputs:
valid
valid
valid
valid
valid
valid
valid
valid
invalid
invalid
invalid
invalid
invalid
invalid
invalid
invalid
valid
invalid
valid
invalid
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79203&edit=1