Start using the Assert helper class for checking parameters.
authordaniel <daniel.kinzler@wikimedia.de>
Wed, 22 Apr 2015 16:46:29 +0000 (18:46 +0200)
committerReedy <reedy@wikimedia.org>
Mon, 11 May 2015 17:54:07 +0000 (17:54 +0000)
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

composer.json
includes/libs/MapCacheLRU.php
includes/libs/ProcessCacheLRU.php
includes/title/TitleValue.php
includes/utils/UIDGenerator.php
tests/phpunit/includes/libs/ProcessCacheLRUTest.php

index 6dc72e7..1495198 100644 (file)
@@ -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"
index 0b6db32..a0230be 100644 (file)
@@ -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;
        }
 
index 8d80eb3..b55ff9d 100644 (file)
@@ -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 );
index 5cac347..a0f3b6f 100644 (file)
@@ -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;
index 9241587..cb32357 100644 (file)
@@ -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 );
 
index 4300197..cec662a 100644 (file)
@@ -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 ) {