From: daniel Date: Wed, 22 Apr 2015 16:46:29 +0000 (+0200) Subject: Start using the Assert helper class for checking parameters. X-Git-Tag: 1.31.0-rc.0~11443^2 X-Git-Url: http://git.cyclocoop.org/data/Luca_Pacioli_%28Gemaelde%29.jpeg?a=commitdiff_plain;h=70812282142b19d8154861a2014a727cfc14720a;p=lhc%2Fweb%2Fwiklou.git Start using the Assert helper class for checking parameters. This introduces https://github.com/wmde/Assert as a dependency, as discussed in the RFC T91071. This change uses assertions to check some parameters in some places, to showcase the main intended use case for assertions in MediaWiki. Bug: T91071 Change-Id: I93ac39b7c146f10532e37b51d973b59b9c424b2f --- diff --git a/composer.json b/composer.json index 6dc72e73b5..1495198557 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "php": ">=5.3.3", "psr/log": "1.0.0", "wikimedia/cdb": "1.0.1", + "wikimedia/assert": "0.2.2", "wikimedia/composer-merge-plugin": "1.0.0", "wikimedia/utfnormal": "1.0.2", "zordius/lightncandy": "0.18" diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php index 0b6db32ef9..a0230bee83 100644 --- a/includes/libs/MapCacheLRU.php +++ b/includes/libs/MapCacheLRU.php @@ -20,6 +20,7 @@ * @file * @ingroup Cache */ +use Wikimedia\Assert\Assert; /** * Handles a simple LRU key/value map with a maximum number of entries @@ -41,9 +42,9 @@ class MapCacheLRU { * @throws Exception When $maxCacheKeys is not an int or =< 0. */ public function __construct( $maxKeys ) { - if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { - throw new Exception( __METHOD__ . " must be given an integer and >= 1" ); - } + Assert::parameterType( 'integer', $maxKeys, '$maxKeys' ); + Assert::parameter( $maxKeys >= 1, '$maxKeys', 'must be >= 1' ); + $this->maxCacheKeys = $maxKeys; } diff --git a/includes/libs/ProcessCacheLRU.php b/includes/libs/ProcessCacheLRU.php index 8d80eb3867..b55ff9da8b 100644 --- a/includes/libs/ProcessCacheLRU.php +++ b/includes/libs/ProcessCacheLRU.php @@ -20,6 +20,7 @@ * @file * @ingroup Cache */ +use Wikimedia\Assert\Assert; /** * Handles per process caching of items @@ -128,9 +129,9 @@ class ProcessCacheLRU { * @throws UnexpectedValueException */ public function resize( $maxKeys ) { - if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { - throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" ); - } + Assert::parameterType( 'integer', $maxKeys, '$maxKeys' ); + Assert::parameter( $maxKeys >= 1, '$maxKeys', 'must be >= 1' ); + $this->maxCacheKeys = $maxKeys; while ( count( $this->cache ) > $this->maxCacheKeys ) { reset( $this->cache ); diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 5cac34704d..a0f3b6f9b2 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -21,6 +21,7 @@ * @license GPL 2+ * @author Daniel Kinzler */ +use Wikimedia\Assert\Assert; /** * Represents a page (or page fragment) title within %MediaWiki. @@ -67,26 +68,13 @@ class TitleValue { * @throws InvalidArgumentException */ public function __construct( $namespace, $dbkey, $fragment = '' ) { - if ( !is_int( $namespace ) ) { - throw new InvalidArgumentException( '$namespace must be an integer' ); - } - - if ( !is_string( $dbkey ) ) { - throw new InvalidArgumentException( '$dbkey must be a string' ); - } + Assert::parameterType( 'integer', $namespace, '$namespace' ); + Assert::parameterType( 'string', $dbkey, '$dbkey' ); + Assert::parameterType( 'string', $fragment, '$fragment' ); // Sanity check, no full validation or normalization applied here! - if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) { - throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey ); - } - - if ( !is_string( $fragment ) ) { - throw new InvalidArgumentException( '$fragment must be a string' ); - } - - if ( $dbkey === '' ) { - throw new InvalidArgumentException( '$dbkey must not be empty' ); - } + Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' ); + Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' ); $this->namespace = $namespace; $this->dbkey = $dbkey; diff --git a/includes/utils/UIDGenerator.php b/includes/utils/UIDGenerator.php index 92415877f7..cb32357cc3 100644 --- a/includes/utils/UIDGenerator.php +++ b/includes/utils/UIDGenerator.php @@ -20,6 +20,7 @@ * @file * @author Aaron Schulz */ +use Wikimedia\Assert\Assert; /** * Class for getting statistically unique IDs @@ -107,9 +108,10 @@ class UIDGenerator { * @throws MWException */ public static function newTimestampedUID88( $base = 10 ) { - if ( !is_integer( $base ) || $base > 36 || $base < 2 ) { - throw new MWException( "Base must an integer be between 2 and 36" ); - } + Assert::parameterType( 'integer', $base, '$base' ); + Assert::parameter( $base <= 36, '$base', 'must be <= 36' ); + Assert::parameter( $base >= 2, '$base', 'must be >= 2' ); + $gen = self::singleton(); $time = $gen->getTimestampAndDelay( 'lockFile88', 1, 1024 ); @@ -152,9 +154,10 @@ class UIDGenerator { * @throws MWException */ public static function newTimestampedUID128( $base = 10 ) { - if ( !is_integer( $base ) || $base > 36 || $base < 2 ) { - throw new MWException( "Base must be an integer between 2 and 36" ); - } + Assert::parameterType( 'integer', $base, '$base' ); + Assert::parameter( $base <= 36, '$base', 'must be <= 36' ); + Assert::parameter( $base >= 2, '$base', 'must be >= 2' ); + $gen = self::singleton(); $time = $gen->getTimestampAndDelay( 'lockFile128', 16384, 1048576 ); diff --git a/tests/phpunit/includes/libs/ProcessCacheLRUTest.php b/tests/phpunit/includes/libs/ProcessCacheLRUTest.php index 43001979fa..cec662a996 100644 --- a/tests/phpunit/includes/libs/ProcessCacheLRUTest.php +++ b/tests/phpunit/includes/libs/ProcessCacheLRUTest.php @@ -70,7 +70,7 @@ class ProcessCacheLRUTest extends PHPUnit_Framework_TestCase { /** * @dataProvider provideInvalidConstructorArg - * @expectedException UnexpectedValueException + * @expectedException Wikimedia\Assert\ParameterAssertionException * @covers ProcessCacheLRU::__construct */ public function testConstructorGivenInvalidValue( $maxSize ) {