EventSaucePHP/ObjectHydrator

Simple array-types in @param don't work

Closed this issue · 0 comments

Example:

<?php

declare(strict_types=1);

use EventSauce\ObjectHydrator\PropertyCasters\CastListToType;
use EventSauce\ObjectHydrator\{ObjectMapperUsingReflection};

require_once __DIR__ . '/../vendor/autoload.php';

class Dummy {
  /**
   * @param string[] $test
   */
  public function __construct(
    #[CastListToType('string')] public array $test,
  ) {
  }
}

$json = <<<JSON
    {
        "test": ["France"]
    }
JSON;

$mapper = new ObjectMapperUsingReflection();
$dummy = $mapper->hydrateObject(
	Dummy::class,
	json_decode($json, true)
);

Throws an exception:

PHP Fatal error:  Uncaught ReflectionException: Class "string" does not exist in /XXX/vendor/eventsauce/object-hydrator/src/NaivePropertyTypeResolver.php:175

The handling for native types is only in CastListToType, but not in resolveFromConstructorDocComment(). That function also assumes that there is always exactly one space between the type and the name of the variable. When you align several @param-lines, this leads to most @param-lines being ignored.