From 8fe224cd60f445492f96183788d8415a3fce4eae Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 17 Aug 2024 21:49:42 +0000 Subject: [PATCH] Use PHP types * Add PHP type hints where possible. * Remove @var, @param and @return if redundant to type declaration * Remove redundant type conversions which are now enforced by type hints. * Enforce that decodeBoolean returns only a bool. Change-Id: I8da84ed4dc778dd9d84f8d9eec0a399f5ed26405 --- includes/ApiCategoryTree.php | 34 +++++--------------- includes/CategoryCache.php | 15 ++------- includes/CategoryTree.php | 59 +++++++++-------------------------- includes/CategoryTreePage.php | 21 +++---------- includes/Hooks.php | 26 ++++----------- includes/OptionManager.php | 50 ++++++++--------------------- 6 files changed, 50 insertions(+), 155 deletions(-) diff --git a/includes/ApiCategoryTree.php b/includes/ApiCategoryTree.php index 5297db47..b5c7bf7a 100644 --- a/includes/ApiCategoryTree.php +++ b/includes/ApiCategoryTree.php @@ -32,33 +32,15 @@ use Wikimedia\Rdbms\IConnectionProvider; */ class ApiCategoryTree extends ApiBase { - /** @var ConfigFactory */ - private $configFactory; + private ConfigFactory $configFactory; + private LanguageConverterFactory $languageConverterFactory; + private LinkRenderer $linkRenderer; + private IConnectionProvider $dbProvider; + private WANObjectCache $wanCache; - /** @var LanguageConverterFactory */ - private $languageConverterFactory; - - /** @var LinkRenderer */ - private $linkRenderer; - - /** @var IConnectionProvider */ - private $dbProvider; - - /** @var WANObjectCache */ - private $wanCache; - - /** - * @param ApiMain $main - * @param string $action - * @param ConfigFactory $configFactory - * @param IConnectionProvider $dbProvider - * @param LanguageConverterFactory $languageConverterFactory - * @param LinkRenderer $linkRenderer - * @param WANObjectCache $wanCache - */ public function __construct( ApiMain $main, - $action, + string $action, ConfigFactory $configFactory, IConnectionProvider $dbProvider, LanguageConverterFactory $languageConverterFactory, @@ -102,7 +84,7 @@ class ApiCategoryTree extends ApiBase { * @param array $params * @return string[] */ - private function extractOptions( $params ): array { + private function extractOptions( array $params ): array { if ( !isset( $params['options'] ) ) { return []; } @@ -157,7 +139,7 @@ class ApiCategoryTree extends ApiBase { * @param Config $ctConfig Config for CategoryTree * @return string HTML */ - private function getHTML( CategoryTree $ct, Title $title, $depth, Config $ctConfig ) { + private function getHTML( CategoryTree $ct, Title $title, int $depth, Config $ctConfig ): string { $langConv = $this->languageConverterFactory->getLanguageConverter(); return $this->wanCache->getWithSetCallback( diff --git a/includes/CategoryCache.php b/includes/CategoryCache.php index 42f5552c..8f225557 100644 --- a/includes/CategoryCache.php +++ b/includes/CategoryCache.php @@ -30,14 +30,10 @@ use Wikimedia\Rdbms\IResultWrapper; */ class CategoryCache { /** @var (?Category)[] Keys are category database names, values are either a Category object or null */ - private $cache = []; + private array $cache = []; - /** @var ILoadBalancer */ - private $loadBalancer; + private ILoadBalancer $loadBalancer; - /** - * @param ILoadBalancer $loadBalancer - */ public function __construct( ILoadBalancer $loadBalancer ) { @@ -47,8 +43,6 @@ class CategoryCache { /** * Get a preloaded Category object or null when the Category does not exists. Loaded the Category on demand, * if not in cache, use self::doQuery when requesting a high number of category - * @param LinkTarget $categoryTarget - * @return ?Category */ public function getCategory( LinkTarget $categoryTarget ): ?Category { if ( $categoryTarget->getNamespace() !== NS_CATEGORY ) { @@ -95,10 +89,7 @@ class CategoryCache { $this->fillFromQuery( $rows ); } - /** - * @param IResultWrapper $rows - */ - public function fillFromQuery( IResultWrapper $rows ) { + public function fillFromQuery( IResultWrapper $rows ): void { foreach ( $rows as $row ) { $this->cache[$row->cat_title] = Category::newFromRow( $row ); } diff --git a/includes/CategoryTree.php b/includes/CategoryTree.php index fe85a47e..5aeaff9f 100644 --- a/includes/CategoryTree.php +++ b/includes/CategoryTree.php @@ -44,24 +44,11 @@ use Wikimedia\Rdbms\IConnectionProvider; * to display the category structure of a wiki */ class CategoryTree { - /** @var OptionManager */ - public $optionManager; + public OptionManager $optionManager; + private Config $config; + private IConnectionProvider $dbProvider; + private LinkRenderer $linkRenderer; - /** @var Config */ - private $config; - - /** @var IConnectionProvider */ - private $dbProvider; - - /** @var LinkRenderer */ - private $linkRenderer; - - /** - * @param array $options - * @param Config $config - * @param IConnectionProvider $dbProvider - * @param LinkRenderer $linkRenderer - */ public function __construct( array $options, Config $config, @@ -76,9 +63,8 @@ class CategoryTree { /** * Add ResourceLoader modules to the OutputPage object - * @param OutputPage $outputPage */ - public static function setHeaders( OutputPage $outputPage ) { + public static function setHeaders( OutputPage $outputPage ): void { # Add the modules $outputPage->addModuleStyles( 'ext.categoryTree.styles' ); $outputPage->addModules( 'ext.categoryTree' ); @@ -95,8 +81,8 @@ class CategoryTree { * @param bool $allowMissing * @return bool|string */ - public function getTag( ?Parser $parser, $category, $hideroot = false, array $attr = [], - $depth = 1, $allowMissing = false + public function getTag( ?Parser $parser, string $category, bool $hideroot = false, array $attr = [], + int $depth = 1, bool $allowMissing = false ) { $disableCache = $this->config->get( 'CategoryTreeDisableCache' ); @@ -147,12 +133,9 @@ class CategoryTree { /** * Returns a string with an HTML representation of the children of the given category. - * @param Title $title - * @param int $depth * @suppress PhanUndeclaredClassMethod,PhanUndeclaredClassInstanceof - * @return string */ - public function renderChildren( Title $title, $depth = 1 ) { + public function renderChildren( Title $title, int $depth = 1 ): string { if ( !$title->inNamespace( NS_CATEGORY ) ) { // Non-categories can't have children. :) return ''; @@ -274,10 +257,8 @@ class CategoryTree { /** * Returns a string with an HTML representation of the parents of the given category. - * @param Title $title - * @return string */ - public function renderParents( Title $title ) { + public function renderParents( Title $title ): string { $dbr = $this->dbProvider->getReplicaDatabase(); $res = $dbr->newSelectQueryBuilder() @@ -315,7 +296,7 @@ class CategoryTree { * @param int $children * @return string */ - public function renderNode( Title $title, $children = 0 ) { + public function renderNode( Title $title, int $children = 0 ): string { if ( $this->config->get( 'CategoryTreeUseCategoryTable' ) && $title->inNamespace( NS_CATEGORY ) && !$this->optionManager->isInverse() @@ -331,12 +312,8 @@ class CategoryTree { /** * Returns a string with a HTML represenation of the given page. * $info must be an associative array, containing at least a Title object under the 'title' key. - * @param Title $title - * @param Category|null $cat - * @param int $children - * @return string */ - public function renderNodeInfo( Title $title, Category $cat = null, $children = 0 ) { + public function renderNodeInfo( Title $title, Category $cat = null, int $children = 0 ): string { $mode = $this->optionManager->getOption( 'mode' ); $isInCatNS = $title->inNamespace( NS_CATEGORY ); @@ -458,14 +435,10 @@ class CategoryTree { /** * Create a string which format the page, subcat and file counts of a category - * @param IContextSource $context - * @param ?Category $cat - * @param int $countMode - * @return string */ public static function createCountString( IContextSource $context, ?Category $cat, - $countMode - ) { + int $countMode + ): string { $allCount = $cat ? $cat->getMemberCount() : 0; $subcatCount = $cat ? $cat->getSubcatCount() : 0; $fileCount = $cat ? $cat->getFileCount() : 0; @@ -513,11 +486,9 @@ class CategoryTree { /** * Creates a Title object from a user provided (and thus unsafe) string - * @param string $title - * @return null|Title */ - public static function makeTitle( $title ) { - $title = trim( strval( $title ) ); + public static function makeTitle( string $title ): ?Title { + $title = trim( $title ); if ( $title === '' ) { return null; diff --git a/includes/CategoryTreePage.php b/includes/CategoryTreePage.php index 34f5abc1..b739a40c 100644 --- a/includes/CategoryTreePage.php +++ b/includes/CategoryTreePage.php @@ -36,22 +36,11 @@ use Wikimedia\Rdbms\IConnectionProvider; * to display the category structure of a wiki */ class CategoryTreePage extends SpecialPage { - /** @var string */ - public $target = ''; + public string $target = ''; + private IConnectionProvider $dbProvider; + private SearchEngineFactory $searchEngineFactory; + public ?CategoryTree $tree = null; - /** @var IConnectionProvider */ - private $dbProvider; - - /** @var SearchEngineFactory */ - private $searchEngineFactory; - - /** @var CategoryTree */ - public $tree = null; - - /** - * @param IConnectionProvider $dbProvider - * @param SearchEngineFactory $searchEngineFactory - */ public function __construct( IConnectionProvider $dbProvider, SearchEngineFactory $searchEngineFactory @@ -65,7 +54,7 @@ class CategoryTreePage extends SpecialPage { * @param string $name * @return mixed */ - private function getOption( $name ) { + private function getOption( string $name ) { if ( $this->tree ) { return $this->tree->optionManager->getOption( $name ); } else { diff --git a/includes/Hooks.php b/includes/Hooks.php index 2f26b745..4a6a8adf 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -62,25 +62,11 @@ class Hooks implements CategoryViewer__doCategoryQueryHook, CategoryViewer__generateLinkHook { - - /** @var CategoryCache */ - private $categoryCache; - - /** @var Config */ - private $config; - - /** @var IConnectionProvider */ + private CategoryCache $categoryCache; + private Config $config; private IConnectionProvider $dbProvider; + private LinkRenderer $linkRenderer; - /** @var LinkRenderer */ - private $linkRenderer; - - /** - * @param CategoryCache $categoryCache - * @param Config $config - * @param IConnectionProvider $dbProvider - * @param LinkRenderer $linkRenderer - */ public function __construct( CategoryCache $categoryCache, Config $config, @@ -215,10 +201,10 @@ class Hooks implements $attr = Sanitizer::validateTagAttributes( $argv, 'div' ); $hideroot = isset( $argv['hideroot'] ) - ? OptionManager::decodeBoolean( $argv['hideroot'] ) : null; + ? OptionManager::decodeBoolean( $argv['hideroot'] ) : false; $onlyroot = isset( $argv['onlyroot'] ) - ? OptionManager::decodeBoolean( $argv['onlyroot'] ) : null; - $depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : null; + ? OptionManager::decodeBoolean( $argv['onlyroot'] ) : false; + $depthArg = isset( $argv['depth'] ) ? (int)$argv['depth'] : 1; $depth = $ct->optionManager->capDepth( $depthArg ); if ( $onlyroot ) { diff --git a/includes/OptionManager.php b/includes/OptionManager.php index 2ac8285f..1bea88db 100644 --- a/includes/OptionManager.php +++ b/includes/OptionManager.php @@ -33,16 +33,9 @@ use MediaWiki\MediaWikiServices; * Core functions to handle the options */ class OptionManager { - /** @var array */ - private $mOptions = []; + private array $mOptions = []; + private Config $config; - /** @var Config */ - private $config; - - /** - * @param array $options - * @param Config $config - */ public function __construct( array $options, Config $config ) { $this->config = $config; @@ -76,10 +69,7 @@ class OptionManager { } } - /** - * @return array - */ - public function getOptions() { + public function getOptions(): array { return $this->mOptions; } @@ -87,14 +77,11 @@ class OptionManager { * @param string $name * @return mixed */ - public function getOption( $name ) { + public function getOption( string $name ) { return $this->mOptions[$name]; } - /** - * @return bool - */ - public function isInverse() { + public function isInverse(): bool { return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS; } @@ -182,11 +169,11 @@ class OptionManager { * Helper function to convert a string to a boolean value. * Perhaps make this a global function in MediaWiki proper * @param mixed $value - * @return bool|null|string + * @return bool */ - public static function decodeBoolean( $value ) { + public static function decodeBoolean( $value ): bool { if ( $value === null ) { - return null; + return false; } if ( is_bool( $value ) ) { return $value; @@ -204,12 +191,6 @@ class OptionManager { || $value === 'true' || $value === 't' || $value === 'on' ) { return true; - } elseif ( $value === 'no' || $value === 'n' - || $value === 'false' || $value === 'f' || $value === 'off' - ) { - return false; - } elseif ( $value === 'null' || $value === 'default' || $value === 'none' || $value === 'x' ) { - return null; } else { return false; } @@ -263,7 +244,7 @@ class OptionManager { * @param string $enc * @return mixed */ - private static function encodeOptions( array $options, $enc ) { + private static function encodeOptions( array $options, string $enc ) { if ( $enc === 'mode' || $enc === '' ) { $opt = $options['mode']; } elseif ( $enc === 'json' ) { @@ -279,7 +260,7 @@ class OptionManager { * @param int|null $depth * @return string */ - public function getOptionsAsCacheKey( $depth = null ) { + public function getOptionsAsCacheKey( int $depth = null ): string { $key = ''; foreach ( $this->mOptions as $k => $v ) { @@ -299,7 +280,7 @@ class OptionManager { * @param int|null $depth * @return mixed */ - public function getOptionsAsJsStructure( $depth = null ) { + public function getOptionsAsJsStructure( int $depth = null ) { $opt = $this->mOptions; if ( $depth !== null ) { $opt['depth'] = $depth; @@ -311,15 +292,10 @@ class OptionManager { /** * Internal function to cap depth * @param int $depth - * @return int|mixed + * @return int */ - public function capDepth( $depth ) { - if ( !is_numeric( $depth ) ) { - return 1; - } - + public function capDepth( int $depth ): int { $mode = $this->getOption( 'mode' ); - $depth = intval( $depth ); $maxDepth = $this->config->get( 'CategoryTreeMaxDepth' ); if ( is_array( $maxDepth ) ) {