Skip to content

Commit ab1db44

Browse files
author
Daniel Kinzler
committed
Merge pull request #665 from wmde/objectById
Block ByPropertyIdArray construction from object properties
2 parents effa40e + 934c73f commit ab1db44

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/ByPropertyIdArray.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\DataModel;
44

55
use ArrayObject;
6+
use InvalidArgumentException;
67
use OutOfBoundsException;
78
use RuntimeException;
89
use Traversable;
@@ -51,11 +52,18 @@ class ByPropertyIdArray extends ArrayObject {
5152
private $byId = null;
5253

5354
/**
55+
* @deprecated since 5.0, use a DataModel Service instead
5456
* @see ArrayObject::__construct
5557
*
5658
* @param PropertyIdProvider[]|Traversable|null $input
59+
*
60+
* @throws InvalidArgumentException
5761
*/
5862
public function __construct( $input = null ) {
63+
if ( is_object( $input ) && !( $input instanceof Traversable ) ) {
64+
throw new InvalidArgumentException( '$input must be an array, Traversable or null' );
65+
}
66+
5967
parent::__construct( (array)$input );
6068
}
6169

@@ -411,6 +419,7 @@ public function moveObjectToIndex( $object, $toIndex ) {
411419
* @param PropertyIdProvider $object
412420
* @param int|null $index Absolute index where to place the new object.
413421
*
422+
* @throws OutOfBoundsException
414423
* @throws RuntimeException
415424
*/
416425
public function addObjectAtIndex( $object, $index = null ) {

tests/unit/ByPropertyIdArrayTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use ArrayObject;
66
use DataValues\StringValue;
7+
use PHPUnit_Framework_TestCase;
78
use ReflectionClass;
89
use ReflectionMethod;
10+
use stdClass;
911
use Wikibase\DataModel\ByPropertyIdArray;
1012
use Wikibase\DataModel\Entity\PropertyId;
1113
use Wikibase\DataModel\PropertyIdProvider;
@@ -26,7 +28,21 @@
2628
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2729
* @author H. Snater < mediawiki@snater.com >
2830
*/
29-
class ByPropertyIdArrayTest extends \PHPUnit_Framework_TestCase {
31+
class ByPropertyIdArrayTest extends PHPUnit_Framework_TestCase {
32+
33+
public function testGivenNull_constructorAssumesEmptyArray() {
34+
$indexedArray = new ByPropertyIdArray( null );
35+
36+
$this->assertSame( 0, $indexedArray->count() );
37+
}
38+
39+
public function testGivenNonTraversableObject_constructorDoesNotCastObjectToArray() {
40+
$object = new stdClass();
41+
$object->property = true;
42+
43+
$this->setExpectedException( 'InvalidArgumentException' );
44+
new ByPropertyIdArray( $object );
45+
}
3046

3147
public function testArrayObjectNotConstructedFromObject() {
3248
$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );

0 commit comments

Comments
 (0)