[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQuality[master]: Use Wikimedia\Assert and type hints
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 =
[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQuality[master]: Use Wikimedia\Assert and type hints
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 ) ) { -