[MediaWiki-commits] [Gerrit] Make constructor of Block accept array of options - change (mediawiki/core)
jenkins-bot has submitted this change and it was merged. Change subject: Make constructor of Block accept array of options .. Make constructor of Block accept array of options Block::__construct now accepts an array of options instead of a myriad of optional parameters. Also add a test for the old constructor. Change-Id: I6ccd4df569ab49ad841a1ad591e23cafb1715841 --- M RELEASE-NOTES-1.26 M includes/Block.php M tests/phpunit/includes/BlockTest.php M tests/phpunit/includes/TitlePermissionTest.php 4 files changed, 179 insertions(+), 84 deletions(-) Approvals: Anomie: Looks good to me, approved jenkins-bot: Verified diff --git a/RELEASE-NOTES-1.26 b/RELEASE-NOTES-1.26 index 419525b..896d1d5 100644 --- a/RELEASE-NOTES-1.26 +++ b/RELEASE-NOTES-1.26 @@ -93,6 +93,9 @@ * wfSuppressWarnings() and wfRestoreWarnings() were split into a separate library, mediawiki/at-ease, and are now deprecated. Callers should use MediaWiki\suppressWarnings() and MediaWiki\restoreWarnings() directly. +* The Block class constructor now takes an associative array of parameters + instead of many optional positional arguments. Calling the constructor the old + way will issue a deprecation warning. == Compatibility == diff --git a/includes/Block.php b/includes/Block.php index 85cfe59..2cbf2d7 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -23,15 +23,16 @@ /** @var string */ public $mReason; - /** @var bool|string */ + /** @var string */ public $mTimestamp; - /** @var int */ + /** @var bool */ public $mAuto; - /** @var bool|string */ + /** @var string */ public $mExpiry; + /** @var bool */ public $mHideName; /** @var int */ @@ -65,10 +66,10 @@ protected $blocker; /** @var bool */ - protected $isHardblock = true; + protected $isHardblock; /** @var bool */ - protected $isAutoblocking = true; + protected $isAutoblocking; # TYPE constants const TYPE_USER = 1; @@ -78,55 +79,84 @@ const TYPE_ID = 5; /** -* @todo FIXME: Don't know what the best format to have for this constructor -* is, but fourteen optional parameters certainly isn't it. -* @param string $address -* @param int $user -* @param int $by -* @param string $reason -* @param mixed $timestamp -* @param int $auto -* @param string $expiry -* @param int $anonOnly -* @param int $createAccount -* @param int $enableAutoblock -* @param int $hideName -* @param int $blockEmail -* @param int $allowUsertalk -* @param string $byText +* Create a new block with specified parameters on a user, IP or IP range. +* +* @param array $options Parameters of the block: +* address string|User Target user name, User object, IP address or IP range +* user int Override target user ID (for foreign users) +* by int User ID of the blocker +* reason stringReason of the block +* timestamp string The time at which the block comes into effect +* auto boolIs this an automatic block? +* expiry stringTimestamp of expiration of the block or 'infinity' +* anonOnly boolOnly disallow anonymous actions +* createAccount bool Disallow creation of new accounts +* enableAutoblock bool Enable automatic blocking +* hideName boolHide the target user name +* blockEmail bool Disallow sending emails +* allowUsertalk bool Allow the target to edit its own talk page +* byText stringUsername of the blocker (for foreign users) +* +* @since 1.26 accepts $options array instead of individual parameters; order +* of parameters above reflects the original order */ - function __construct( $address = '', $user = 0, $by = 0, $reason = '', - $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0, - $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byText = '' - ) { - if ( $timestamp === 0 ) { - $timestamp = wfTimestampNow(); + function __construct( $options = array() ) { + $defaults = array( + 'address' = '', + 'user'= null, + 'by' = null, + 'reason' = '', + 'timestamp' = '', + 'auto'= false, + 'expiry' = '', + 'anonOnly'
[MediaWiki-commits] [Gerrit] Make constructor of Block accept array of options - change (mediawiki/core)
Matěj Grabovský has uploaded a new change for review. https://gerrit.wikimedia.org/r/134827 Change subject: Make constructor of Block accept array of options .. Make constructor of Block accept array of options Block::__construct now accepts an array of options instead of a myriad of optional parameters. Also deprecates the old calling style. Rename Block's protected variables $isHardblock and $isAutoblock to prevent confusion with namesake functions. Change-Id: I6ccd4df569ab49ad841a1ad591e23cafb1715841 --- M includes/Block.php M tests/phpunit/includes/BlockTest.php M tests/phpunit/includes/TitlePermissionTest.php 3 files changed, 137 insertions(+), 75 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/27/134827/1 diff --git a/includes/Block.php b/includes/Block.php index 3896369..ea598ca 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -23,15 +23,16 @@ /** @var string */ public $mReason; - /** @var bool|string */ + /** @var string */ public $mTimestamp; - /** @var int */ + /** @var bool */ public $mAuto; - /** @var bool|string */ + /** @var bool */ public $mExpiry; + /** @var bool */ public $mHideName; /** @var int */ @@ -65,10 +66,10 @@ protected $blocker; /** @var bool */ - protected $isHardblock = true; + protected $mHardblock; /** @var bool */ - protected $isAutoblocking = true; + protected $mAutoblock; # TYPE constants const TYPE_USER = 1; @@ -78,45 +79,96 @@ const TYPE_ID = 5; /** -* @todo FIXME: Don't know what the best format to have for this constructor -* is, but fourteen optional parameters certainly isn't it. +* Create a new block with specified parameters on a user, IP or IP range. +* +* @param array $options Parameters of the block: +* - address : Target user name, User object, IP address or IP range +* - user : Override target user ID (for foreign users) +* - by : User ID of the blocker +* - byText : Username of the blocker (for foreign users) +* - reason : Reason of the block +* - timestamp : The time at which the block comes into effect +* - auto : Is this an automatic block? +* - hideName : Hide the target user name +* - anonOnly : Only disallow anonymous actions +* - enableAutoblock : Enable automatic blocking +* - blockEmail : Disallow sending emails +* - allowUsertalk : Allow the target to edit its own talk page +* - createAccount : Disallow creation of new accounts +* - expiry : Timestamp of expiration of the block or 'infinity' */ - function __construct( $address = '', $user = 0, $by = 0, $reason = '', - $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0, - $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byText = '' - ) { - if ( $timestamp === 0 ) { - $timestamp = wfTimestampNow(); + function __construct( array $options = array() ) { + if ( func_num_args() 1 ) { + wfDeprecated( __METHOD__ . ' with multiple arguments', '1.24' ); + $options = array_combine( + array( 'address', 'user', 'by', 'reason', 'timestamp', 'auto', + 'expiry', 'anonOnly', 'createAccount', 'enableAutoblock', + 'hideName', 'blockEmail', 'allowUsertalk', 'byText' + ), + func_get_args() + ); } - if ( count( func_get_args() ) 0 ) { - # Soon... :D - # wfDeprecated( __METHOD__ . with arguments ); + if ( isset( $options['address'] ) ) { + $this-setTarget( $options['address'] ); } - $this-setTarget( $address ); - if ( $this-target instanceof User $user ) { - $this-forcedTargetID = $user; // needed for foreign users + if ( $this-target instanceof User isset( $options['user'] ) ) { + // Needed for foreign users + $this-forcedTargetID = $options['user']; } - if ( $by ) { // local user - $this-setBlocker( User::newFromID( $by ) ); - } else { // foreign user - $this-setBlocker( $byText ); + + if ( isset( $options['by'] ) ) { + // Local user + $this-setBlocker(