diff options
author | Bartosz Dziewoński <matma.rex@gmail.com> | 2023-04-22 15:57:00 +0200 |
---|---|---|
committer | Bartosz Dziewoński <matma.rex@gmail.com> | 2023-04-22 16:45:09 +0200 |
commit | 6ba47296d9bcf8397974e47bbc635377b9b3b304 (patch) | |
tree | 4e2fd750bf5dcaf9c4b35d0be073e77a31609208 /includes/Category/CategoryViewer.php | |
parent | 774750b00dab24765ebf4bce26501eba288b1977 (diff) | |
download | mediawikicore-6ba47296d9bcf8397974e47bbc635377b9b3b304.tar.gz mediawikicore-6ba47296d9bcf8397974e47bbc635377b9b3b304.zip |
Fix Phan suppressions related to Title::castFrom*() and friends
There is no way to express that Title::castFromPageIdentity(),
Title::castFromPageReference() and Title::castFromLinkTarget()
can only return null when the parameter is null. We need to add
Phan suppressions or explicit types almost everywhere that these
methods are used with parameters that are known to not be null.
Instead, introduce new methods Title::newFromPageIdentity() and
Title::newFromPageReference() (Title::newFromLinkTarget() already
exists), without the null-coalescing behavior, and use them when
the parameter is not null. This lets static analysis tools, and
humans, easily understand where nulls can't appear.
Do the same with the corresponding TitleFactory methods.
Change the obvious uses of castFrom*() to newFrom*() (if there is
a Phan suppression, a type check, or a method call on the result).
Change-Id: Ida4da75953cf3bca372a40dc88022443109ca0cb
Diffstat (limited to 'includes/Category/CategoryViewer.php')
-rw-r--r-- | includes/Category/CategoryViewer.php | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/includes/Category/CategoryViewer.php b/includes/Category/CategoryViewer.php index 462442739848..d72b4d4cd421 100644 --- a/includes/Category/CategoryViewer.php +++ b/includes/Category/CategoryViewer.php @@ -122,8 +122,7 @@ class CategoryViewer extends ContextSource { 'title', '1.37', function (): Title { - // @phan-suppress-next-line PhanTypeMismatchReturnNullable castFrom does not return null here - return Title::castFromPageIdentity( $this->page ); + return Title::newFromPageIdentity( $this->page ); }, function ( PageIdentity $page ) { $this->page = $page; @@ -260,8 +259,8 @@ class CategoryViewer extends ContextSource { ): string { $link = null; $legacyTitle = MediaWikiServices::getInstance()->getTitleFactory() - ->castFromPageReference( $page ); - // @phan-suppress-next-line PhanTypeMismatchArgument castFrom does not return null here + ->newFromPageReference( $page ); + // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args $this->getHookRunner()->onCategoryViewer__generateLink( $type, $legacyTitle, $html, $link ); if ( $link === null ) { $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); @@ -317,14 +316,12 @@ class CategoryViewer extends ContextSource { PageReference $page, string $sortkey, int $pageLength, bool $isRedirect = false ): void { $title = MediaWikiServices::getInstance()->getTitleFactory() - ->castFromPageReference( $page ); + ->newFromPageReference( $page ); if ( $this->showGallery ) { $flip = $this->flip['file']; if ( $flip ) { - // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom does not return null here $this->gallery->insert( $title, '', '', '', [], ImageGalleryBase::LOADING_LAZY ); } else { - // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom does not return null here $this->gallery->add( $title, '', '', '', [], ImageGalleryBase::LOADING_LAZY ); } } else { @@ -638,7 +635,7 @@ class CategoryViewer extends ContextSource { } $pageLang = MediaWikiServices::getInstance()->getTitleFactory() - ->castFromPageIdentity( $this->page ) + ->newFromPageIdentity( $this->page ) ->getPageLanguage(); $attribs = [ 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(), 'class' => 'mw-content-' . $pageLang->getDir() ]; |