8000 bug #37971 [PropertyInfo] Backport support for typed properties (PHP … · symfony/symfony@03d494d · GitHub
[go: up one dir, main page]

Skip to content

Commit 03d494d

Browse files
committed
bug #37971 [PropertyInfo] Backport support for typed properties (PHP 7.4) (dunglas)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] Backport support for typed properties (PHP 7.4) | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A. <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#13719 to be fixed Backports #34557 as suggested. One of my workmate hit this issue with the Serializer component and it was not obvious for him why it was not working, without hint. Commits ------- 1b19f25 [PropertyInfo] Backport support for typed properties (PHP 7.4)
2 parents ac96fda + 1b19f25 commit 03d494d

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ public function getProperties($class, array $context = []): ?array
139139
*/
140140
public function getTypes($class, $property, array $context = []): ?array
141141
{
142+
if (\PHP_VERSION_ID >= 70400) {
143+
try {
144+
$reflectionProperty = new \ReflectionProperty($class, $property);
145+
$type = $reflectionProperty->getType();
146+
if (null !== $type) {
147+
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
148+
}
149+
} catch (\ReflectionException $e) {
150+
// noop
151+
}
152+
}
153+
142154
if ($fromMutator = $this->extractFromMutator($class, $property)) {
143155
return $fromMutator;
144156
}
@@ -233,7 +245,7 @@ private function extractFromMutator(string $class, string $property): ?array
233245
if (!$reflectionType = $reflectionParameter->getType()) {
234246
return null;
235247
}
236-
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod);
248+
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod->getDeclaringClass());
237249

238250
if (1 === \count($type) && \in_array($prefix, $this->arrayMutatorPrefixes)) {
239251
$type = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
@@ -255,7 +267,7 @@ private function extractFromAccessor(string $class, string $property): ?array
255267
}
256268

257269
if ($reflectionType = $reflectionMethod->getReturnType()) {
258-
return $this->extractFromReflectionType($reflectionType, $reflectionMethod);
270+
return $this->extractFromReflectionType($reflectionType, $reflectionMethod->getDeclaringClass());
259271
}
260272

261273
if (\in_array($prefix, ['is', 'can', 'has'])) {
@@ -290,7 +302,7 @@ private function extractFromConstructor(string $class, string $property): ?array
290302
}
291303
$reflectionType = $parameter->getType();
292304

293-
return $reflectionType ? $this->extractFromReflectionType($reflectionType, $constructor) : null;
305+
return $reflectionType ? $this->extractFromReflectionType($reflectionType, $constructor->getDeclaringClass()) : null;
294306
}
295307

296308
if ($parentClass = $reflectionClass->getParentClass()) {
@@ -319,7 +331,7 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
319331
return [new Type(static::MAP_TYPES[$type] ?? $type)];
320332
}
321333

322-
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod): array
334+
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionClass $declaringClass): array
323335
{
324336
$types = [];
325337
$nullable = $reflectionType->allowsNull();
@@ -337,19 +349,19 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
337349
} elseif ($type->isBuiltin()) {
338350
$types[] = new Type($phpTypeOrClass, $nullable);
339351
} else {
340-
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));
352+
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $declaringClass));
341353
}
342354
}
343355

344356
return $types;
345357
}
346358

347-
private function resolveTypeName(string $name, \ReflectionMethod $reflectionMethod): string
359+
private function resolveTypeName(string $name, \ReflectionClass $declaringClass): string
348360
{
349361
if ('self' === $lcName = strtolower($name)) {
350-
return $reflectionMethod->getDeclaringClass()->name;
362+
return $declaringClass->name;
351363
}
352-
if ('parent' === $lcName && $parent = $reflectionMethod->getDeclaringClass()->getParentClass()) {
364+
if ('parent' === $lcName && $parent = $declaringClass->getParentClass()) {
353365
return $parent->name;
354366
}
355367

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\PropertyInfo\Tests\Fixtures\NotInstantiable;
2020
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
2121
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
22+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php74Dummy;
2223
use Symfony\Component\PropertyInfo\Type;
2324

2425
/**
@@ -389,4 +390,13 @@ public function constructorTypesProvider(): array
389390
[DefaultValue::class, 'foo', null],
390391
];
391392
}
393+
394+
/**
395+
* @requires PHP 7.4
396+
*/
397+
public function testTypedProperties(): void
398+
{
399+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], $this->extractor->getTypes(Php74Dummy::class, 'dummy'));
400+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_BOOL, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableBoolProp'));
401+
}
392402
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
13+
14+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class Php74Dummy
18+
{
19+
public Dummy $dummy;
20+
private ?bool $nullableBoolProp;
21+
}

0 commit comments

Comments
 (0)
0