Repository: JsonMapper/JsonMapper (php)

Issue:
PropertyMapper cannot map with array in phpdoc
**Describe the bug**
I have a property like this:
```php```
    /** @var string|string[] */
    public $value;
```php```

The PropertyMapper in line 160 (v. 2.9.1) uses only the first match (in this case string) for scalar mapping.
If I have a valid empty array the PropertyMapper in line 225 calls the scalarcaster and throws an error.

I use "(new JsonMapperFactory())->bestFit()" with PHP 7.4


Pull request:
Return empty array for union type with an array type when value is an empty array
Will fix #120 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bf26f45..f838a9c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Fixed
 - Replace duplicates in middleware with object wrapper calls. [PR#123](https://github.com/JsonMapper/JsonMapper/pull/123)
 - Correct code style issues. [PR#124](https://github.com/JsonMapper/JsonMapper/pull/124)
+- Return empty array for union type with an array type when value is an empty array. [PR#125](https://github.com/JsonMapper/JsonMapper/pull/125) Thanks to [template-provider](https://github.com/template-provider) for reporting the issue
 
 ## [2.9.1] - 2021-11-12
 ### Fixed
diff --git a/src/Handler/PropertyMapper.php b/src/Handler/PropertyMapper.php
index 795b048..c9a750d 100644
--- a/src/Handler/PropertyMapper.php
+++ b/src/Handler/PropertyMapper.php
@@ -114,6 +114,10 @@ class PropertyMapper
         // For union types, loop through and see if value is a match with the type
         if (\count($property->getPropertyTypes()) > 1) {
             foreach ($property->getPropertyTypes() as $type) {
+                if (\is_array($value) && $type->isArray() && count($value) === 0) {
+                    return [];
+                }
+
                 if (\is_array($value) && $type->isArray()) {
                     $copy = $value;
                     $firstValue = \array_shift($copy);


Test patch:
diff --git a/tests/Integration/Regression/Bug120RegressionTest.php b/tests/Integration/Regression/Bug120RegressionTest.php
new file mode 100644
index 0000000..8d44cf5
--- /dev/null
+++ b/tests/Integration/Regression/Bug120RegressionTest.php
@@ -0,0 +1,31 @@
+<?php
+
+declare(strict_types=1);
+
+namespace JsonMapper\Tests\Integration\Regression;
+
+use JsonMapper\JsonMapperFactory;
+use PHPUnit\Framework\TestCase;
+
+class Bug120RegressionTest extends TestCase
+{
+    /**
+     * @test
+     * @coversNothing
+     */
+    public function jsonMapperEagerlyMapsToSingleScalarValueForUnionTypeWhereValueIsArray(): void
+    {
+        $mapper = (new JsonMapperFactory())->bestFit();
+        $object = new class {
+            /** @var string|string[] */
+            public $value;
+        };
+        $json = (object) [
+            'value' => []
+        ];
+
+        $mapper->mapObject($json, $object);
+
+        self::assertEquals($json->value, $object->value);
+    }
+}
diff --git a/tests/Unit/Handler/PropertyMapperTest.php b/tests/Unit/Handler/PropertyMapperTest.php
index 04f3498..cb3e77a 100644
--- a/tests/Unit/Handler/PropertyMapperTest.php
+++ b/tests/Unit/Handler/PropertyMapperTest.php
@@ -720,6 +720,31 @@ class PropertyMapperTest extends TestCase
         $propertyMapper->__invoke($json, $wrapped, $propertyMap, $jsonMapper);
     }
 
+    /**
+     * @covers \JsonMapper\Handler\PropertyMapper
+     */
+    public function testItCanMapAnEmptyArrayForArrayType(): void
+    {
+        $property = PropertyBuilder::new()
+            ->setName('value')
+            ->addType('string', false)
+            ->addType('string', true)
+            ->setIsNullable(false)
+            ->setVisibility(Visibility::PUBLIC())
+            ->build();
+        $propertyMap = new PropertyMap();
+        $propertyMap->addProperty($property);
+        $json = (object) ['value' => []];
+        $object = new \stdClass();
+        $wrapped = new ObjectWrapper($object);
+        $propertyMapper = new PropertyMapper();
+        $jsonMapper = (new JsonMapperFactory())->create($propertyMapper, new DocBlockAnnotations(new NullCache()));
+
+        $propertyMapper->__invoke($json, $wrapped, $propertyMap, $jsonMapper);
+
+        self::assertEquals([], $object->value);
+    }
+
     public function scalarValueDataTypes(): array
     {
         return [