{"record":{"id":"9e3d38e0f1896e31","repo":"phar-io/manifest","slug":"domelement-expected-got-s","errorCode":null,"errorMessage":"\\DOMElement expected, got \\%s","messagePattern":"\\\\DOMElement expected, got \\\\(.+?)","errorType":"exception","errorClass":"ElementCollectionException","httpStatus":null,"severity":"error","filePath":"src/xml/ElementCollection.php","lineNumber":60,"sourceCode":"        return $this->position;\n    }\n\n    public function valid(): bool {\n        return $this->position < count($this->nodes);\n    }\n\n    public function rewind(): void {\n        $this->position = 0;\n    }\n\n    protected function getCurrentElement(): DOMElement {\n        return $this->nodes[$this->position];\n    }\n\n    private function importNodes(DOMNodeList $nodeList): void {\n        foreach ($nodeList as $node) {\n            if (!$node instanceof DOMElement) {\n                throw new ElementCollectionException(\n                    sprintf('\\DOMElement expected, got \\%s', get_class($node))\n                );\n            }\n\n            $this->nodes[] = $node;\n        }\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":69,"githubUrl":"https://github.com/phar-io/manifest/blob/c581d4941e196459bf76c945a8ca922963a66708/src/xml/ElementCollection.php#L42-L69","documentation":"ElementCollection::importNodes() walks a DOMNodeList and throws ElementCollectionException if any node is not a DOMElement, using message '\\DOMElement expected, got \\%s' with the node's actual class (e.g. DOMText, DOMComment). Collections of manifest elements must contain only element nodes.","triggerScenarios":"Building an ElementCollection (or a subclass such as AuthorElements or ExtElements) from an XPath/node list that picked up text nodes or comments between elements.","commonSituations":"XPath expressions without normalizing whitespace; XML with pretty-printing comments inside list containers; hand-building node lists in tests.","solutions":["Use an XPath query returning only elements (e.g. //author instead of //author/node())","Strip comments/whitespace text nodes from the DOMNodeList before constructing the collection","Filter with $node instanceof DOMElement when iterating manually"],"exampleFix":"// before\nnew AuthorElementsCollection($xpath->query('//author/node()'));\n// after\nnew AuthorElementsCollection($xpath->query('//author'));","handlingStrategy":"type-guard","validationCode":"// filter non-element nodes before constructing the collection\n$elements = array_filter(iterator_to_array($nodeList),\n    fn($n) => $n instanceof DOMElement);","typeGuard":"function allElements(DOMNodeList $list): bool {\n    foreach ($list as $n) { if (!$n instanceof DOMElement) return false; }\n    return true;\n}","tryCatchPattern":"try {\n    $collection = new ExtElementsCollection($nodeList);\n} catch (ElementCollectionException $e) {\n    // rebuild the list filtering to DOMElement nodes only\n}","preventionTips":["Use XPath expressions that select elements, not child nodes","Remove comments/whitespace text nodes from parsed DOM","Sanity-check node lists with instanceof filters in tests"],"tags":["php","dom","xml","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"c581d4941e196459bf76c945a8ca922963a66708","analyzedAt":"2026-09-14T11:08:47.165Z","contentChangedAt":"2026-09-14T11:08:47.165Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}