Skip to content

Commit e6dfa4c

Browse files
committed
Cover missing line in ArrayDeserializer
1 parent 420ef32 commit e6dfa4c

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

src/ArrayDeserializer.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,36 +203,49 @@ protected function parseNamedType(
203203
string $name
204204
): mixed
205205
{
206+
$propertyPath = $path . "." . $name;
207+
206208
if ($type->isBuiltin()) {
207-
$valid = match ($type->getName()) {
208-
"bool" => is_bool($value),
209-
"int" => is_int($value),
210-
"float" => is_float($value) || is_int($value),
211-
"string" => is_string($value),
212-
"mixed" => true,
213-
"array" => is_array($value),
214-
"object" => is_object($value),
215-
"false" => $value === false,
216-
"true" => $value === true,
217-
default => throw new UnsupportedTypeException($path . "." . $name, $type->getName()),
218-
};
219-
220-
if (!$valid) {
221-
throw new IncorrectTypeException($path . "." . $name, $type->getName(), $value);
209+
if (!$this->isBuiltInTypeValid($type->getName(), $value, $propertyPath)) {
210+
throw new IncorrectTypeException($propertyPath, $type->getName(), $value);
222211
}
223212

224213
return $value;
225214
}
226215

227216
if (!is_array($value)) {
228-
throw new IncorrectTypeException($path . "." . $name, $type->getName(), $value);
217+
throw new IncorrectTypeException($propertyPath, $type->getName(), $value);
229218
}
230219

231220
if ($type->getName() === "self") {
232221
$deserializer = $this;
233222
} else {
234223
$deserializer = new static($type->getName());
235224
}
236-
return $deserializer->deserialize($value, $path . "." . $name);
225+
return $deserializer->deserialize($value, $propertyPath);
226+
}
227+
228+
/**
229+
* Check if a built-in type is valid
230+
* @param string $type the type to check
231+
* @param mixed $value the value to check
232+
* @param string $path the path to the data in the base data (used for error messages)
233+
* @return bool if the type is valid
234+
* @throws UnsupportedTypeException if the type of the property is unsupported
235+
*/
236+
protected function isBuiltInTypeValid(string $type, mixed $value, string $path): bool
237+
{
238+
return match ($type) {
239+
"bool" => is_bool($value),
240+
"int" => is_int($value),
241+
"float" => is_float($value) || is_int($value),
242+
"string" => is_string($value),
243+
"mixed" => true,
244+
"array" => is_array($value),
245+
"object" => is_object($value),
246+
"false" => $value === false,
247+
"true" => $value === true,
248+
default => throw new UnsupportedTypeException($path, $type),
249+
};
237250
}
238251
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Aternos\Serializer\Test\Src;
4+
5+
use Aternos\Serializer\ArrayDeserializer;
6+
7+
class ArrayDeserializerAccessor extends ArrayDeserializer
8+
{
9+
public function isBuiltInTypeValid(string $type, mixed $value, string $path): bool
10+
{
11+
return parent::isBuiltInTypeValid($type, $value, $path);
12+
}
13+
}

tests/tests/DeserializerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Aternos\Serializer\Exceptions\MissingPropertyException;
88
use Aternos\Serializer\Exceptions\UnsupportedTypeException;
99
use Aternos\Serializer\Serialize;
10+
use Aternos\Serializer\Test\Src\ArrayDeserializerAccessor;
1011
use Aternos\Serializer\Test\Src\ArrayTests;
1112
use Aternos\Serializer\Test\Src\BuiltInTypeTestClass;
1213
use Aternos\Serializer\Test\Src\DefaultValueTestClass;
@@ -471,4 +472,12 @@ public function testDeserializeTypedArrayPreservesKeys(): void
471472
$expected->int = 1;
472473
$this->assertEquals(["test" => $expected], $testClass->typedArray);
473474
}
475+
476+
public function testUnknownBuiltInType(): void
477+
{
478+
$deserializer = new ArrayDeserializerAccessor(TestClass::class);
479+
$this->expectException(UnsupportedTypeException::class);
480+
$this->expectExceptionMessage("Unsupported type 'not-a-real-type' for property '.name'");
481+
$deserializer->isBuiltInTypeValid("not-a-real-type", "test", ".name");
482+
}
474483
}

0 commit comments

Comments
 (0)