From 9b9ef62a4c00d9fbc2503668f7289507f2feb907 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Fri, 6 Oct 2023 20:08:08 +0200 Subject: [PATCH] Remove null fallback from DOMElement::getAttribute Also set @phan-var to handle nullable parentNode DOMNode::$parentNode is documented as nullable in php8.1 DOMElement::getAttribute is documented to return string in php8.1 Reported by phan running under php8.1 Change-Id: Ia4abd7bf03bc52d7d4ba2c69738bb90c55d01a2b --- includes/ImageMap.php | 4 +++- includes/ParsoidImageMap.php | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/includes/ImageMap.php b/includes/ImageMap.php index a1881ea..280ae59 100644 --- a/includes/ImageMap.php +++ b/includes/ImageMap.php @@ -317,6 +317,7 @@ class ImageMap implements ParserFirstCallInitHook { // Add a surrounding div, remove the default link to the description page $anchor = $imageNode->parentNode; $parent = $anchor->parentNode; + '@phan-var DOMElement $anchor'; // Handle cases where there are no anchors, like `|link=` if ( $anchor instanceof DOMDocumentFragment ) { @@ -349,6 +350,7 @@ class ImageMap implements ParserFirstCallInitHook { $anchor = $imageNode->parentNode; $wrapper = $anchor->parentNode; Assert::precondition( $wrapper instanceof DOMElement, 'Anchor node has a parent' ); + '@phan-var DOMElement $anchor'; $classes = $wrapper->getAttribute( 'class' ); @@ -438,7 +440,7 @@ class ImageMap implements ParserFirstCallInitHook { } } else { '@phan-var DOMElement $wrapper'; - $typeOf = $wrapper->getAttribute( 'typeof' ) ?? ''; + $typeOf = $wrapper->getAttribute( 'typeof' ); if ( preg_match( '#\bmw:File/Thumb\b#', $typeOf ) ) { // $imageNode was cloned above $img = $imageParent->firstChild; diff --git a/includes/ParsoidImageMap.php b/includes/ParsoidImageMap.php index 7857c90..be7dc48 100644 --- a/includes/ParsoidImageMap.php +++ b/includes/ParsoidImageMap.php @@ -138,10 +138,10 @@ class ParsoidImageMap extends ExtensionTagHandler implements ExtensionModule { // factor when one is much larger than the other // (sx+sy)/(x+y) = s - $thumbWidth = (int)( $imageNode->getAttribute( 'width' ) ?? '' ); - $thumbHeight = (int)( $imageNode->getAttribute( 'height' ) ?? '' ); - $imageWidth = (int)( $imageNode->getAttribute( 'data-file-width' ) ?? '' ); - $imageHeight = (int)( $imageNode->getAttribute( 'data-file-height' ) ?? '' ); + $thumbWidth = (int)( $imageNode->getAttribute( 'width' ) ); + $thumbHeight = (int)( $imageNode->getAttribute( 'height' ) ); + $imageWidth = (int)( $imageNode->getAttribute( 'data-file-width' ) ); + $imageHeight = (int)( $imageNode->getAttribute( 'data-file-height' ) ); $denominator = $imageWidth + $imageHeight; $numerator = $thumbWidth + $thumbHeight; @@ -207,7 +207,7 @@ class ParsoidImageMap extends ExtensionTagHandler implements ExtensionModule { } DOMUtils::assertElt( $a ); - $href = $a->getAttribute( 'href' ) ?? ''; + $href = $a->getAttribute( 'href' ); $externLink = DOMUtils::matchRel( $a, '#^mw:ExtLink/#D' ) !== null; $alt = ''; @@ -323,7 +323,7 @@ class ParsoidImageMap extends ExtensionTagHandler implements ExtensionModule { DOMCompat::getClassList( $thumb )->add( 'noresize' ); // Determine whether a "magnify" link is present - $typeOf = $thumb->getAttribute( 'typeof' ) ?? ''; + $typeOf = $thumb->getAttribute( 'typeof' ); if ( !preg_match( '#\bmw:File/Thumb\b#', $typeOf ) && $descType !== self::NONE ) { // The following classes are used here: // * mw-ext-imagemap-desc-top-right