Skip to content

Commit

Permalink
bug #38041 [PropertyInfo] Fix typed collections in PHP 7.4 (ndench)
Browse files Browse the repository at this point in the history
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
-------

282ed28 [PropertyInfo] Fix typed collections in PHP 7.4
  • Loading branch information
fabpot committed Sep 4, 2020
2 parents 067dc2d + 282ed28 commit 4482fcf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Expand Up @@ -139,18 +139,6 @@ public function getProperties($class, array $context = []): ?array
*/
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;
}
Expand All @@ -170,6 +158,18 @@ public function getTypes($class, $property, array $context = []): ?array
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;
}

Expand Down
Expand Up @@ -398,5 +398,6 @@ public function testTypedProperties(): void
{
$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'));
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php74Dummy.php
Expand Up @@ -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
{
}
}

0 comments on commit 4482fcf

Please sign in to comment.