jenkins-bot has submitted this change and it was merged. ( 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. In the case where error handling is now done via type hint, the test covering the behavior is removed, since it cannot be tested across PHP versions (TypeError is new in PHP 7). Change-Id: Idc984d31e27d61bc625663c9db1c1a804fe385ef --- M includes/Html/HtmlTableBuilder.php M includes/Html/HtmlTableCellBuilder.php M includes/Html/HtmlTableHeaderBuilder.php M tests/phpunit/Html/HtmlTableBuilderTest.php 4 files changed, 11 insertions(+), 37 deletions(-) Approvals: jenkins-bot: Verified Thiemo Kreuz (WMDE): Looks good to me, approved diff --git a/includes/Html/HtmlTableBuilder.php b/includes/Html/HtmlTableBuilder.php index 6bb7a0d..c1009ad 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|' . HtmlTableHeaderBuilder::class, $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..96ccdf7 100644 --- a/includes/Html/HtmlTableCellBuilder.php +++ b/includes/Html/HtmlTableCellBuilder.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use Html; +use Wikimedia\Assert\Assert; /** * @package WikibaseQuality\Html @@ -39,13 +40,8 @@ * @throws InvalidArgumentException */ 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..ffbb931 100644 --- a/includes/Html/HtmlTableHeaderBuilder.php +++ b/includes/Html/HtmlTableHeaderBuilder.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use Html; +use Wikimedia\Assert\Assert; /** * @package WikibaseQuality\Html @@ -41,15 +42,9 @@ * @throws InvalidArgumentException */ 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..6c498da 100644 --- a/tests/phpunit/Html/HtmlTableBuilderTest.php +++ b/tests/phpunit/Html/HtmlTableBuilderTest.php @@ -84,12 +84,6 @@ false, InvalidArgumentException::class ], - [ - 'foobar', - null, - false, - InvalidArgumentException::class - ] ]; } -- To view, visit https://gerrit.wikimedia.org/r/404975 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Idc984d31e27d61bc625663c9db1c1a804fe385ef Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/WikibaseQuality Gerrit-Branch: master Gerrit-Owner: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de> Gerrit-Reviewer: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de> Gerrit-Reviewer: Thiemo Kreuz (WMDE) <thiemo.kr...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits