Avoid Phan PhanUndeclaredMethod false positives

The insertBefore() and appendChild() methods return their first
argument, but Phan only knows that they return a DOMNode, even though
we passed in a DOMElement. Rearrange slightly to avoid this pattern.

Change-Id: I04c58458e1ba0d728374ead756afb6facf970766
This commit is contained in:
Bartosz Dziewoński 2022-10-28 06:31:05 +02:00
parent 2cc6549898
commit c8c8b9c735
2 changed files with 13 additions and 11 deletions

View file

@ -2,10 +2,4 @@
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php'; $cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
// Phan gets confused because DOMNode::setAttribute doesn't
// exist, it's DOMElement::setAttribute, and some functions
// are documented to return DOMNode but they actually return
// DOMElement.
$cfg['suppress_issue_types'][] = 'PhanUndeclaredMethod';
return $cfg; return $cfg;

View file

@ -22,12 +22,14 @@ namespace MediaWiki\Extension\ImageMap;
use ConfigFactory; use ConfigFactory;
use DOMDocumentFragment; use DOMDocumentFragment;
use DOMElement;
use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use OutputPage; use OutputPage;
use Parser; use Parser;
use Sanitizer; use Sanitizer;
use Title; use Title;
use Wikimedia\Assert\Assert;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
use Wikimedia\Parsoid\Utils\DOMUtils; use Wikimedia\Parsoid\Utils\DOMUtils;
use Xml; use Xml;
@ -318,10 +320,12 @@ class ImageMap implements ParserFirstCallInitHook {
$anchor = $imageNode; $anchor = $imageNode;
} }
$div = $parent->insertBefore( $domDoc->createElement( 'div' ), $anchor ); $div = $domDoc->createElement( 'div' );
$parent->insertBefore( $div, $anchor );
$div->setAttribute( 'class', 'noresize' ); $div->setAttribute( 'class', 'noresize' );
if ( $defaultLinkAttribs ) { if ( $defaultLinkAttribs ) {
$defaultAnchor = $div->appendChild( $domDoc->createElement( 'a' ) ); $defaultAnchor = $domDoc->createElement( 'a' );
$div->appendChild( $defaultAnchor );
foreach ( $defaultLinkAttribs as $name => $value ) { foreach ( $defaultLinkAttribs as $name => $value ) {
$defaultAnchor->setAttribute( $name, $value ); $defaultAnchor->setAttribute( $name, $value );
} }
@ -340,6 +344,7 @@ class ImageMap implements ParserFirstCallInitHook {
} else { } else {
$anchor = $imageNode->parentNode; $anchor = $imageNode->parentNode;
$wrapper = $anchor->parentNode; $wrapper = $anchor->parentNode;
Assert::precondition( $wrapper instanceof DOMElement, 'Anchor node has a parent' );
$classes = $wrapper->getAttribute( 'class' ); $classes = $wrapper->getAttribute( 'class' );
@ -401,20 +406,23 @@ class ImageMap implements ParserFirstCallInitHook {
$marginTop = -20; $marginTop = -20;
} }
$div->setAttribute( 'style', "height: {$thumbHeight}px; width: {$thumbWidth}px; " ); $div->setAttribute( 'style', "height: {$thumbHeight}px; width: {$thumbWidth}px; " );
$descWrapper = $div->appendChild( $domDoc->createElement( 'div' ) ); $descWrapper = $domDoc->createElement( 'div' );
$div->appendChild( $descWrapper );
$descWrapper->setAttribute( 'style', $descWrapper->setAttribute( 'style',
"margin-left: {$marginLeft}px; " . "margin-left: {$marginLeft}px; " .
"margin-top: {$marginTop}px; " . "margin-top: {$marginTop}px; " .
"text-align: left;" "text-align: left;"
); );
$descAnchor = $descWrapper->appendChild( $domDoc->createElement( 'a' ) ); $descAnchor = $domDoc->createElement( 'a' );
$descWrapper->appendChild( $descAnchor );
$descAnchor->setAttribute( 'href', $imageTitle->getLocalURL() ); $descAnchor->setAttribute( 'href', $imageTitle->getLocalURL() );
$descAnchor->setAttribute( $descAnchor->setAttribute(
'title', 'title',
wfMessage( 'imagemap_description' )->inContentLanguage()->text() wfMessage( 'imagemap_description' )->inContentLanguage()->text()
); );
$descImg = $descAnchor->appendChild( $domDoc->createElement( 'img' ) ); $descImg = $domDoc->createElement( 'img' );
$descAnchor->appendChild( $descImg );
$descImg->setAttribute( $descImg->setAttribute(
'alt', 'alt',
wfMessage( 'imagemap_description' )->inContentLanguage()->text() wfMessage( 'imagemap_description' )->inContentLanguage()->text()