Skip to content

Commit

Permalink
Less strict array req in UnboundedQuantityValue::newFromArray (#105)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
manicki authored and thiemowmde committed Jun 23, 2017
1 parent c84e181 commit 6498e61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/DataValues/UnboundedQuantityValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
9 changes: 9 additions & 0 deletions tests/DataValues/UnboundedQuantityValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 6498e61

Please sign in to comment.