Skip to content

Undefined array key 0 after array_filter #96

@mihaistana

Description

@mihaistana

After the array_filter on line 1481 on function hasSingleTagInsideElement the array sometimes not start with 0,

ErrorException

Undefined array key 0

at vendor/j0k3r/php-readability/src/Readability.php:1485
1481▕ $children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
1482▕ //$children = array_values($children);
1483▕ // There should be exactly 1 element child with given tag
1484▕
➜ 1485▕ if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
1486▕ return false;
1487▕ }
1488▕
1489▕ $a = array_filter(

to fix it you have to add array_values to reset the array index.

private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
{
$childNodes = iterator_to_array($node->childNodes);
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
$children = array_values($children);
// There should be exactly 1 element child with given tag
if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
return false;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions