[PropertyInfo] Fix typed collections in PHP 7.4

This commit is contained in:
Nathan Dench 2020-09-03 10:26:19 +10:00
parent 6c2a1c9a57
commit 282ed2850c
3 changed files with 23 additions and 12 deletions

View File

@ -139,18 +139,6 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
public function getTypes($class, $property, array $context = []): ?array
{
if (\PHP_VERSION_ID >= 70400) {
try {
$reflectionProperty = new \ReflectionProperty($class, $property);
$type = $reflectionProperty->getType();
if (null !== $type) {
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
}
} catch (\ReflectionException $e) {
// noop
}
}
if ($fromMutator = $this->extractFromMutator($class, $property)) {
return $fromMutator;
}
@ -170,6 +158,18 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
return $fromDefaultValue;
}
if (\PHP_VERSION_ID >= 70400) {
try {
$reflectionProperty = new \ReflectionProperty($class, $property);
$type = $reflectionProperty->getType();
if (null !== $type) {
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
}
} catch (\ReflectionException $e) {
// noop
}
}
return null;
}

View File

@ -398,5 +398,6 @@ class ReflectionExtractorTest extends TestCase
{
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], $this->extractor->getTypes(Php74Dummy::class, 'dummy'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_BOOL, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableBoolProp'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))], $this->extractor->getTypes(Php74Dummy::class, 'stringCollection'));
}
}

View File

@ -18,4 +18,14 @@ class Php74Dummy
{
public Dummy $dummy;
private ?bool $nullableBoolProp;
/** @var string[] */
private array $stringCollection;
public function addStringCollection(string $string): void
{
}
public function removeStringCollection(string $string): void
{
}
}