Skip to content

Commit 40bf9ba

Browse files
committed
Better type safety for typeOf return value
1 parent e038d6a commit 40bf9ba

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/viewmodel/ViewModelRenderer.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ private function conditionalApply(DOMElement $context, object|iterable $model):
386386
}
387387

388388
$typeOf = $current->typeOf();
389+
if (!is_string($typeOf)) {
390+
throw new ViewModelRendererException(
391+
sprintf('Value returned by typeOf() must be string (%s)', $this->getModelPath($context)),
392+
ViewModelRendererException::WrongTypeForTypeOf
393+
);
394+
}
389395

390396
$matches = $this->xp->query(
391397
sprintf(
@@ -402,7 +408,7 @@ private function conditionalApply(DOMElement $context, object|iterable $model):
402408
$context,
403409
$current,
404410
'typeOf',
405-
sprintf('No matching types for "%s" found', $typeOf)
411+
sprintf('No matching types for "%s" found', (string)$typeOf)
406412
),
407413
ViewModelRendererException::NoMatch
408414
);
@@ -555,6 +561,7 @@ private function objectApply(DOMElement $context, object $model): void {
555561
continue;
556562
}
557563

564+
/** @psalm-suppress MixedAssignment */
558565
$result = match (true) {
559566
// method variants
560567
method_exists($model, $name) => $model->{$name}($attribute->nodeValue),

tests/viewmodel/ViewModelRendererTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ public function typeOf() {
731731
$renderer = new ViewModelRenderer();
732732

733733
$this->expectException(ViewModelRendererException::class);
734-
$this->expectExceptionCode(ViewModelRendererException::NoMatch);
734+
$this->expectExceptionCode(ViewModelRendererException::WrongTypeForTypeOf);
735735
$renderer->render($dom->documentElement, $class);
736736
}
737737

0 commit comments

Comments
 (0)