From 6498e61543e7dd0aacf44e1a471a05dbca43b78e Mon Sep 17 00:00:00 2001 From: Leszek Manicki Date: Fri, 23 Jun 2017 10:11:51 +0200 Subject: [PATCH] Less strict array req in UnboundedQuantityValue::newFromArray (#105) Users of newFromArray give no guarantee that the value passed in is actually the array. DataValueObject::requireArrayFields asserts that the value is an array, and throws the exception that callers can handle. Having no strict array requirement lets callers handle erroneous input nicer. Other DataValue classes already seem to follow similar pattern. Strict array requirement was related to: https://phabricator.wikimedia.org/T168681 --- src/DataValues/UnboundedQuantityValue.php | 4 ++-- tests/DataValues/UnboundedQuantityValueTest.php | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/DataValues/UnboundedQuantityValue.php b/src/DataValues/UnboundedQuantityValue.php index cc4a67c..23a14b0 100644 --- a/src/DataValues/UnboundedQuantityValue.php +++ b/src/DataValues/UnboundedQuantityValue.php @@ -251,12 +251,12 @@ public function getArrayValue() { * depending on the serialization provided. Required for @see DataValueDeserializer. This can * round-trip with both @see self::getArrayValue as well as @see QuantityValue::getArrayValue. * - * @param array $data + * @param mixed $data * * @return self|QuantityValue Either an unbounded or bounded quantity value object. * @throws IllegalValueException */ - public static function newFromArray( array $data ) { + public static function newFromArray( $data ) { self::requireArrayFields( $data, [ 'amount', 'unit' ] ); if ( !isset( $data['upperBound'] ) && !isset( $data['lowerBound'] ) ) { diff --git a/tests/DataValues/UnboundedQuantityValueTest.php b/tests/DataValues/UnboundedQuantityValueTest.php index 37e7f65..1a8cd61 100644 --- a/tests/DataValues/UnboundedQuantityValueTest.php +++ b/tests/DataValues/UnboundedQuantityValueTest.php @@ -156,6 +156,15 @@ public function testNewFromArray_failure( $data ) { public function invalidArraySerializationProvider() { return [ + 'not an array (string)' => [ + 'foo' + ], + 'not an array (int)' => [ + 303 + ], + 'not an array (object)' => [ + new \stdClass() + ], 'no-amount' => [ [ 'unit' => '1',