bug #38041 [PropertyInfo] Fix typed collections in PHP 7.4 (ndench)

This PR was merged into the 4.4 branch.

Discussion
----------

[PropertyInfo] Fix typed collections in PHP 7.4

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | No ticket
| License       | MIT
| Doc PR        | N/A

#37971 introduced support for typed properties in PHP 7.4, however by short circuiting the `getTypes()` method, typed collections are returned as `Type::BUILTIN_TYPE_ARRAY` without a proper collection type. If running on PHP < 7.4, the `getMutator()` method would be called which would extract the collection type from the getter/setter or adder/remover.

I updated the typedPropertiesTest to cover this case.

Commits
-------

282ed2850c [PropertyInfo] Fix typed collections in PHP 7.4
This commit is contained in:
Fabien Potencier 2020-09-04 07:32:57 +02:00
commit 4482fcf28d
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
{
}
}