php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66411 RecursiveIterator and Countable don't work together
Submitted: 2014-01-04 18:15 UTC Modified: 2014-01-05 00:20 UTC
From: webmaster at johnatan dot ru Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.23 OS: Debian 3.2.51-1
Private report: No CVE-ID: None
 [2014-01-04 18:15 UTC] webmaster at johnatan dot ru
Description:
------------
---
From manual page: http://www.php.net/countable.count
---

If I implement Countable interface in class RecursiveIteratorIterator show only nodes with count(node) > 0. If I remove Countable interface show all nodes.

Test script:
---------------
<?php
class TreeModel implements RecursiveIterator, Countable
{
	public $id;
	public $parentId;
	public $name;

	private $children = array();
	private $position = 0;
	private $counter = 0;

	public function addChildren(TreeModel $child)
	{
		$this->children[] = $child;
		$this->counter++;
	}
	
	public final function getChildren()
	{
		return $this->valid() ? $this->children[$this->position] : null;
	}

	public function hasChildren()
	{
		if (!$this->valid()) {
			return false;
		}
		
		return count($this->children[$this->position]->getChildren()) > 0;
	}

	public function current()
	{
		return $this->children[$this->position];
	}

	public function next()
	{
		$this->position++;
	}

	public function key()
	{
		return $this->position;
	}

	public function valid()
	{
		return isset($this->children[$this->position]);
	}

	public function rewind()
	{
		$this->position = 0;
	}

	public function count()
	{
		return iterator_count(new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST));
	}
}

$data = array(
	array('parentId' => 0, 'id' => 1, 'name' => 'Node 1'),
	array('parentId' => 0, 'id' => 2, 'name' => 'Node 2'),
	array('parentId' => 0, 'id' => 3, 'name' => 'Node 3'),
	array('parentId' => 1, 'id' => 4, 'name' => 'Node 1.1'),
	array('parentId' => 1, 'id' => 5, 'name' => 'Node 1.2'),
	array('parentId' => 1, 'id' => 6, 'name' => 'Node 1.3'),
	array('parentId' => 2, 'id' => 7, 'name' => 'Node 2.1'),
	array('parentId' => 3, 'id' => 10, 'name' => 'Node 3.1'),
	array('parentId' => 7, 'id' => 8, 'name' => 'Node 2.1.1'),
	array('parentId' => 8, 'id' => 9, 'name' => 'Node 2.1.1.1')
);

$root = new TreeModel();
$root->id = 0;
$root->parentId = null;
$root->name = 'ROOT';

$tempList = array($root);
foreach($data as $treeNode) {
	$treeItem = new TreeModel();
	$treeItem->id = $treeNode['id'];
	$treeItem->parentId = $treeNode['parentId'];
	$treeItem->name = $treeNode['name'];

	$tempList[$treeNode['id']] = $treeItem;
	$tempList[$treeNode['parentId']]->addChildren($treeItem);
}
unset($tempList);

$ritit = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach($ritit as $index => $treeItem) {
	echo $index, ': ', $treeItem->name, ": ", count($treeItem), "\n";
}
echo 'Total: ', count($root), "\n";

Expected result:
----------------
0: Node 1: 3
0: Node 1.1: 0
1: Node 1.2: 0
2: Node 1.3: 0
1: Node 2: 1
0: Node 2.1: 1
0: Node 2.1.1: 1
0: Node 2.1.1.1: 0
2: Node 3: 1
0: Node 3.1: 0
Total: 10

Actual result:
--------------
0: Node 1: 3
1: Node 2: 1
0: Node 2.1: 1
2: Node 3: 1
Total: 3

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-05 00:20 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-01-05 00:20 UTC] requinix@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

return count($this->children[$this->position]->getChildren()) > 0;
is wrong. It should be
  return count($this->children[$this->position]) > 0;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC