From c6c9254b7db7acb5b44111b2452efada1d1a96e3 Mon Sep 17 00:00:00 2001
From: Philipp Scheit
Date: Tue, 16 Jun 2026 14:43:38 +0200
Subject: [PATCH] Fix PHPStan property.notFound on DOMNameSpaceNode in HasXPath
PHPStan 2.2.x updated its bundled DOM stubs so that iterating a
\DOMNodeList now yields DOMNameSpaceNode|DOMNode. DOMNameSpaceNode has
no $textContent property, which triggered property.notFound errors at
HasXPath::matchesContent() (lines 128 and 134).
Guard the textContent access with an instanceof \DOMNode check so only
real DOM nodes are matched/collected.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
hamcrest/Hamcrest/Xml/HasXPath.php | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/hamcrest/Hamcrest/Xml/HasXPath.php b/hamcrest/Hamcrest/Xml/HasXPath.php
index 2529f7270..097c8c5ff 100644
--- a/hamcrest/Hamcrest/Xml/HasXPath.php
+++ b/hamcrest/Hamcrest/Xml/HasXPath.php
@@ -125,13 +125,15 @@ protected function matchesContent(\DOMNodeList $nodes, Description $mismatchDesc
return true;
} else {
foreach ($nodes as $node) {
- if ($this->_matcher->matches($node->textContent)) {
+ if ($node instanceof \DOMNode && $this->_matcher->matches($node->textContent)) {
return true;
}
}
$content = array();
foreach ($nodes as $node) {
- $content[] = $node->textContent;
+ if ($node instanceof \DOMNode) {
+ $content[] = $node->textContent;
+ }
}
$mismatchDescription->appendText('XPath returned ')
->appendValue($content);