Skip to content

Commit

Permalink
DomPlugin/DOMDocumentPlugin: Use array access for lists
Browse files Browse the repository at this point in the history
NodeList and NamedNodeMap have undocumented array access without
implemeting ArrayAccess since 5.6.3 - let's use it.
  • Loading branch information
jnvsor committed Aug 26, 2024
1 parent 48fa3c0 commit 804095a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 61 deletions.
Binary file modified build/kint.phar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Parser/DOMDocumentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ private function parseList($var, InstanceValue &$o, int $trigger): void
//
// Contrary to the PHP docs, getNamedItemNS takes null
// as a namespace argument for an unnamespaced item.
$base_obj->access_path = $o->access_path.'->getNamedItem('.\var_export($item->nodeName, true).')';
$base_obj->access_path = $o->access_path.'['.\var_export($item->nodeName, true).']';
} else { // Dom\NodeList
$base_obj->access_path = $o->access_path.'->item('.\var_export($key, true).')';
$base_obj->access_path = $o->access_path.'['.\var_export($key, true).']';
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Parser/DomPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ private function parseList($var, InstanceValue &$o, int $trigger): void
//
// Contrary to the PHP docs, getNamedItemNS takes null
// as a namespace argument for an unnamespaced item.
$base_obj->access_path = $o->access_path.'->getNamedItem('.\var_export($item->nodeName, true).')';
$base_obj->access_path = $o->access_path.'['.\var_export($item->nodeName, true).']';
} else { // Dom\NodeList
$base_obj->access_path = $o->access_path.'->item('.\var_export($key, true).')';
$base_obj->access_path = $o->access_path.'['.\var_export($key, true).']';
}
}

Expand Down
50 changes: 25 additions & 25 deletions tests/Parser/DOMDocumentPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function testNamespaces()
$o = $p->parse($v, clone $b);

$root = $o->getRepresentation('children')->contents[0];
$this->assertSame('$v->childNodes->item(0)', $root->access_path);
$this->assertSame('$v->childNodes[0]', $root->access_path);

$root_props = [];
foreach ($root->value->contents as $val) {
Expand All @@ -238,8 +238,8 @@ public function testNamespaces()
$this->assertSame('x', $root_props['tagName']->value->contents);
$this->assertSame('x', $root_props['localName']->value->contents);
$this->assertSame('http://localhost/', $root_props['namespaceURI']->value->contents);
$this->assertSame('$v->childNodes->item(0)->namespaceURI', $root_props['namespaceURI']->access_path);
$this->assertSame('$v->childNodes->item(0)->attributes', $root_props['attributes']->access_path);
$this->assertSame('$v->childNodes[0]->namespaceURI', $root_props['namespaceURI']->access_path);
$this->assertSame('$v->childNodes[0]->attributes', $root_props['attributes']->access_path);
$this->assertSame(1, $root_props['attributes']->size);

$this->assertEquals(
Expand All @@ -255,11 +255,11 @@ public function testNamespaces()
$this->assertCount(1, $attribs);

$this->assertSame('0 0 30 150', $attribs['viewBox']->value->contents);
$this->assertSame('$v->childNodes->item(0)->attributes->getNamedItem(\'viewBox\')->nodeValue', $attribs['viewBox']->access_path);
$this->assertSame('$v->childNodes[0]->attributes[\'viewBox\']->nodeValue', $attribs['viewBox']->access_path);

$g = $root->getRepresentation('children')->contents[0];
$this->assertSame('test:g', $g->name);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(1)', $g->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[1]', $g->access_path);

$gprops = [];
foreach ($g->value->contents as $val) {
Expand All @@ -269,13 +269,13 @@ public function testNamespaces()
$this->assertSame('test:g', $gprops['tagName']->value->contents);
$this->assertSame('g', $gprops['localName']->value->contents);
$this->assertSame('http://localhost/test', $gprops['namespaceURI']->value->contents);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(1)->namespaceURI', $gprops['namespaceURI']->access_path);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(1)->attributes', $gprops['attributes']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[1]->namespaceURI', $gprops['namespaceURI']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[1]->attributes', $gprops['attributes']->access_path);
$this->assertSame(2, $gprops['attributes']->size);

$g = $root->getRepresentation('children')->contents[1];
$this->assertSame('g', $g->name);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(3)', $g->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[3]', $g->access_path);

$gprops = [];
foreach ($g->value->contents as $val) {
Expand All @@ -285,13 +285,13 @@ public function testNamespaces()
$this->assertSame('g', $gprops['tagName']->value->contents);
$this->assertSame('g', $gprops['localName']->value->contents);
$this->assertSame('http://localhost/test', $gprops['namespaceURI']->value->contents);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(3)->namespaceURI', $gprops['namespaceURI']->access_path);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(3)->attributes', $gprops['attributes']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[3]->namespaceURI', $gprops['namespaceURI']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[3]->attributes', $gprops['attributes']->access_path);
$this->assertNull($gprops['attributes']->size);

$both = $root->getRepresentation('children')->contents[3];
$this->assertSame('both', $both->name);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(5)', $both->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[5]', $both->access_path);

$attribs = [];
foreach ($both->getRepresentation('attributes')->contents as $val) {
Expand All @@ -301,10 +301,10 @@ public function testNamespaces()
$this->assertCount(2, $attribs);

$this->assertSame('base', $attribs['attribs']->value->contents);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(5)->attributes->getNamedItem(\'attribs\')->nodeValue', $attribs['attribs']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[5]->attributes[\'attribs\']->nodeValue', $attribs['attribs']->access_path);

$this->assertSame('exists', $attribs['test:attribs']->value->contents);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(5)->attributes->getNamedItem(\'test:attribs\')->nodeValue', $attribs['test:attribs']->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[5]->attributes[\'test:attribs\']->nodeValue', $attribs['test:attribs']->access_path);
}

/**
Expand Down Expand Up @@ -456,7 +456,7 @@ public function testHTML()
$body = $found_props['childNodes']->getRepresentation('iterator')->contents[1] ?? null;
$this->assertNotNull($body);
$this->assertSame('body', $body->name);
$this->assertSame('$v->childNodes->item(1)', $body->access_path);
$this->assertSame('$v->childNodes[1]', $body->access_path);

$found_props = [];
foreach ($body->value->contents as $val) {
Expand All @@ -474,7 +474,7 @@ public function testHTML()
// strong
$strong = $body->getRepresentation('children')->contents[0];
$this->assertNotNull($strong);
$this->assertSame('$v->childNodes->item(1)->childNodes->item(1)', $strong->access_path);
$this->assertSame('$v->childNodes[1]->childNodes[1]', $strong->access_path);

$found_props = [];
foreach ($strong->value->contents as $val) {
Expand All @@ -495,7 +495,7 @@ public function testHTML()
$attrib = $attributes->contents[0];
$this->assertInstanceOf(BlobValue::class, $attrib);
$this->assertSame('class', $attrib->name);
$this->assertSame('$v->childNodes->item(1)->childNodes->item(1)->attributes->getNamedItem(\'class\')->nodeValue', $attrib->access_path);
$this->assertSame('$v->childNodes[1]->childNodes[1]->attributes[\'class\']->nodeValue', $attrib->access_path);
$this->assertSame('text', $attrib->value->contents);

if (KINT_PHP83) {
Expand All @@ -508,7 +508,7 @@ public function testHTML()
// div
$div = $body->getRepresentation('children')->contents[1];
$this->assertNotNull($div);
$this->assertSame('$v->childNodes->item(1)->childNodes->item(3)', $div->access_path);
$this->assertSame('$v->childNodes[1]->childNodes[3]', $div->access_path);

$found_props = [];
foreach ($div->value->contents as $val) {
Expand All @@ -529,7 +529,7 @@ public function testHTML()
$attrib = $attributes->contents[0];
$this->assertInstanceOf(BlobValue::class, $attrib);
$this->assertSame('namespaces', $attrib->name);
$this->assertSame('$v->childNodes->item(1)->childNodes->item(3)->attributes->getNamedItem(\'namespaces\')->nodeValue', $attrib->access_path);
$this->assertSame('$v->childNodes[1]->childNodes[3]->attributes[\'namespaces\']->nodeValue', $attrib->access_path);
$this->assertSame('allowed', $attrib->value->contents);
}

Expand Down Expand Up @@ -597,8 +597,8 @@ protected function basicAssertionsXml(Value $o, bool $verbose)
$x = $found_props['childNodes']->getRepresentation('iterator')->contents[0] ?? null;

$this->assertNotNull($x);
$this->assertSame('$v->childNodes->item(0)', $x->access_path);
$this->assertSame('$v->childNodes->item(0)', $o->getRepresentation('children')->contents[0]->access_path);
$this->assertSame('$v->childNodes[0]', $x->access_path);
$this->assertSame('$v->childNodes[0]', $o->getRepresentation('children')->contents[0]->access_path);

$found_props = [];
foreach ($x->value->contents as $val) {
Expand Down Expand Up @@ -640,13 +640,13 @@ protected function basicAssertionsXml(Value $o, bool $verbose)
$attrib = $attributes->contents[0];
$this->assertInstanceOf(BlobValue::class, $attrib);
$this->assertSame('viewBox', $attrib->name);
$this->assertSame('$v->childNodes->item(0)->attributes->getNamedItem(\'viewBox\')->nodeValue', $attrib->access_path);
$this->assertSame('$v->childNodes[0]->attributes[\'viewBox\']->nodeValue', $attrib->access_path);
$this->assertSame('0 0 30 150', $attrib->value->contents);

// g1
$g1 = $x->getRepresentation('children')->contents[0];
$this->assertNotNull($g1);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(1)', $g1->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[1]', $g1->access_path);

$found_props = [];
foreach ($g1->value->contents as $val) {
Expand Down Expand Up @@ -684,7 +684,7 @@ protected function basicAssertionsXml(Value $o, bool $verbose)
// g2
$g2 = $x->getRepresentation('children')->contents[1];
$this->assertNotNull($g2);
$this->assertSame('$v->childNodes->item(0)->childNodes->item(3)', $g2->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[3]', $g2->access_path);

$found_props = [];
foreach ($g2->value->contents as $val) {
Expand Down Expand Up @@ -722,9 +722,9 @@ protected function basicAssertionsXml(Value $o, bool $verbose)
// not-php-compatible
$incomp = $x->getRepresentation('children')->contents[3]->getRepresentation('children')->contents[1];

$this->assertSame('$v->childNodes->item(0)->childNodes->item(5)->childNodes->item(3)', $incomp->access_path);
$this->assertSame('$v->childNodes[0]->childNodes[5]->childNodes[3]', $incomp->access_path);
$this->assertSame(
'$v->childNodes->item(0)->childNodes->item(5)->childNodes->item(3)->attributes->getNamedItem(\'also-not\')->nodeValue',
'$v->childNodes[0]->childNodes[5]->childNodes[3]->attributes[\'also-not\']->nodeValue',
$incomp->getRepresentation('attributes')->contents[0]->access_path
);
$this->assertSame('php-compatible', $incomp->getRepresentation('attributes')->contents[0]->value->contents);
Expand Down
Loading

0 comments on commit 804095a

Please sign in to comment.