Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0d4d12

Browse files
[PropertyInfo] Combine default value logic with property type
1 parent 0ed6766 commit a0d4d12

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,8 @@ public function getTypes($class, $property, array $context = []): ?array
154154
return $fromConstructor;
155155
}
156156

157-
if ($fromDefaultValue = $this->extractFromDefaultValue($class, $property)) {
158-
return $fromDefaultValue;
159-
}
160-
161-
if (\PHP_VERSION_ID >= 70400) {
162-
try {
163-
$reflectionProperty = new \ReflectionProperty($class, $property);
164-
$type = $reflectionProperty->getType();
165-
if (null !== $type && $types = $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass())) {
166-
return $types;
167-
}
168-
} catch (\ReflectionException $e) {
169-
// noop
170-
}
157+
if ($fromPropertyDeclaration = $this->extractFromPropertyDeclaration($class, $property)) {
158+
return $fromPropertyDeclaration;
171159
}
172160

173161
return null;
@@ -312,17 +300,18 @@ private function extractFromConstructor(string $class, string $property): ?array
312300
return null;
313301
}
314302

315-
private function extractFromDefaultValue(string $class, string $property): ?array
303+
private function extractFromPropertyDeclaration(string $class, string $property): ?array
316304
{
317-
$isNullable = false;
318-
319305
try {
320306
$reflectionClass = new \ReflectionClass($class);
321307

322308
if (\PHP_VERSION_ID >= 70400) {
323-
$type = $reflectionClass->getProperty($property)->getType();
309+
$reflectionProperty = $reflectionClass->getProperty($property);
310+
$reflectionPropertyType = $reflectionProperty->getType();
324311

325-
$isNullable = null !== $type && $type->allowsNull();
312+
if (null !== $reflectionPropertyType && $types = $this->extractFromReflectionType($reflectionPropertyType, $reflectionProperty->getDeclaringClass())) {
313+
return $types;
314+
}
326315
}
327316
} catch (\ReflectionException $e) {
328317
return null;
@@ -336,7 +325,7 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
336325

337326
$type = \gettype($defaultValue);
338327

339-
return [new Type(static::MAP_TYPES[$type] ?? $type, $isNullable)];
328+
return [new Type(static::MAP_TYPES[$type] ?? $type, $this->isNullableProperty($class, $property))];
340329
}
341330

342331
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionClass $declaringClass): array
@@ -376,6 +365,25 @@ private function resolveTypeName(string $name, \ReflectionClass $declaringClass)
376365
return $name;
377366
}
378367

368+
private function isNullableProperty(string $class, string $property): bool
369+
{
370+
try {
371+
$reflectionProperty = new \ReflectionProperty($class, $property);
372+
373+
if (\PHP_VERSION_ID >= 70400) {
374+
$reflectionPropertyType = $reflectionProperty->getType();
375+
376+
return null !== $reflectionPropertyType && $reflectionPropertyType->allowsNull();
377+
}
378+
379+
return false;
380+
} catch (\ReflectionException $e) {
381+
// Return false if the property doesn't exist
382+
}
383+
384+
return false;
385+
}
386+
379387
private function isAllowedProperty(string $class, string $property): bool
380388
{
381389
try {

0 commit comments

Comments
 (0)