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; namespace MediaWiki\Extension\EventLogging;
class EventLogging { class EventLogging {
/**
* @param string $streamName
* @param array $event
*/
public static function submit( string $streamName, array $event ): void { public static function submit( string $streamName, array $event ): void {
} }
} }

View file

@ -3,6 +3,22 @@
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki"> <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment" /> <exclude name="MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment" />
</rule> </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> <file>.</file>
<arg name="extensions" value="php" /> <arg name="extensions" value="php" />
<arg name="encoding" value="UTF-8" /> <arg name="encoding" value="UTF-8" />

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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