Skip to content

Commit

Permalink
feature: consistent type safety within arrays
Browse files Browse the repository at this point in the history
closes #49
  • Loading branch information
g105b committed Feb 14, 2025
1 parent 299bb5b commit e85a696
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public function asObject():object {
}

/**
* @param array $array
* @return array
* @param array<mixed> $array
* @return array<mixed>
*/
private function checkArrayType(array $array, string $type):array {
$errorMessage = "";
Expand Down
11 changes: 5 additions & 6 deletions test/phpunit/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public function testGetArrayFixedTypeString() {
}
}

public function testGetArrayFixedTypeString_typeErrorWithObject() {
public function testGetArrayFixedTypeString_typeErrorWithDateTime() {
$wordsArray = [
"one",
"two",
Expand All @@ -465,7 +465,7 @@ public function testGetArrayFixedTypeString_typeErrorWithObject() {
];
$sut = (new DataObject())
->with("words", $wordsArray);
self::expectExceptionMessage("Array index 3 must be of type string, DateTime given");
self::expectExceptionMessage("Object of class DateTime could not be converted to string");
$sut->getArray("words", "string");
}

Expand All @@ -478,7 +478,7 @@ public function testGetArrayFixedTypeDateTime() {
->with("dates", $dateArray);
$array = $sut->getArray("dates", DateTimeInterface::class);
foreach($array as $i => $value) {
self::assertInstanceOf(DateTimeInterface::class, $value);
self::assertInstanceOf(DateTime::class, $value);
}
}

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

public function testGetArray_nullableType():void {
Expand Down

0 comments on commit e85a696

Please sign in to comment.