From 87459ec679e9fc990bc264df6a7b8824882217ce Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sat, 7 Dec 2019 17:20:10 +0000 Subject: [PATCH] build: Upgrade phan Depends-On: I6d538ce3ca7fd2d495c2bafbab7cc279da69db1c Change-Id: Ic8c3a01a5c37fdf461f4fd5598e597eb9c9073d3 --- composer.json | 2 +- includes/AFComputedVariable.php | 14 ++++++------ includes/AbuseFilter.php | 10 ++++++--- includes/AbuseFilterRunner.php | 23 +++++++++++++++----- includes/Views/AbuseFilterView.php | 4 ++-- includes/Views/AbuseFilterViewEdit.php | 23 +++++++++----------- includes/Views/AbuseFilterViewList.php | 1 + includes/Views/AbuseFilterViewRevert.php | 18 +++++++-------- includes/api/ApiQueryAbuseLog.php | 7 +++--- includes/pagers/AbuseFilterHistoryPager.php | 6 ++--- includes/pagers/AbuseFilterPager.php | 4 ++-- includes/parser/AFPData.php | 1 + includes/parser/AFPTreeNode.php | 2 +- includes/parser/AFPTreeParser.php | 3 ++- includes/parser/AbuseFilterCachingParser.php | 2 ++ includes/special/SpecialAbuseFilter.php | 4 ++-- includes/special/SpecialAbuseLog.php | 12 +++++----- 17 files changed, 76 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index dc13c4352..917076a57 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "mediawiki/mediawiki-codesniffer": "28.0.0", "jakub-onderka/php-console-highlighter": "0.3.2", "mediawiki/minus-x": "0.3.2", - "mediawiki/mediawiki-phan-config": "0.8.0" + "mediawiki/mediawiki-phan-config": "0.9.0" }, "scripts": { "fix": [ diff --git a/includes/AFComputedVariable.php b/includes/AFComputedVariable.php index fb4b0be85..4e1d935ad 100644 --- a/includes/AFComputedVariable.php +++ b/includes/AFComputedVariable.php @@ -416,13 +416,13 @@ class AFComputedVariable { $asOf = $parameters['asof']; $obj = self::getUserObject( $user ); - if ( $obj->getId() === 0 ) { - $result = 0; - break; - } - $registration = $obj->getRegistration(); - $result = wfTimestamp( TS_UNIX, $asOf ) - wfTimestampOrNull( TS_UNIX, $registration ); + + if ( $obj->getId() === 0 || $registration === null ) { + $result = 0; + } else { + $result = (int)wfTimestamp( TS_UNIX, $asOf ) - (int)wfTimestamp( TS_UNIX, $registration ); + } break; case 'page-age': $title = $this->buildTitle( $parameters['namespace'], $parameters['title'] ); @@ -434,7 +434,7 @@ class AFComputedVariable { } $asOf = $parameters['asof']; - $result = wfTimestamp( TS_UNIX, $asOf ) - wfTimestampOrNull( TS_UNIX, $firstRevisionTime ); + $result = (int)wfTimestamp( TS_UNIX, $asOf ) - (int)wfTimestamp( TS_UNIX, $firstRevisionTime ); break; case 'user-groups': // Deprecated but needed by old log entries diff --git a/includes/AbuseFilter.php b/includes/AbuseFilter.php index bd4cec9aa..3279ea396 100644 --- a/includes/AbuseFilter.php +++ b/includes/AbuseFilter.php @@ -510,7 +510,7 @@ class AbuseFilter { } /** - * @param int $filter + * @param int|string $filter * @internal */ public static function resetFilterProfile( $filter ) { @@ -1045,7 +1045,7 @@ class AbuseFilter { /** * @return User */ - public static function getFilterUser() { + public static function getFilterUser() : User { $username = wfMessage( 'abusefilter-blocker' )->inContentLanguage()->text(); $user = User::newSystemUser( $username, [ 'steal' => true ] ); @@ -1061,6 +1061,7 @@ class AbuseFilter { $defaultName = wfMessage( 'abusefilter-blocker' )->inLanguage( 'en' )->text(); $user = User::newSystemUser( $defaultName, [ 'steal' => true ] ); } + '@phan-var User $user'; // Promote user to 'sysop' so it doesn't look // like an unprivileged account is blocking users @@ -1419,6 +1420,7 @@ class AbuseFilter { if ( $is_new ) { $new_id = $dbw->insertId(); } + '@phan-var int $new_id'; $availableActions = $context->getConfig()->get( 'AbuseFilterActions' ); $actionsRows = []; @@ -1502,7 +1504,9 @@ class AbuseFilter { $subtype = $filter === 'new' ? 'create' : 'modify'; $logEntry = new ManualLogEntry( 'abusefilter', $subtype ); $logEntry->setPerformer( $user ); - $logEntry->setTarget( SpecialPage::getTitleFor( SpecialAbuseFilter::PAGE_NAME, $new_id ) ); + $logEntry->setTarget( + SpecialPage::getTitleFor( SpecialAbuseFilter::PAGE_NAME, (string)$new_id ) + ); $logEntry->setParameters( [ 'historyId' => $history_id, 'newId' => $new_id diff --git a/includes/AbuseFilterRunner.php b/includes/AbuseFilterRunner.php index a5e017d82..02260d7c5 100644 --- a/includes/AbuseFilterRunner.php +++ b/includes/AbuseFilterRunner.php @@ -167,6 +167,7 @@ class AbuseFilterRunner { 'profiling' => $this->profilingData ]; } + '@phan-var array{matches:array,runtime:int,condCount:int,profiling:array} $result'; $matchedFilters = array_keys( array_filter( $result['matches'] ) ); $allFilters = array_keys( $result['matches'] ); @@ -647,6 +648,8 @@ class AbuseFilterRunner { $parameters = $actions['throttle']['parameters']; $throttleId = array_shift( $parameters ); list( $rateCount, $ratePeriod ) = explode( ',', array_shift( $parameters ) ); + $rateCount = (int)$rateCount; + $ratePeriod = (int)$ratePeriod; $hitThrottle = false; @@ -744,6 +747,7 @@ class AbuseFilterRunner { foreach ( $actions as $action => $info ) { $newMsg = $this->takeConsequenceAction( $action, + // @phan-suppress-next-line PhanTypeArraySuspiciousNullable False positive $info['parameters'], AbuseFilter::getFilter( $filter )->af_public_comments, $filter @@ -786,12 +790,18 @@ class AbuseFilterRunner { /** * @param string $throttleId * @param string $types - * @param string $rateCount - * @param string $ratePeriod + * @param int $rateCount + * @param int $ratePeriod * @param bool $global * @return bool */ - protected function isThrottled( $throttleId, $types, $rateCount, $ratePeriod, $global = false ) { + protected function isThrottled( + $throttleId, + $types, + int $rateCount, + int $ratePeriod, + $global = false + ) { $stash = MediaWikiServices::getInstance()->getMainObjectStash(); $key = $this->throttleKey( $throttleId, $types, $global ); $count = intval( $stash->get( $key ) ); @@ -869,12 +879,12 @@ class AbuseFilterRunner { $identifier = substr( IP::toHex( $request->getIP() ), 0, 4 ); break; case 'creationdate': - $reg = $this->user->getRegistration(); + $reg = (int)$this->user->getRegistration(); $identifier = $reg - ( $reg % 86400 ); break; case 'editcount': // Hack for detecting different single-purpose accounts. - $identifier = $this->user->getEditCount(); + $identifier = (int)$this->user->getEditCount(); break; case 'site': $identifier = 1; @@ -1348,7 +1358,7 @@ class AbuseFilterRunner { $matchCount = $filterProfile['matches'] ?? 1; // Figure out if the filter is subject to being throttled. - $filterAge = wfTimestamp( TS_UNIX, AbuseFilter::getFilter( $filter )->af_timestamp ); + $filterAge = (int)wfTimestamp( TS_UNIX, AbuseFilter::getFilter( $filter )->af_timestamp ); $exemptTime = $filterAge + $maxAge; if ( $totalActions && $exemptTime > time() && $matchCount > $hitCountLimit && @@ -1387,6 +1397,7 @@ class AbuseFilterRunner { $username = $this->vars->getVar( 'accountname' )->toString(); $actionTitle = Title::makeTitleSafe( NS_USER, $username ); } + '@phan-var Title $actionTitle'; return AbuseFilter::getTaggingActionId( $action, $actionTitle, $username ); } diff --git a/includes/Views/AbuseFilterView.php b/includes/Views/AbuseFilterView.php index bc3bad983..e3b2236ab 100644 --- a/includes/Views/AbuseFilterView.php +++ b/includes/Views/AbuseFilterView.php @@ -8,7 +8,7 @@ abstract class AbuseFilterView extends ContextSource { */ public $mFilter; /** - * @var string The history ID of the current filter + * @var int|null The history ID of the current filter */ public $mHistoryID; /** @@ -41,7 +41,7 @@ abstract class AbuseFilterView extends ContextSource { } /** - * @param string $subpage + * @param string|int $subpage * @return Title */ public function getTitle( $subpage = '' ) { diff --git a/includes/Views/AbuseFilterViewEdit.php b/includes/Views/AbuseFilterViewEdit.php index 9b62da58c..56939a155 100644 --- a/includes/Views/AbuseFilterViewEdit.php +++ b/includes/Views/AbuseFilterViewEdit.php @@ -504,7 +504,7 @@ class AbuseFilterViewEdit extends AbuseFilterView { * @param string $action The action to build an editor for * @param bool $set Whether or not the action is activated * @param stdClass $row abuse_filter row object - * @param array|null $parameters Action parameters + * @param string[]|null $parameters Action parameters. Null iff $set is false. * @return string|\OOUI\FieldLayout */ private function buildConsequenceSelector( $action, $set, $row, ?array $parameters ) { @@ -750,11 +750,8 @@ class AbuseFilterViewEdit extends AbuseFilterView { return $output; case 'tag': - if ( $set ) { - $tags = $parameters; - } else { - $tags = []; - } + $tags = $set ? $parameters : []; + '@phan-var string[] $parameters'; $output = ''; $checkbox = @@ -973,9 +970,9 @@ class AbuseFilterViewEdit extends AbuseFilterView { // Find other messages. $dbr = wfGetDB( DB_REPLICA ); $pageTitlePrefix = "Abusefilter-$action"; - $res = $dbr->select( + $titles = $dbr->selectFieldValues( 'page', - [ 'page_title' ], + 'page_title', [ 'page_namespace' => 8, 'page_title LIKE ' . $dbr->addQuotes( $pageTitlePrefix . '%' ) @@ -984,19 +981,19 @@ class AbuseFilterViewEdit extends AbuseFilterView { ); $lang = $this->getLanguage(); - foreach ( $res as $row ) { - if ( $lang->lcfirst( $row->page_title ) === $lang->lcfirst( $warnMsg ) ) { + foreach ( $titles as $title ) { + if ( $lang->lcfirst( $title ) === $lang->lcfirst( $warnMsg ) ) { $existingSelector->setValue( $lang->lcfirst( $warnMsg ) ); } - if ( $row->page_title !== "Abusefilter-$action" ) { - $options += [ $lang->lcfirst( $row->page_title ) => $lang->lcfirst( $row->page_title ) ]; + if ( $title !== "Abusefilter-$action" ) { + $options[ $lang->lcfirst( $title ) ] = $lang->lcfirst( $title ); } } } // abusefilter-edit-warn-other, abusefilter-edit-disallow-other - $options += [ $this->msg( "abusefilter-edit-$formId-other" )->text() => 'other' ]; + $options[ $this->msg( "abusefilter-edit-$formId-other" )->text() ] = 'other'; $options = Xml::listDropDownOptionsOoui( $options ); $existingSelector->setOptions( $options ); diff --git a/includes/Views/AbuseFilterViewList.php b/includes/Views/AbuseFilterViewList.php index 4653e9f6c..1ce53750f 100644 --- a/includes/Views/AbuseFilterViewList.php +++ b/includes/Views/AbuseFilterViewList.php @@ -33,6 +33,7 @@ class AbuseFilterViewList extends AbuseFilterView { $conds = []; $deleted = $request->getVal( 'deletedfilters' ); $furtherOptions = $request->getArray( 'furtheroptions', [] ); + '@phan-var array $furtherOptions'; // Backward compatibility with old links if ( $request->getBool( 'hidedisabled' ) ) { $furtherOptions[] = 'hidedisabled'; diff --git a/includes/Views/AbuseFilterViewRevert.php b/includes/Views/AbuseFilterViewRevert.php index 911056b0d..e86b52298 100644 --- a/includes/Views/AbuseFilterViewRevert.php +++ b/includes/Views/AbuseFilterViewRevert.php @@ -13,11 +13,11 @@ class AbuseFilterViewRevert extends AbuseFilterView { */ public $origPeriodEnd; /** - * @var string The same as $origPeriodStart + * @var string|null The same as $origPeriodStart */ public $mPeriodStart; /** - * @var string The same as $origPeriodEnd + * @var string|null The same as $origPeriodEnd */ public $mPeriodEnd; /** @@ -61,8 +61,8 @@ class AbuseFilterViewRevert extends AbuseFilterView { $max = wfTimestampNow(); $filterLink = $this->linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseFilter', intval( $filter ) ), - $lang->formatNum( intval( $filter ) ) + SpecialPage::getTitleFor( 'AbuseFilter', $filter ), + $lang->formatNum( $filter ) ); $searchFields = []; $searchFields['filterid'] = [ @@ -185,10 +185,10 @@ class AbuseFilterViewRevert extends AbuseFilterView { $dbr = wfGetDB( DB_REPLICA ); - if ( $periodStart ) { + if ( $periodStart !== null ) { $conds[] = 'afl_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( $periodStart ) ); } - if ( $periodEnd ) { + if ( $periodEnd !== null ) { $conds[] = 'afl_timestamp <= ' . $dbr->addQuotes( $dbr->timestamp( $periodEnd ) ); } @@ -240,10 +240,10 @@ class AbuseFilterViewRevert extends AbuseFilterView { $request = $this->getRequest(); $this->origPeriodStart = $request->getText( 'wpPeriodStart' ); - $this->mPeriodStart = strtotime( $this->origPeriodStart ); + $this->mPeriodStart = strtotime( $this->origPeriodStart ) ?: null; $this->origPeriodEnd = $request->getText( 'wpPeriodEnd' ); - $this->mPeriodEnd = strtotime( $this->origPeriodEnd ); - $this->mSubmit = $request->getVal( 'submit' ); + $this->mPeriodEnd = strtotime( $this->origPeriodEnd ) ?: null; + $this->mSubmit = $request->getBool( 'submit' ); $this->mReason = $request->getVal( 'wpReason' ); } diff --git a/includes/api/ApiQueryAbuseLog.php b/includes/api/ApiQueryAbuseLog.php index 26a961a5a..2efa40694 100644 --- a/includes/api/ApiQueryAbuseLog.php +++ b/includes/api/ApiQueryAbuseLog.php @@ -136,10 +136,9 @@ class ApiQueryAbuseLog extends ApiQueryBase { } } - $this->addWhereIf( - [ 'afl_filter' => $params['filter'] ], - isset( $params['filter'] ) && $params['filter'] !== [] - ); + if ( isset( $params['filter'] ) && $params['filter'] !== [] ) { + $this->addWhere( [ 'afl_filter' => $params['filter'] ] ); + } $this->addWhereIf( [ 'afl_deleted' => 0 ], !SpecialAbuseLog::canSeeHidden( $user ) ); if ( isset( $params['wiki'] ) ) { // 'wiki' won't be set if $wgAbuseFilterIsCentral = false diff --git a/includes/pagers/AbuseFilterHistoryPager.php b/includes/pagers/AbuseFilterHistoryPager.php index 408e9249c..a3d0ae8b1 100644 --- a/includes/pagers/AbuseFilterHistoryPager.php +++ b/includes/pagers/AbuseFilterHistoryPager.php @@ -4,7 +4,7 @@ use MediaWiki\Linker\LinkRenderer; class AbuseFilterHistoryPager extends TablePager { /** - * @var string The filter ID + * @var int The filter ID */ public $mFilter; /** @@ -27,7 +27,7 @@ class AbuseFilterHistoryPager extends TablePager { * @param string $user User name * @param LinkRenderer $linkRenderer */ - public function __construct( $filter, AbuseFilterViewHistory $page, $user, + public function __construct( int $filter, AbuseFilterViewHistory $page, $user, LinkRenderer $linkRenderer ) { $this->mFilter = $filter; $this->mPage = $page; @@ -83,7 +83,7 @@ class AbuseFilterHistoryPager extends TablePager { switch ( $name ) { case 'afh_filter': $formatted = $this->linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseFilter', intval( $row->afh_filter ) ), + SpecialPage::getTitleFor( 'AbuseFilter', $row->afh_filter ), $lang->formatNum( $row->afh_filter ) ); break; diff --git a/includes/pagers/AbuseFilterPager.php b/includes/pagers/AbuseFilterPager.php index 2f1ead77e..c81c08b1b 100644 --- a/includes/pagers/AbuseFilterPager.php +++ b/includes/pagers/AbuseFilterPager.php @@ -186,14 +186,14 @@ class AbuseFilterPager extends TablePager { switch ( $name ) { case 'af_id': return $this->linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseFilter', intval( $value ) ), + SpecialPage::getTitleFor( 'AbuseFilter', $value ), $lang->formatNum( intval( $value ) ) ); case 'af_pattern': return $this->getHighlightedPattern( $row ); case 'af_public_comments': return $this->linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseFilter', intval( $row->af_id ) ), + SpecialPage::getTitleFor( 'AbuseFilter', $row->af_id ), $value ); case 'af_actions': diff --git a/includes/parser/AFPData.php b/includes/parser/AFPData.php index e01ac9647..6ec1915b8 100644 --- a/includes/parser/AFPData.php +++ b/includes/parser/AFPData.php @@ -181,6 +181,7 @@ class AFPData { * only returns a boolean, and thus the type of the result has already been decided and cannot * be changed to be a DUNDEFINED from here. * @internal + * @suppress PhanSuspiciousValueComparison Lots of false positives for self::DARRAY */ public function equals( AFPData $d2, $strict = false ) { if ( $this->type === self::DUNDEFINED || $d2->type === self::DUNDEFINED ) { diff --git a/includes/parser/AFPTreeNode.php b/includes/parser/AFPTreeNode.php index 076112570..38227f08b 100644 --- a/includes/parser/AFPTreeNode.php +++ b/includes/parser/AFPTreeNode.php @@ -99,7 +99,7 @@ class AFPTreeNode { /** * @param string $type - * @param AFPTreeNode[]|string[]|AFPToken $children + * @param (AFPTreeNode|null)[]|string[]|AFPToken $children * @param int $position */ public function __construct( $type, $children, $position ) { diff --git a/includes/parser/AFPTreeParser.php b/includes/parser/AFPTreeParser.php index 669deaf9a..ea868f359 100644 --- a/includes/parser/AFPTreeParser.php +++ b/includes/parser/AFPTreeParser.php @@ -206,7 +206,7 @@ class AFPTreeParser extends AFPTransitionBase { */ protected function doLevelSet() { if ( $this->mCur->type === AFPToken::TID ) { - $varname = $this->mCur->value; + $varname = (string)$this->mCur->value; // Speculatively parse the assignment statement assuming it can // potentially be an assignment, but roll back if it isn't. @@ -731,6 +731,7 @@ class AFPTreeParser extends AFPTransitionBase { } $this->move(); + // @phan-suppress-next-line PhanTypeMismatchReturnNullable Until phan can understand the switch return $result; } diff --git a/includes/parser/AbuseFilterCachingParser.php b/includes/parser/AbuseFilterCachingParser.php index 5dc9a936a..e3caf3c43 100644 --- a/includes/parser/AbuseFilterCachingParser.php +++ b/includes/parser/AbuseFilterCachingParser.php @@ -342,6 +342,8 @@ class AbuseFilterCachingParser extends AbuseFilterParser { $lastValue = $this->evalNode( $statement ); } + // @phan-suppress-next-next-line PhanTypeMismatchReturnNullable Can never be null because + // empty statements are discarded in AFPTreeParser return $lastValue; default: // @codeCoverageIgnoreStart diff --git a/includes/special/SpecialAbuseFilter.php b/includes/special/SpecialAbuseFilter.php index 594cabc8b..e15d1a027 100644 --- a/includes/special/SpecialAbuseFilter.php +++ b/includes/special/SpecialAbuseFilter.php @@ -7,7 +7,7 @@ class SpecialAbuseFilter extends AbuseFilterSpecialPage { */ public $mFilter; /** - * @var string|null The history ID of the current version + * @var int|null The history ID of the current version */ public $mHistoryID; @@ -112,7 +112,7 @@ class SpecialAbuseFilter extends AbuseFilterSpecialPage { $this->mFilter = $params[1]; } elseif ( count( $params ) === 4 && $params[2] === 'item' ) { $this->mFilter = $params[1]; - $this->mHistoryID = $params[3]; + $this->mHistoryID = (int)$params[3]; $view = 'AbuseFilterViewEdit'; } elseif ( count( $params ) === 5 && $params[2] === 'diff' ) { // Special:AbuseFilter/history//diff// diff --git a/includes/special/SpecialAbuseLog.php b/includes/special/SpecialAbuseLog.php index e28bf282b..28e87eee3 100644 --- a/includes/special/SpecialAbuseLog.php +++ b/includes/special/SpecialAbuseLog.php @@ -123,7 +123,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { $args = explode( '/', $parameter ); if ( count( $args ) === 2 && $args[0] === 'private' ) { - $this->showPrivateDetails( $args[1] ); + $this->showPrivateDetails( (int)$args[1] ); } elseif ( count( $args ) === 1 && $args[0] !== '' ) { if ( $args[0] === 'private' ) { $out->addWikiMsg( 'abusefilter-invalid-request-noid' ); @@ -313,7 +313,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { } /** - * @param string $id + * @param int $id */ public function showHideForm( $id ) { $output = $this->getOutput(); @@ -375,7 +375,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { // Show suppress log for this entry $suppressLogPage = new LogPage( 'suppress' ); $output->addHTML( "

" . $suppressLogPage->getName()->escaped() . "

\n" ); - LogEventsList::showLogExtract( $output, 'suppress', $this->getPageTitle( $id ) ); + LogEventsList::showLogExtract( $output, 'suppress', $this->getPageTitle( (string)$id ) ); } /** @@ -597,7 +597,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { } /** - * @param string $id + * @param string|int $id */ public function showDetails( $id ) { $out = $this->getOutput(); @@ -897,7 +897,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { } /** - * @param string $id + * @param int $id * @return void */ public function showPrivateDetails( $id ) { @@ -968,7 +968,7 @@ class SpecialAbuseLog extends AbuseFilterSpecialPage { * @return void */ public static function addPrivateDetailsAccessLogEntry( $logID, $reason, User $user ) { - $target = self::getTitleFor( 'AbuseLog', $logID ); + $target = self::getTitleFor( 'AbuseLog', (string)$logID ); $logEntry = new ManualLogEntry( 'abusefilterprivatedetails', 'access' ); $logEntry->setPerformer( $user );