Skip to content

Commit e85a696

Browse files
author
Greg Bowler
committed
feature: consistent type safety within arrays
closes #49
1 parent 299bb5b commit e85a696

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

src/DataObject.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public function asObject():object {
114114
}
115115

116116
/**
117-
* @param array $array
118-
* @return array
117+
* @param array<mixed> $array
118+
* @return array<mixed>
119119
*/
120120
private function checkArrayType(array $array, string $type):array {
121121
$errorMessage = "";

test/phpunit/DataObjectTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public function testGetArrayFixedTypeString() {
455455
}
456456
}
457457

458-
public function testGetArrayFixedTypeString_typeErrorWithObject() {
458+
public function testGetArrayFixedTypeString_typeErrorWithDateTime() {
459459
$wordsArray = [
460460
"one",
461461
"two",
@@ -465,7 +465,7 @@ public function testGetArrayFixedTypeString_typeErrorWithObject() {
465465
];
466466
$sut = (new DataObject())
467467
->with("words", $wordsArray);
468-
self::expectExceptionMessage("Array index 3 must be of type string, DateTime given");
468+
self::expectExceptionMessage("Object of class DateTime could not be converted to string");
469469
$sut->getArray("words", "string");
470470
}
471471

@@ -478,7 +478,7 @@ public function testGetArrayFixedTypeDateTime() {
478478
->with("dates", $dateArray);
479479
$array = $sut->getArray("dates", DateTimeInterface::class);
480480
foreach($array as $i => $value) {
481-
self::assertInstanceOf(DateTimeInterface::class, $value);
481+
self::assertInstanceOf(DateTime::class, $value);
482482
}
483483
}
484484

@@ -492,9 +492,8 @@ public function testGetArrayFixedTypesMismatch() {
492492
];
493493
$sut = (new DataObject())
494494
->with("timestamps", $timestampArray);
495-
self::expectException(TypeError::class);
496-
self::expectExceptionMessage("Array index 2 must be of type int, double given");
497-
$sut->getArray("timestamps", "int");
495+
$array = $sut->getArray("timestamps", "int");
496+
self::assertSame(49999, $array[2]);
498497
}
499498

500499
public function testGetArray_nullableType():void {

0 commit comments

Comments
 (0)