Enable and enforce a few extra PHPCS sniffs

This is not really anything new. Most code already followed these
sniffs. This patch just fixes the remaining exceptions. Also:
* Remove PHPDoc blocks that don't add anything but just repeat the
  strict types.
* Remove @file comments in favor of class-level comments.
* Add strict types where possible, most notably some `void`.

Change-Id: Iff6872dff68170b0fc4e82ac2ba3cad385e8773e
This commit is contained in:
thiemowmde 2023-08-23 09:29:25 +02:00
parent bde2188280
commit 8f81812242
14 changed files with 67 additions and 92 deletions

View file

@ -3,11 +3,6 @@
namespace MediaWiki\Extension\EventLogging;
class EventLogging {
/**
* @param string $streamName
* @param array $event
*/
public static function submit( string $streamName, array $event ): void {
}
}

View file

@ -3,6 +3,22 @@
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment" />
</rule>
<rule ref="MediaWiki.Commenting.ClassLevelLicense">
<properties>
<property name="license" value="GPL-2.0-or-later" />
</properties>
</rule>
<rule ref="Squiz.Strings.DoubleQuoteUsage">
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar" />
</rule>
<rule ref="Squiz.WhiteSpace.FunctionSpacing.AfterLast">
<severity>5</severity>
</rule>
<rule ref="Squiz.WhiteSpace.FunctionSpacing.BeforeFirst">
<severity>5</severity>
</rule>
<file>.</file>
<arg name="extensions" value="php" />
<arg name="encoding" value="UTF-8" />

View file

@ -1,10 +1,4 @@
<?php
/**
* Implement the 'templatedata' query module in the API.
* Format JSON only.
*
* @file
*/
namespace MediaWiki\Extension\TemplateData\Api;
@ -21,16 +15,16 @@ use TextContent;
use Wikimedia\ParamValidator\ParamValidator;
/**
* Implement the 'templatedata' query module in the API.
* Format JSON only.
* @license GPL-2.0-or-later
* @ingroup API
* @emits error.code templatedata-corrupt
* @todo Support continuation (see I1a6e51cd)
*/
class ApiTemplateData extends ApiBase {
/**
* @var ApiPageSet|null
*/
private $mPageSet = null;
private ?ApiPageSet $mPageSet = null;
/**
* For backwards compatibility, this module needs to output format=json when
@ -47,9 +41,6 @@ class ApiTemplateData extends ApiBase {
return null;
}
/**
* @return ApiPageSet
*/
private function getPageSet(): ApiPageSet {
$this->mPageSet ??= new ApiPageSet( $this );
return $this->mPageSet;
@ -281,4 +272,5 @@ class ApiTemplateData extends ApiBase {
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:TemplateData';
}
}

View file

@ -1,7 +1,5 @@
<?php
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
namespace MediaWiki\Extension\TemplateData;
use CommentStoreComment;
@ -30,12 +28,9 @@ use ResourceLoader;
use Status;
/**
* Hooks for TemplateData extension
*
* @file
* @ingroup Extensions
* @license GPL-2.0-or-later
* phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName
*/
class Hooks implements
EditPage__showEditForm_fieldsHook,
ParserFirstCallInitHook,
@ -126,16 +121,11 @@ class Hooks implements
return true;
}
/**
* @param RevisionRecord $revisionRecord
* @param ?string $newPageProperty
* @param UserIdentity $user
*/
private static function logChangeEvent(
RevisionRecord $revisionRecord,
?string $newPageProperty,
UserIdentity $user
) {
): void {
if ( !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
return;
}
@ -213,7 +203,7 @@ class Hooks implements
*
* @return string HTML to insert in the page.
*/
public static function render( $input, $args, Parser $parser, $frame ) {
public static function render( ?string $input, array $args, Parser $parser, PPFrame $frame ): string {
$parserOutput = $parser->getOutput();
$ti = TemplateDataBlob::newFromJSON( wfGetDB( DB_REPLICA ), $input ?? '' );
@ -268,7 +258,6 @@ class Hooks implements
$localizer = new TemplateDataMessageLocalizer( $lang );
$formatter = new TemplateDataHtmlFormatter( $localizer, $lang->getCode() );
$formatter->replaceEditLink( $text );
}
}
@ -315,7 +304,7 @@ class Hooks implements
}
if ( !$title->exists() ) {
$tplData[$tplTitle] = (object)[ "missing" => true ];
$tplData[$tplTitle] = (object)[ 'missing' => true ];
continue;
}
@ -331,7 +320,7 @@ class Hooks implements
$props = $pageProps->getProperties( $title, 'templatedata' );
if ( !isset( $props[$pageId] ) ) {
// No templatedata
$tplData[$tplTitle] = (object)[ "notemplatedata" => true ];
$tplData[$tplTitle] = (object)[ 'notemplatedata' => true ];
continue;
}
@ -339,7 +328,7 @@ class Hooks implements
$status = $tdb->getStatus();
if ( !$status->isOK() ) {
// Invalid data. Parsoid has no use for the error.
$tplData[$tplTitle] = (object)[ "notemplatedata" => true ];
$tplData[$tplTitle] = (object)[ 'notemplatedata' => true ];
continue;
}

View file

@ -1,8 +1,4 @@
<?php
/**
* @file
* @ingroup Extensions
*/
namespace MediaWiki\Extension\TemplateData;
@ -15,18 +11,12 @@ use Wikimedia\Rdbms\IReadableDatabase;
* Represents the information about a template,
* coming from the JSON blob in the <templatedata> tags
* on wiki pages.
* @license GPL-2.0-or-later
*/
class TemplateDataBlob {
/**
* @var string
*/
protected $json;
/**
* @var Status
*/
protected $status;
protected string $json;
protected Status $status;
/**
* Parse and validate passed JSON and create a blob handling
@ -117,9 +107,6 @@ class TemplateDataBlob {
return null;
}
/**
* @return Status
*/
public function getStatus(): Status {
return $this->status;
}

View file

@ -1,8 +1,4 @@
<?php
/**
* @file
* @ingroup Extensions
*/
namespace MediaWiki\Extension\TemplateData;
@ -14,15 +10,17 @@ use Message;
* on wiki pages.
* This implementation stores the information as a compressed gzip blob
* in the database.
* @license GPL-2.0-or-later
*/
class TemplateDataCompressedBlob extends TemplateDataBlob {
// Size of MySQL 'blob' field; page_props table where the data is stored uses one.
private const MAX_LENGTH = 65535;
/**
* @var string In-object cache for {@see getJSONForDatabase}
*/
private $jsonDB;
private string $jsonDB;
/**
* @inheritDoc

View file

@ -9,18 +9,14 @@ use MediaWiki\Title\Title;
use MessageLocalizer;
use stdClass;
/**
* @license GPL-2.0-or-later
*/
class TemplateDataHtmlFormatter {
/** @var MessageLocalizer */
private $localizer;
private MessageLocalizer $localizer;
private string $languageCode;
/** @var string */
private $languageCode;
/**
* @param MessageLocalizer $localizer
* @param string $languageCode
*/
public function __construct( MessageLocalizer $localizer, string $languageCode = 'en' ) {
$this->localizer = $localizer;
$this->languageCode = $languageCode;
@ -57,7 +53,7 @@ class TemplateDataHtmlFormatter {
}
}
$sorting = count( (array)$data->params ) > 1 ? " sortable" : "";
$sorting = count( (array)$data->params ) > 1 ? ' sortable' : '';
$html = '<header>'
. Html::element( 'p',
[
@ -138,12 +134,12 @@ class TemplateDataHtmlFormatter {
*
* @param string &$text
*/
public function replaceEditLink( string &$text ) {
public function replaceEditLink( string &$text ): void {
$localizer = $this->localizer;
$text = preg_replace_callback(
// Based on EDITSECTION_REGEX in ParserOutput
'#<mw:edittemplatedata page="(.*?)"></mw:edittemplatedata>#s',
static function ( $m ) use ( $localizer ) {
static function ( array $m ) use ( $localizer ): string {
$editsectionPage = Title::newFromText( htmlspecialchars_decode( $m[1] ) );
if ( !is_object( $editsectionPage ) ) {

View file

@ -6,16 +6,13 @@ use Language;
use MessageLocalizer;
/**
* @license GPL-2.0-or-later
* @codeCoverageIgnore Trivial facade
*/
class TemplateDataMessageLocalizer implements MessageLocalizer {
/** @var Language */
private $language;
private Language $language;
/**
* @param Language $language
*/
public function __construct( Language $language ) {
$this->language = $language;
}

View file

@ -4,6 +4,9 @@ namespace MediaWiki\Extension\TemplateData;
use stdClass;
/**
* @license GPL-2.0-or-later
*/
class TemplateDataNormalizer {
public const DEPRECATED_PARAMETER_TYPES = [
@ -13,12 +16,8 @@ class TemplateDataNormalizer {
'string/wiki-file-name' => 'wiki-file-name',
];
/** @var string */
private string $contentLanguageCode;
/**
* @param string $contentLanguageCode
*/
public function __construct( string $contentLanguageCode ) {
$this->contentLanguageCode = $contentLanguageCode;
}
@ -26,7 +25,7 @@ class TemplateDataNormalizer {
/**
* @param stdClass $data Expected to be valid according to the {@see TemplateDataValidator}
*/
public function normalize( stdClass $data ) {
public function normalize( stdClass $data ): void {
$data->description ??= null;
$data->sets ??= [];
$data->maps ??= (object)[];
@ -55,10 +54,7 @@ class TemplateDataNormalizer {
}
}
/**
* @param stdClass $paramObj
*/
private function normalizeParameter( stdClass $paramObj ) {
private function normalizeParameter( stdClass $paramObj ): void {
$paramObj->label ??= null;
$paramObj->description ??= null;
$paramObj->required ??= false;
@ -91,7 +87,7 @@ class TemplateDataNormalizer {
/**
* @param string|stdClass &$text
*/
private function normaliseInterfaceText( &$text ) {
private function normaliseInterfaceText( &$text ): void {
if ( is_string( $text ) ) {
$text = (object)[ $this->contentLanguageCode => $text ];
}

View file

@ -4,6 +4,9 @@ namespace MediaWiki\Extension\TemplateData;
use Status;
/**
* @license GPL-2.0-or-later
*/
class TemplateDataStatus {
/**

View file

@ -5,6 +5,9 @@ namespace MediaWiki\Extension\TemplateData;
use Status;
use stdClass;
/**
* @license GPL-2.0-or-later
*/
class TemplateDataValidator {
public const PREDEFINED_FORMATS = [
@ -53,7 +56,7 @@ class TemplateDataValidator {
];
/** @var string[] */
private $validParameterTypes;
private array $validParameterTypes;
/**
* @param string[] $additionalParameterTypes
@ -282,7 +285,7 @@ class TemplateDataValidator {
} elseif ( count( $paramOrder ) < count( (array)$params ) ) {
$missing = array_diff( array_keys( (array)$params ), $paramOrder );
return Status::newFatal( 'templatedata-invalid-missing',
"paramOrder[ \"" . implode( '", "', $missing ) . '" ]' );
'paramOrder[ "' . implode( '", "', $missing ) . '" ]' );
}
// Validate each of the values corresponds to a parameter and that there are no
@ -413,10 +416,6 @@ class TemplateDataValidator {
return null;
}
/**
* @param string|null $format
* @return bool
*/
private function isValidCustomFormatString( ?string $format ): bool {
return $format && preg_match( '/^\n?{{ *_+\n? *\|\n? *_+ *= *_+\n? *}}\n?$/', $format );
}

View file

@ -9,6 +9,9 @@ require_once "$IP/maintenance/Maintenance.php";
use MediaWiki\Extension\TemplateData\TemplateDataBlob;
use MediaWiki\Title\Title;
/**
* @license GPL-2.0-or-later
*/
class ValidateTemplateData extends Maintenance {
public function __construct() {
@ -60,6 +63,7 @@ class ValidateTemplateData extends Maintenance {
$this->output( "Rows checked: {$rowsChecked}\n" );
$this->output( "Bad rows: {$badRows}\n" );
}
}
$maintClass = ValidateTemplateData::class;

View file

@ -3,8 +3,8 @@
use MediaWiki\Extension\TemplateData\TemplateDataStatus;
/**
* @group TemplateData
* @covers \MediaWiki\Extension\TemplateData\TemplateDataStatus
* @license GPL-2.0-or-later
*/
class SerializationTest extends MediaWikiIntegrationTestCase {

View file

@ -8,12 +8,12 @@ use MediaWiki\Title\Title;
use Wikimedia\TestingAccessWrapper;
/**
* @group TemplateData
* @group Database
* @covers \MediaWiki\Extension\TemplateData\TemplateDataBlob
* @covers \MediaWiki\Extension\TemplateData\TemplateDataCompressedBlob
* @covers \MediaWiki\Extension\TemplateData\TemplateDataNormalizer
* @covers \MediaWiki\Extension\TemplateData\TemplateDataValidator
* @license GPL-2.0-or-later
*/
class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
@ -1537,9 +1537,11 @@ HTML
public function testGetHtml( array $data, string $expected ) {
$t = TemplateDataBlob::newFromJSON( $this->db, json_encode( $data ) );
$localizer = new class implements MessageLocalizer {
public function msg( $key, ...$params ) {
return new RawMessage( "($key)" );
}
};
$title = Title::newFromText( 'Template:Test/doc' );
$formatter = new TemplateDataHtmlFormatter( $localizer );
@ -1550,4 +1552,5 @@ HTML
$this->assertSame( $linedExpected, $linedActual );
}
}