Skip to content

Commit 38c92c7

Browse files
author
omasn
committed
Fix for readonly classes
1 parent c091bb1 commit 38c92c7

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/ObjectHandler.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@ public function instantiateObject(
108108
}
109109

110110
if (!$constructor->isPublic()) {
111+
if ($this->isReadonly($reflClass)) {
112+
throw new RuntimeException('invalid class');
113+
}
111114
return $reflClass->newInstanceWithoutConstructor();
112115
}
113116

114-
if (0 === $constructor->getNumberOfRequiredParameters()) {
115-
return new $class();
116-
}
117-
118117
if ([] !== $data && !ArrayHelper::isAssoc($data)) {
119118
throw new RuntimeException('invalid data');
120119
}
@@ -260,8 +259,14 @@ public function handle(
260259
HandleContextInterface $context = null,
261260
DefaultValueExtractorInterface $defaultValueExtractor = null
262261
): object {
262+
$reflClass = new ReflectionClass($class);
263263
$object = $this->instantiateObject($class, $data, $context, $defaultValueExtractor);
264-
$this->handleObject($object, $data, $context, $defaultValueExtractor);
264+
if ([] !== $data) {
265+
if ($this->isReadonly($reflClass)) {
266+
throw new RuntimeException('invalid class');
267+
}
268+
$this->handleObject($object, $data, $context, $defaultValueExtractor);
269+
}
265270

266271
return $object;
267272
}
@@ -327,4 +332,13 @@ private function getPropertyType(string $class, string $property): Type
327332

328333
return $types[0];
329334
}
335+
336+
private function isReadonly(\ReflectionClass $reflClass): bool
337+
{
338+
if (!method_exists($reflClass, 'isReadOnly')) {
339+
return false;
340+
}
341+
342+
return $reflClass->isReadOnly();
343+
}
330344
}

0 commit comments

Comments
 (0)