Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/404975 )
Change subject: Use Wikimedia\Assert and type hints
......................................................................
Use Wikimedia\Assert and type hints
This reduces the verbosity of some error handling code, and since parts
of that error handling code were untested, also increases test coverage.
Change-Id: Idc984d31e27d61bc625663c9db1c1a804fe385ef
---
M includes/Html/HtmlTableBuilder.php
M includes/Html/HtmlTableCellBuilder.php
M includes/Html/HtmlTableHeaderBuilder.php
M tests/phpunit/Html/HtmlTableBuilderTest.php
M tests/phpunit/Html/HtmlTableCellBuilderTest.php
M tests/phpunit/Html/HtmlTableHeaderBuilderTest.php
6 files changed, 24 insertions(+), 42 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuality
refs/changes/75/404975/1
diff --git a/includes/Html/HtmlTableBuilder.php
b/includes/Html/HtmlTableBuilder.php
index 6bb7a0d..8a9a98e 100644
--- a/includes/Html/HtmlTableBuilder.php
+++ b/includes/Html/HtmlTableBuilder.php
@@ -4,6 +4,7 @@
use InvalidArgumentException;
use Html;
+use Wikimedia\Assert\Assert;
/**
* @package WikibaseQuality\Html
@@ -31,16 +32,8 @@
/**
* @param array $headers
- *
- * @throws InvalidArgumentException
*/
- public function __construct( $headers ) {
- if ( !is_array( $headers ) ) {
- throw new InvalidArgumentException(
- '$headers must be an array of strings or
HtmlTableHeader elements.'
- );
- }
-
+ public function __construct( array $headers ) {
foreach ( $headers as $header ) {
$this->addHeader( $header );
}
@@ -52,14 +45,10 @@
* @throws InvalidArgumentException
*/
private function addHeader( $header ) {
+ Assert::parameterType(
'string|\WikibaseQuality\Html\HtmlTableHeaderBuilder', $header, '$header' );
+
if ( is_string( $header ) ) {
$header = new HtmlTableHeaderBuilder( $header );
- }
-
- if ( !( $header instanceof HtmlTableHeaderBuilder ) ) {
- throw new InvalidArgumentException(
- 'Each element in $headers must be a string or
an HtmlTableHeader'
- );
}
$this->headers[] = $header;
diff --git a/includes/Html/HtmlTableCellBuilder.php
b/includes/Html/HtmlTableCellBuilder.php
index d49a056..691c39d 100644
--- a/includes/Html/HtmlTableCellBuilder.php
+++ b/includes/Html/HtmlTableCellBuilder.php
@@ -2,8 +2,9 @@
namespace WikibaseQuality\Html;
-use InvalidArgumentException;
use Html;
+use Wikimedia\Assert\Assert;
+use Wikimedia\Assert\ParameterTypeException;
/**
* @package WikibaseQuality\Html
@@ -36,16 +37,11 @@
* @param array $attributes
* @param bool $isRawContent
*
- * @throws InvalidArgumentException
+ * @throws ParameterTypeException
*/
public function __construct( $content, array $attributes = [],
$isRawContent = false ) {
- // Check parameters
- if ( !is_string( $content ) ) {
- throw new InvalidArgumentException( '$content must be
string.' );
- }
- if ( !is_bool( $isRawContent ) ) {
- throw new InvalidArgumentException( '$isRawContent must
be boolean.' );
- }
+ Assert::parameterType( 'string', $content, '$content' );
+ Assert::parameterType( 'boolean', $isRawContent,
'$isRawContent' );
$this->content = $content;
$this->attributes = $attributes;
diff --git a/includes/Html/HtmlTableHeaderBuilder.php
b/includes/Html/HtmlTableHeaderBuilder.php
index c1e03ed..15c906c 100644
--- a/includes/Html/HtmlTableHeaderBuilder.php
+++ b/includes/Html/HtmlTableHeaderBuilder.php
@@ -2,8 +2,9 @@
namespace WikibaseQuality\Html;
-use InvalidArgumentException;
use Html;
+use Wikimedia\Assert\Assert;
+use Wikimedia\Assert\ParameterTypeException;
/**
* @package WikibaseQuality\Html
@@ -38,18 +39,12 @@
* @param bool $isSortable
* @param bool $isRawContent
*
- * @throws InvalidArgumentException
+ * @throws ParameterTypeException
*/
public function __construct( $content, $isSortable = false,
$isRawContent = false ) {
- if ( !is_string( $content ) ) {
- throw new InvalidArgumentException( '$content must be
string.' );
- }
- if ( !is_bool( $isSortable ) ) {
- throw new InvalidArgumentException( '$isSortable must
be boolean.' );
- }
- if ( !is_bool( $isRawContent ) ) {
- throw new InvalidArgumentException( '$isRawContent must
be boolean.' );
- }
+ Assert::parameterType( 'string', $content, '$content' );
+ Assert::parameterType( 'boolean', $isSortable, '$isSortable' );
+ Assert::parameterType( 'boolean', $isRawContent,
'$isRawContent' );
$this->content = $content;
$this->isSortable = $isSortable;
diff --git a/tests/phpunit/Html/HtmlTableBuilderTest.php
b/tests/phpunit/Html/HtmlTableBuilderTest.php
index 08c5e54..72fd14d 100644
--- a/tests/phpunit/Html/HtmlTableBuilderTest.php
+++ b/tests/phpunit/Html/HtmlTableBuilderTest.php
@@ -4,9 +4,11 @@
use InvalidArgumentException;
use PHPUnit_Framework_TestCase;
+use TypeError;
use WikibaseQuality\Html\HtmlTableBuilder;
use WikibaseQuality\Html\HtmlTableCellBuilder;
use WikibaseQuality\Html\HtmlTableHeaderBuilder;
+use Wikimedia\Assert\ParameterTypeException;
/**
* @covers \WikibaseQuality\Html\HtmlTableBuilder
@@ -82,13 +84,13 @@
[ 42 ],
null,
false,
- InvalidArgumentException::class
+ ParameterTypeException::class
],
[
'foobar',
null,
false,
- InvalidArgumentException::class
+ TypeError::class
]
];
}
diff --git a/tests/phpunit/Html/HtmlTableCellBuilderTest.php
b/tests/phpunit/Html/HtmlTableCellBuilderTest.php
index 08ccef9..dcd2c9d 100644
--- a/tests/phpunit/Html/HtmlTableCellBuilderTest.php
+++ b/tests/phpunit/Html/HtmlTableCellBuilderTest.php
@@ -2,9 +2,9 @@
namespace WikibaseQuality\ExternalValidation\Tests\Html;
-use InvalidArgumentException;
use PHPUnit_Framework_TestCase;
use WikibaseQuality\Html\HtmlTableCellBuilder;
+use Wikimedia\Assert\ParameterTypeException;
/**
* @covers \WikibaseQuality\Html\HtmlTableCellBuilder
@@ -48,7 +48,7 @@
[
42,
[],
- InvalidArgumentException::class
+ ParameterTypeException::class
]
];
}
diff --git a/tests/phpunit/Html/HtmlTableHeaderBuilderTest.php
b/tests/phpunit/Html/HtmlTableHeaderBuilderTest.php
index 3cc32c4..8570e37 100644
--- a/tests/phpunit/Html/HtmlTableHeaderBuilderTest.php
+++ b/tests/phpunit/Html/HtmlTableHeaderBuilderTest.php
@@ -2,9 +2,9 @@
namespace WikibaseQuality\ExternalValidation\Tests\Html;
-use InvalidArgumentException;
use PHPUnit_Framework_TestCase;
use WikibaseQuality\Html\HtmlTableHeaderBuilder;
+use Wikimedia\Assert\ParameterTypeException;
/**
* @covers \WikibaseQuality\Html\HtmlTableHeaderBuilder
@@ -41,12 +41,12 @@
[
42,
true,
- InvalidArgumentException::class
+ ParameterTypeException::class
],
[
'fooar',
42,
- InvalidArgumentException::class
+ ParameterTypeException::class
]
];
}
--
To view, visit https://gerrit.wikimedia.org/r/404975
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idc984d31e27d61bc625663c9db1c1a804fe385ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQuality
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits