Skip to content

Commit 61fcb73

Browse files
author
akond
committed
Refactoring out isChild method.
1 parent 1e0e1c9 commit 61fcb73

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,42 @@ public function previousChild($id)
358358
}
359359

360360
/**
361-
* Checks if the given node id is a decendant of the
361+
* Checks if the given node id is a child of the
362362
* current node.
363363
*
364364
* @param int $id
365365
* @return bool
366366
*/
367-
public function isDescendant($id)
367+
public function isChild ($id)
368368
{
369369
foreach ($this->children as $childId => $child)
370370
{
371371
if ($id == $childId)
372372
{
373373
return true;
374374
}
375-
elseif ($child['node']->hasChildren())
375+
}
376+
377+
return false;
378+
}
379+
380+
/**
381+
* Checks if the given node id is a decendant of the
382+
* current node.
383+
*
384+
* @param int $id
385+
* @return bool
386+
*/
387+
public function isDescendant($id)
388+
{
389+
if ($this->isChild ($id))
390+
{
391+
return true;
392+
}
393+
394+
foreach ($this->children as $childId => $child)
395+
{
396+
if ($child['node']->hasChildren())
376397
{
377398
if ($child['node']->isDescendant($id))
378399
{

tests/Node/ChildrenTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,18 @@ public function testCountChildren()
6161
$child2->setParent($parent);
6262
$this->assertEquals(2, $parent->countChildren());
6363
}
64+
65+
public function testIsChild ()
66+
{
67+
$parent = new Node;
68+
$child1 = new Node;
69+
$child2 = new Node;
70+
71+
$child1->setParent($parent);
72+
$child2->setParent($child1);
73+
74+
$this->assertTrue ($parent->isChild ($child1->id ()));
75+
$this->assertTrue ($parent->isDescendant ($child2->id ()));
76+
$this->assertFalse ($parent->isChild ($child2->id ()));
77+
}
6478
}

0 commit comments

Comments
 (0)