Skip to content

Commit 5ff8b49

Browse files
committed
Throw exception rather than ignoring non-iterable
Just ran into this while working on new term store stuff
1 parent 58116a3 commit 5ff8b49

6 files changed

Lines changed: 26 additions & 1 deletion

File tree

RELEASE-NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Wikibase DataModel release notes
22

3+
## Version 9.2.0 (dev)
4+
5+
* `TermList` now throws `InvalidArgumentException` when given non-iterable rather than failing silently
6+
* `SiteLinkList` now throws `InvalidArgumentException` when given non-iterable rather than failing silently
7+
38
## Version 9.1.0 (2019-01-24)
49

510
* Raised minimum PHP version to 7.0 or HHVM

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"extra": {
5151
"branch-alias": {
52-
"dev-master": "9.1.x-dev"
52+
"dev-master": "9.2.x-dev"
5353
}
5454
},
5555
"scripts": {

src/SiteLinkList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class SiteLinkList implements IteratorAggregate, Countable, Comparable {
3535
* @throws InvalidArgumentException
3636
*/
3737
public function __construct( /* iterable */ $siteLinks = [] ) {
38+
if ( !is_array( $siteLinks ) && !( $siteLinks instanceof \Traversable ) ) {
39+
throw new InvalidArgumentException( '$siteLinks must be iterable' );
40+
}
41+
3842
foreach ( $siteLinks as $siteLink ) {
3943
if ( !( $siteLink instanceof SiteLink ) ) {
4044
throw new InvalidArgumentException( 'Every element of $siteLinks must be an instance of SiteLink' );

src/Term/TermList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ public function clear() {
196196
* @throws InvalidArgumentException
197197
*/
198198
public function addAll( /* iterable */ $terms ) {
199+
if ( !is_array( $terms ) && !( $terms instanceof \Traversable ) ) {
200+
throw new InvalidArgumentException( '$terms must be iterable' );
201+
}
202+
199203
foreach ( $terms as $term ) {
200204
if ( !( $term instanceof Term ) ) {
201205
throw new InvalidArgumentException( 'Every element in $terms must be an instance of Term' );

tests/unit/SiteLinkListTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,9 @@ public function testCanConstructWithIterable() {
346346
);
347347
}
348348

349+
public function testWhenProvidingNonIterable_constructorThrowsException() {
350+
$this->setExpectedException( InvalidArgumentException::class );
351+
new SiteLinkList( new SiteLink( 'enwiki', 'foo' ) );
352+
}
353+
349354
}

tests/unit/Term/TermListTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,11 @@ public function testWhenProvidingNonTerms_addAllThrowsException() {
479479
$list->addAll( [ 'no-a-term' ] );
480480
}
481481

482+
public function testWhenProvidingNonIterable_addAllThrowsException() {
483+
$list = new TermList( [] );
484+
485+
$this->setExpectedException( InvalidArgumentException::class );
486+
$list->addAll( new Term( 'en', 'foo' ) );
487+
}
488+
482489
}

0 commit comments

Comments
 (0)