Skip to content

Commit ce1b925

Browse files
committed
Do not allow null in Reference[List] constructors
1 parent 028ed44 commit ce1b925

4 files changed

Lines changed: 14 additions & 22 deletions

File tree

src/Reference.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@ class Reference implements \Hashable, \Comparable, \Immutable, \Countable {
2424
private $snaks;
2525

2626
/**
27-
* An array of Snak is only supported since version 1.1.
27+
* An array of Snak objects is only supported since version 1.1.
2828
*
29-
* @param Snaks|Snak[]|null $snaks
29+
* @param Snak[]|Snaks $snaks
3030
* @throws InvalidArgumentException
3131
*/
32-
public function __construct( $snaks = null ) {
33-
if ( $snaks === null ) {
34-
$this->snaks = new SnakList();
32+
public function __construct( $snaks = array() ) {
33+
if ( is_array( $snaks ) ) {
34+
$snaks = new SnakList( $snaks );
3535
}
36-
elseif ( $snaks instanceof Snaks ) {
37-
$this->snaks = $snaks;
38-
}
39-
elseif ( is_array( $snaks ) ) {
40-
$this->snaks = new SnakList( $snaks );
41-
}
42-
else {
43-
throw new InvalidArgumentException( '$snaks must be an instance of Snaks, an array of instances of Snak, or null' );
36+
37+
if ( !( $snaks instanceof Snaks ) ) {
38+
throw new InvalidArgumentException( '$snaks must be an array or an instance of Snaks' );
4439
}
40+
41+
$this->snaks = $snaks;
4542
}
4643

4744
/**

src/ReferenceList.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@
2626
class ReferenceList extends HashableObjectStorage {
2727

2828
/**
29-
* @param Reference[]|Traversable|null $references
29+
* @param Reference[]|Traversable $references
3030
*
3131
* @throws InvalidArgumentException
3232
*/
33-
public function __construct( $references = null ) {
34-
if ( $references === null ) {
35-
return;
36-
}
37-
33+
public function __construct( $references = array() ) {
3834
if ( !is_array( $references ) && !( $references instanceof Traversable ) ) {
3935
throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' );
4036
}

tests/unit/ReferenceListTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function getElementInstances() {
4343

4444
public function getConstructorArg() {
4545
return array(
46-
null,
4746
array(),
4847
$this->getElementInstances(),
4948
);
@@ -61,7 +60,7 @@ public function invalidConstructorArgumentsProvider() {
6160
$id1 = new PropertyId( 'P1' );
6261

6362
return array(
64-
// TODO: Disallow array( null ),
63+
array( null ),
6564
array( false ),
6665
array( 1 ),
6766
array( 0.1 ),
@@ -96,7 +95,6 @@ public function testHasReferenceBeforeRemoveButNotAfter( ReferenceList $array )
9695
}
9796
}
9897

99-
10098
public function testGivenCloneOfReferenceInList_hasReferenceReturnsTrue() {
10199
$list = new ReferenceList();
102100

tests/unit/ReferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ public function invalidConstructorArgumentsProvider() {
258258
$id1 = new PropertyId( 'P1' );
259259

260260
return array(
261+
array( null ),
261262
array( false ),
262263
array( 1 ),
263264
array( 0.1 ),

0 commit comments

Comments
 (0)