From 55d068708a1e81780e4b4f521816088fba1c63b6 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Tue, 6 Jun 2017 18:18:36 +0200 Subject: [PATCH] Add phpcs and make pass Change-Id: If5a2ec9700148eb842d6580945e51e1942a2e0ed --- TitleBlacklist.hooks.php | 30 ++++++++-------- TitleBlacklist.library.php | 14 ++++---- TitleBlacklist.list.php | 34 +++++++++---------- api/ApiQueryTitleBlacklist.php | 32 ++++++++--------- composer.json | 7 ++-- phpcs.xml | 18 ++++++++++ tests/phpunit/ApiQueryTitleBlacklistTest.php | 26 +++++++------- ...itleBlacklistAuthenticationRequestTest.php | 2 +- 8 files changed, 92 insertions(+), 71 deletions(-) create mode 100644 phpcs.xml diff --git a/TitleBlacklist.hooks.php b/TitleBlacklist.hooks.php index 94b134a9..d0495acf 100644 --- a/TitleBlacklist.hooks.php +++ b/TitleBlacklist.hooks.php @@ -54,10 +54,10 @@ class TitleBlacklistHooks { $blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action ); if ( $blacklisted instanceof TitleBlacklistEntry ) { $errmsg = $blacklisted->getErrorMessage( 'edit' ); - $params = array( + $params = [ $blacklisted->getRaw(), $title->getFullText() - ); + ]; ApiResult::setIndexedTagName( $params, 'param' ); $result = ApiMessage::create( wfMessage( @@ -66,18 +66,18 @@ class TitleBlacklistHooks { $title->getFullText() ), 'titleblacklist-forbidden', - array( - 'message' => array( + [ + 'message' => [ 'key' => $errmsg, 'params' => $params, - ), + ], 'line' => $blacklisted->getRaw(), // As $errmsg usually represents a non-default message here, and ApiBase // uses ->inLanguage( 'en' )->useDatabase( false ) for all messages, it will // never result in useful 'info' text in the API. Try this, extra data seems // to override the default. 'info' => 'TitleBlacklist prevents this title from being created', - ) + ] ); return false; } @@ -265,12 +265,12 @@ class TitleBlacklistHooks { $errlines = '* ' . implode( "\n* ", array_map( 'wfEscapeWikiText', $ok ) ) . ''; - $error = Html::openElement( 'div', array( 'class' => 'errorbox' ) ) . + $error = Html::openElement( 'div', [ 'class' => 'errorbox' ] ) . $errmsg . "\n" . $errlines . Html::closeElement( 'div' ) . "\n" . - Html::element( 'br', array( 'clear' => 'all' ) ) . "\n"; + Html::element( 'br', [ 'clear' => 'all' ] ) . "\n"; // $error will be displayed by the edit class } @@ -283,8 +283,8 @@ class TitleBlacklistHooks { * @param Article $article */ public static function clearBlacklist( &$article, &$user, - $content, $summary, $isminor, $iswatch, $section ) - { + $content, $summary, $isminor, $iswatch, $section + ) { $title = $article->getTitle(); if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) { TitleBlacklist::singleton()->invalidate(); @@ -311,10 +311,10 @@ class TitleBlacklistHooks { */ public static function onAPIGetAllowedParams( ApiBase &$module, array &$params ) { if ( $module instanceof ApiCreateAccount ) { - $params['ignoretitleblacklist'] = array( + $params['ignoretitleblacklist'] = [ ApiBase::PARAM_TYPE => 'boolean', ApiBase::PARAM_DFLT => false - ); + ]; } return true; @@ -356,9 +356,9 @@ class TitleBlacklistHooks { $logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' ); $logEntry->setPerformer( $user ); $logEntry->setTarget( $title ); - $logEntry->setParameters( array( + $logEntry->setParameters( [ '4::entry' => $entry, - ) ); + ] ); $logid = $logEntry->insert(); $logEntry->publish( $logid ); } @@ -372,7 +372,7 @@ class TitleBlacklistHooks { * @return bool */ public static function scribuntoExternalLibraries( $engine, array &$extraLibraries ) { - if( $engine == 'lua' ) { + if ( $engine == 'lua' ) { $extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary'; } return true; diff --git a/TitleBlacklist.library.php b/TitleBlacklist.library.php index 87a016db..3ed4ce06 100644 --- a/TitleBlacklist.library.php +++ b/TitleBlacklist.library.php @@ -2,12 +2,12 @@ class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase { public function register() { - $lib = array( - 'test' => array( $this, 'test' ), - ); + $lib = [ + 'test' => [ $this, 'test' ], + ]; return $this->getEngine()->registerInterface( - __DIR__ . '/mw.ext.TitleBlacklist.lua', $lib, array() + __DIR__ . '/mw.ext.TitleBlacklist.lua', $lib, [] ); } @@ -20,16 +20,16 @@ class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase { } $entry = TitleBlacklist::singleton()->isBlacklisted( $title, $action ); if ( $entry ) { - return array( array( + return [ [ 'params' => $entry->getParams(), 'regex' => $entry->getRegex(), 'raw' => $entry->getRaw(), 'version' => $entry->getFormatVersion(), 'message' => $entry->getErrorMessage( $action ), 'custommessage' => $entry->getCustomMessage() - ) ); + ] ]; } - return array( null ); + return [ null ]; } } diff --git a/TitleBlacklist.list.php b/TitleBlacklist.list.php index a039bcf3..1c1a8950 100644 --- a/TitleBlacklist.list.php +++ b/TitleBlacklist.list.php @@ -7,7 +7,6 @@ * @file */ -//@{ /** * @ingroup Extensions */ @@ -70,9 +69,9 @@ class TitleBlacklist { } $sources = $wgTitleBlacklistSources; - $sources['local'] = array( 'type' => 'message' ); - $this->mBlacklist = array(); - foreach( $sources as $sourceName => $source ) { + $sources['local'] = [ 'type' => 'message' ]; + $this->mBlacklist = []; + foreach ( $sources as $sourceName => $source ) { $this->mBlacklist = array_merge( $this->mBlacklist, $this->parseBlacklist( $this->getBlacklistText( $source ), $sourceName ) @@ -156,7 +155,7 @@ class TitleBlacklist { */ public static function parseBlacklist( $list, $sourceName ) { $lines = preg_split( "/\r?\n/", $list ); - $result = array(); + $result = []; foreach ( $lines as $line ) { $line = TitleBlacklistEntry :: newFromString( $line, $sourceName ); if ( $line ) { @@ -312,7 +311,7 @@ class TitleBlacklist { * @return Array of bad entries; empty array means blacklist is valid */ public function validate( $blacklist ) { - $badEntries = array(); + $badEntries = []; foreach ( $blacklist as $e ) { wfSuppressWarnings(); $regex = $e->getRegex(); @@ -337,7 +336,6 @@ class TitleBlacklist { } } - /** * Represents a title blacklist entry */ @@ -370,15 +368,15 @@ class TitleBlacklistEntry { private function filtersNewAccounts() { global $wgTitleBlacklistUsernameSources; - if( $wgTitleBlacklistUsernameSources === '*' ) { + if ( $wgTitleBlacklistUsernameSources === '*' ) { return true; } - if( !$wgTitleBlacklistUsernameSources ) { + if ( !$wgTitleBlacklistUsernameSources ) { return false; } - if( !is_array( $wgTitleBlacklistUsernameSources ) ) { + if ( !is_array( $wgTitleBlacklistUsernameSources ) ) { throw new Exception( '$wgTitleBlacklistUsernameSources must be "*", false or an array' ); } @@ -462,7 +460,7 @@ class TitleBlacklistEntry { */ public static function newFromString( $line, $source ) { $raw = $line; // Keep line for raw data - $options = array(); + $options = []; // Strip comments $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line ); $line = trim( $line ); @@ -507,7 +505,7 @@ class TitleBlacklistEntry { preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER ); foreach ( $magicwords as $mword ) { global $wgParser; // Functions we're calling don't need, nevertheless let's use it - switch( strtolower( $mword[1] ) ) { + switch ( strtolower( $mword[1] ) ) { case 'ns': $cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] ); if ( is_string( $cpf_result ) ) { @@ -523,7 +521,7 @@ class TitleBlacklistEntry { } } // Return result - if( $regex ) { + if ( $regex ) { return new TitleBlacklistEntry( $regex, $options, $raw, $source ); } else { return null; @@ -561,14 +559,18 @@ class TitleBlacklistEntry { /** * @return string The format version */ - public function getFormatVersion() { return $this->mFormatVersion; } + public function getFormatVersion() { + return $this->mFormatVersion; + } /** * Set the format version * * @param $v string New version to set */ - public function setFormatVersion( $v ) { $this->mFormatVersion = $v; } + public function setFormatVersion( $v ) { + $this->mFormatVersion = $v; + } /** * Return the error message name for the blacklist entry. @@ -585,5 +587,3 @@ class TitleBlacklistEntry { return $message ? $message : "titleblacklist-forbidden-{$operation}"; } } - -//@} diff --git a/api/ApiQueryTitleBlacklist.php b/api/ApiQueryTitleBlacklist.php index cf72260b..53dab845 100644 --- a/api/ApiQueryTitleBlacklist.php +++ b/api/ApiQueryTitleBlacklist.php @@ -47,10 +47,10 @@ class ApiQueryTitleBlacklist extends ApiBase { if ( !$title ) { if ( is_callable( [ $this, 'dieWithError' ] ) ) { $this->dieWithError( - array( 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ) + [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] ); } else { - $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); + $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] ); } } @@ -59,10 +59,10 @@ class ApiQueryTitleBlacklist extends ApiBase { ); if ( $blacklisted instanceof TitleBlacklistEntry ) { // this title is blacklisted. - $result = array( + $result = [ htmlspecialchars( $blacklisted->getRaw() ), htmlspecialchars( $params['title'] ), - ); + ]; $res = $this->getResult(); $res->addValue( 'titleblacklist', 'result', 'blacklisted' ); @@ -78,33 +78,33 @@ class ApiQueryTitleBlacklist extends ApiBase { } public function getAllowedParams() { - return array( - 'title' => array( + return [ + 'title' => [ ApiBase::PARAM_REQUIRED => true, - ), - 'action' => array( + ], + 'action' => [ ApiBase::PARAM_DFLT => 'edit', ApiBase::PARAM_ISMULTI => false, - ApiBase::PARAM_TYPE => array( + ApiBase::PARAM_TYPE => [ // createtalk and createpage are useless as they're treated exactly like create 'create', 'edit', 'upload', 'createtalk', 'createpage', 'move', 'new-account' - ), - ), - 'nooverride' => array( + ], + ], + 'nooverride' => [ ApiBase::PARAM_DFLT => false, - ) - ); + ] + ]; } /** * @see ApiBase::getExamplesMessages() */ protected function getExamplesMessages() { - return array( + return [ 'action=titleblacklist&tbtitle=Foo' => 'apihelp-titleblacklist-example-1', 'action=titleblacklist&tbtitle=Bar&tbaction=edit' => 'apihelp-titleblacklist-example-2', - ); + ]; } } diff --git a/composer.json b/composer.json index a3aecbe7..e4e502fc 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,14 @@ { "require-dev": { "jakub-onderka/php-parallel-lint": "0.9.2", - "jakub-onderka/php-console-highlighter": "0.3.2" + "jakub-onderka/php-console-highlighter": "0.3.2", + "mediawiki/mediawiki-codesniffer": "0.7.2" }, "scripts": { + "fix": "phpcbf", "test": [ - "parallel-lint . --exclude node_modules --exclude vendor" + "parallel-lint . --exclude node_modules --exclude vendor", + "phpcs -p -s" ] } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..e16cb543 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + . + + + vendor + node_modules + diff --git a/tests/phpunit/ApiQueryTitleBlacklistTest.php b/tests/phpunit/ApiQueryTitleBlacklistTest.php index 9a31968c..21f71e80 100644 --- a/tests/phpunit/ApiQueryTitleBlacklistTest.php +++ b/tests/phpunit/ApiQueryTitleBlacklistTest.php @@ -24,12 +24,12 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase { $this->doLogin(); TitleBlacklist::destroySingleton(); - $this->setMwGlobals( 'wgTitleBlacklistSources', array( - array( + $this->setMwGlobals( 'wgTitleBlacklistSources', [ + [ 'type' => 'file', 'src' => __DIR__ . '/testSource', - ), - ) ); + ], + ] ); } function tearDown() { @@ -41,13 +41,13 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase { * Verify we allow a title which is not blacklisted */ function testCheckingUnlistedTitle() { - $unlisted = $this->doApiRequest( array( + $unlisted = $this->doApiRequest( [ 'action' => 'titleblacklist', // evil_acc is blacklisted as 'tbtitle' => 'evil_acc', 'tbaction' => 'create', 'tbnooverride' => true, - ) ); + ] ); $this->assertEquals( 'ok', @@ -66,11 +66,11 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase { $this->stashMwGlobals( 'wgGroupPermissions' ); $wgGroupPermissions['*']['tboverride'] = true; - $unlisted = $this->doApiRequest( array( + $unlisted = $this->doApiRequest( [ 'action' => 'titleblacklist', 'tbtitle' => 'bar', 'tbaction' => 'create', - ) ); + ] ); $this->assertEquals( 'ok', @@ -83,12 +83,12 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase { * Verify a blacklisted title gives out an error. */ function testCheckingBlackListedTitle() { - $listed = $this->doApiRequest( array( + $listed = $this->doApiRequest( [ 'action' => 'titleblacklist', 'tbtitle' => 'bar', 'tbaction' => 'create', 'tbnooverride' => true, - ) ); + ] ); $this->assertEquals( 'blacklisted', @@ -119,16 +119,16 @@ class ApiQueryTitleBlacklistTest extends ApiTestCase { * Tests integration with the AntiSpoof extension */ function testAntiSpoofIntegration() { - if ( !class_exists( 'AntiSpoof') ) { + if ( !class_exists( 'AntiSpoof' ) ) { $this->markTestSkipped( "This test requires the AntiSpoof extension" ); } - $listed = $this->doApiRequest( array( + $listed = $this->doApiRequest( [ 'action' => 'titleblacklist', 'tbtitle' => 'AVVVV', 'tbaction' => 'create', 'tbnooverride' => true, - ) ); + ] ); $this->assertEquals( 'blacklisted', diff --git a/tests/phpunit/TitleBlacklistAuthenticationRequestTest.php b/tests/phpunit/TitleBlacklistAuthenticationRequestTest.php index 30e1020a..ea084025 100644 --- a/tests/phpunit/TitleBlacklistAuthenticationRequestTest.php +++ b/tests/phpunit/TitleBlacklistAuthenticationRequestTest.php @@ -12,7 +12,7 @@ class TitleBlacklistAuthenticationRequestTest extends AuthenticationRequestTestC parent::setUp(); } - protected function getInstance( array $args = [ ] ) { + protected function getInstance( array $args = [] ) { return new TitleBlacklistAuthenticationRequest(); }