Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework ...List constructors #416

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ class Reference implements Hashable, Comparable, Immutable, Countable {
/**
* An array of Snak objects is only supported since version 1.1.
*
* @param Snak[]|SnakList $snaks
* @param Snak[]|SnakList|Snak $snaks
* @param Snak [$snak2,...]
*
* @throws InvalidArgumentException
*/
public function __construct( $snaks = [] ) {
public function __construct( $snaks = [] /*...*/ ) {
if ( $snaks instanceof Snak ) {
$snaks = func_get_args();
}

if ( is_array( $snaks ) ) {
$snaks = new SnakList( $snaks );
}
Expand Down
9 changes: 7 additions & 2 deletions src/ReferenceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ class ReferenceList implements Comparable, Countable, IteratorAggregate, Seriali
private $references = [];

/**
* @param Reference[]|Traversable $references
* @param Reference[]|Traversable|Reference $references
* @param Reference [$reference2,...]
*
* @throws InvalidArgumentException
*/
public function __construct( $references = [] ) {
public function __construct( $references = [] /*...*/ ) {
if ( $references instanceof Reference ) {
$references = func_get_args();
}

if ( !is_array( $references ) && !( $references instanceof Traversable ) ) {
throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' );
}
Expand Down
22 changes: 22 additions & 0 deletions src/Snak/SnakList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wikibase\DataModel\Snak;

use InvalidArgumentException;
use Traversable;
use Wikibase\DataModel\HashArray;
use Wikibase\DataModel\Internal\MapValueHasher;

Expand All @@ -17,6 +19,26 @@
*/
class SnakList extends HashArray {

/**
* @param Snak[]|Traversable|Snak $snaks
* @param Snak [$snak2,...]
*
* @throws InvalidArgumentException
*/
public function __construct( $snaks = array() /*...*/ ) {
if ( $snaks instanceof Snak ) {
$snaks = func_get_args();
}

if ( !is_array( $snaks ) && !( $snaks instanceof Traversable ) ) {
throw new InvalidArgumentException( '$snaks must be an array or an instance of Traversable' );
}

foreach ( $snaks as $index => $snak ) {
$this->setElement( $index, $snak );
}
}

/**
* @see GenericArrayObject::getObjectType
*
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/HashArray/HashArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function instanceProvider() {
$instances = [];

foreach ( $this->constructorProvider() as $args ) {
$instances[] = [ new $class( array_key_exists( 0, $args ) ? $args[0] : null ) ];
$instances[] = [ new $class( array_key_exists( 0, $args ) ? $args[0] : [] ) ];
}

return $instances;
Expand Down
1 change: 0 additions & 1 deletion tests/unit/ReferenceListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function invalidConstructorArgumentsProvider() {
[ 'string' ],
[ $id1 ],
[ new PropertyNoValueSnak( $id1 ) ],
[ new Reference() ],
[ new SnakList( [ new PropertyNoValueSnak( $id1 ) ] ) ],
[ [ new PropertyNoValueSnak( $id1 ) ] ],
[ [ new ReferenceList() ] ],
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public function invalidConstructorArgumentsProvider() {
[ 0.1 ],
[ 'string' ],
[ $id1 ],
[ new PropertyNoValueSnak( $id1 ) ],
[ new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ],
[ [ new Reference() ] ],
[ [ new SnakList() ] ],
[ new Reference() ],
];
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Snak/SnakListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ public function invalidConstructorArgumentsProvider() {
$id1 = new PropertyId( 'P1' );

return [
[ null ],
[ false ],
[ 1 ],
[ 0.1 ],
[ 'string' ],
[ $id1 ],
[ new PropertyNoValueSnak( $id1 ) ],
[ new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ],
[ [ null ] ],
[ [ $id1 ] ],
[ [ new SnakList() ] ],
Expand Down
16 changes: 4 additions & 12 deletions tests/unit/Statement/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ public function testGivenSameStatement_equalsReturnsTrue() {
new SnakList( [
new PropertyNoValueSnak( 1337 ),
] ),
new ReferenceList( [
new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
] )
new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) )
);

$statement->setGuid( 'kittens' );
Expand Down Expand Up @@ -347,17 +345,13 @@ public function testStatementWithDifferentReferences_equalsReturnsFalse() {
$statement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList(),
new ReferenceList( [
new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
] )
new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) )
);

$differentStatement = new Statement(
new PropertyNoValueSnak( 42 ),
new SnakList(),
new ReferenceList( [
new Reference( [ new PropertyNoValueSnak( 32202 ) ] ),
] )
new ReferenceList( new Reference( new PropertyNoValueSnak( 32202 ) ) )
);

$this->assertFalse( $statement->equals( $differentStatement ) );
Expand Down Expand Up @@ -406,9 +400,7 @@ private function newStatement() {
$statement = new Statement(
new PropertyNoValueSnak( 42 ),
$qualifiers,
new ReferenceList( [
new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
] )
new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) )
);

$statement->setRank( Statement::RANK_NORMAL );
Expand Down