Use PHP type hints

* Add PHP type hints where possible.
* Remove @var, @param and @return if redundant to type declaration.
* Use explite type cast to int for boolean array index.

Change-Id: I3531b3f735327b5c427f1fc434adfb495b1c743f
This commit is contained in:
Fomafix 2024-10-21 10:37:10 +00:00
parent 36d44c823f
commit 43ea305f33
3 changed files with 27 additions and 100 deletions

View file

@ -22,8 +22,7 @@ use Exception;
use MediaWiki\Message\Message;
class ExprError extends Exception {
/** @var Message */
private $mwMessage;
private Message $mwMessage;
/**
* @param string $msg

View file

@ -16,23 +16,9 @@ class Hooks implements
\MediaWiki\Hook\ParserFirstCallInitHook,
\MediaWiki\Hook\ParserTestGlobalsHook
{
private Config $config;
private ParserFunctions $parserFunctions;
/** @var Config */
private $config;
/** @var ParserFunctions */
private $parserFunctions;
/**
* @param Config $config
* @param HookContainer $hookContainer
* @param LanguageConverterFactory $languageConverterFactory
* @param LanguageFactory $languageFactory
* @param LanguageNameUtils $languageNameUtils
* @param LinkCache $linkCache
* @param RepoGroup $repoGroup
* @param SpecialPageFactory $specialPageFactory
*/
public function __construct(
Config $config,
HookContainer $hookContainer,

View file

@ -28,50 +28,23 @@ use Wikimedia\RequestTimeout\TimeoutException;
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions
*/
class ParserFunctions {
/** @var ExprParser|null */
private static $mExprParser = null;
private static ?ExprParser $mExprParser = null;
/** @var array[][][][] */
private static $mTimeCache = [];
/** @var int */
private static $mTimeChars = 0;
private static array $mTimeCache = [];
private static int $mTimeChars = 0;
/** ~10 seconds */
private const MAX_TIME_CHARS = 6000;
/** @var Config */
private $config;
private Config $config;
private HookContainer $hookContainer;
private LanguageConverterFactory $languageConverterFactory;
private LanguageFactory $languageFactory;
private LanguageNameUtils $languageNameUtils;
private LinkCache $linkCache;
private RepoGroup $repoGroup;
private SpecialPageFactory $specialPageFactory;
/** @var HookContainer */
private $hookContainer;
/** @var LanguageConverterFactory */
private $languageConverterFactory;
/** @var LanguageFactory */
private $languageFactory;
/** @var LanguageNameUtils */
private $languageNameUtils;
/** @var LinkCache */
private $linkCache;
/** @var RepoGroup */
private $repoGroup;
/** @var SpecialPageFactory */
private $specialPageFactory;
/**
* @param Config $config
* @param HookContainer $hookContainer
* @param LanguageConverterFactory $languageConverterFactory
* @param LanguageFactory $languageFactory
* @param LanguageNameUtils $languageNameUtils
* @param LinkCache $linkCache
* @param RepoGroup $repoGroup
* @param SpecialPageFactory $specialPageFactory
*/
public function __construct(
Config $config,
HookContainer $hookContainer,
@ -92,10 +65,7 @@ class ParserFunctions {
$this->specialPageFactory = $specialPageFactory;
}
/**
* @return ExprParser
*/
private static function &getExprParser() {
private static function &getExprParser(): ExprParser {
if ( self::$mExprParser === null ) {
self::$mExprParser = new ExprParser;
}
@ -373,13 +343,7 @@ class ParserFunctions {
return implode( '/', $newExploded );
}
/**
* @param Parser $parser
* @param string $titletext
*
* @return bool
*/
private function ifexistInternal( Parser $parser, $titletext ): bool {
private function ifexistInternal( Parser $parser, string $titletext ): bool {
$title = Title::newFromText( $titletext );
$this->languageConverterFactory->getLanguageConverter( $parser->getContentLanguage() )
->findVariantLink( $titletext, $title, true );
@ -462,18 +426,10 @@ class ParserFunctions {
/**
* Used by time() and localTime()
*
* @param Parser $parser
* @param PPFrame $frame
* @param string $format
* @param string $date
* @param string $language
* @param string|bool $local
* @return string
*/
private function timeCommon(
Parser $parser, PPFrame $frame, $format, $date, $language, $local
) {
Parser $parser, PPFrame $frame, string $format, string $date, string $language, bool $local
): string {
$this->hookContainer->register(
'ParserClearState',
static function () {
@ -490,8 +446,8 @@ class ParserFunctions {
$cacheKey = $date;
$useTTL = false;
}
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) {
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local];
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][(int)$local] ) ) {
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][(int)$local];
if ( $useTTL && $cachedVal[1] !== null ) {
$frame->setTTL( $cachedVal[1] );
}
@ -570,7 +526,7 @@ class ParserFunctions {
$this->normalizeLangCode( $parser, $language ) );
$result = $langObject->sprintfDate( $format, $ts, $tz, $ttl );
}
self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ];
self::$mTimeCache[$format][$cacheKey][$language][(int)$local] = [ $result, $ttl ];
if ( $useTTL && $ttl !== null ) {
$frame->setTTL( $ttl );
}
@ -579,12 +535,8 @@ class ParserFunctions {
/**
* Convert an input string to a known language code for time formatting
*
* @param Parser $parser
* @param string $langCode
* @return string
*/
private function normalizeLangCode( Parser $parser, string $langCode ) {
private function normalizeLangCode( Parser $parser, string $langCode ): string {
if ( $langCode !== '' && $this->languageNameUtils->isKnownLanguageTag( $langCode ) ) {
return $langCode;
} else {
@ -659,14 +611,8 @@ class ParserFunctions {
/**
* Helper for timef and timefl
*
* @param Parser $parser
* @param PPFrame $frame
* @param array $args
* @param bool $local
* @return string
*/
private function timefCommon( Parser $parser, PPFrame $frame, array $args, $local ) {
private function timefCommon( Parser $parser, PPFrame $frame, array $args, bool $local ): string {
$date = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$inputType = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
@ -724,19 +670,15 @@ class ParserFunctions {
/**
* Verifies parameter is less than max string length.
*
* @param string $text
* @return bool
*/
private function checkLength( $text ) {
return ( mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' ) );
private function checkLength( string $text ): bool {
return mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' );
}
/**
* Generates error message. Called when string is too long.
* @return string
*/
private function tooLongError() {
private function tooLongError(): string {
$msg = wfMessage( 'pfunc_string_too_long' )
->numParams( $this->config->get( 'PFStringLengthLimit' ) );
return '<strong class="error">' . $msg->inContentLanguage()->escaped() . '</strong>';
@ -1025,7 +967,7 @@ class ParserFunctions {
* but with char refs intact
* @return string The trimmed, expanded and entity reference decoded version of the PPNode
*/
private static function decodeTrimExpand( $obj, PPFrame $frame, &$trimExpanded = '' ) {
private static function decodeTrimExpand( $obj, PPFrame $frame, string &$trimExpanded = '' ): string {
$expanded = $frame->expand( $obj );
$trimExpanded = trim( $expanded );
return trim( Sanitizer::decodeCharReferences( $expanded ) );