zendframework / zend-stdlib

Stdlib component from Zend Framework
BSD 3-Clause "New" or "Revised" License
384 stars 76 forks source link

Zend\Stdlib\PriorityList reports incorrect count when inserting duplicate items #8

Closed dankobiaka closed 9 years ago

dankobiaka commented 9 years ago

See https://github.com/zendframework/zf2/blob/master/library/Zend/Stdlib/PriorityList.php#L66

Zend\Stdlib\PriorityList increments $this->count every single time insert($name, $value) is called, regardless of whether $name already exists.

This results in an incorrect count being calculated when inserting items with the same $name.

For example, to see this issue:

$list = new \Zend\Stdlib\PriorityList();
$list->insert('test', 'value');
$list->insert('test', 'value');
$list->insert('test', 'value');

print sprintf("List count: %d\n", count($list));
print sprintf("Array count: %d\n", count($list->toArray()));

$list->remove('test');

print sprintf("List count after removing 1 item: %d\n", count($list));
print sprintf("Array count after removing 1 item: %d\n", count($list->toArray()));

// -- outputs: --
// List count: 3
// Array count: 1
// List count after removing 1 item: 2
// Array count after removing 1 item: 0
weierophinney commented 9 years ago

Fixed with #9.