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

Skip to content

Commit 649ca46

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

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

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

Lines changed: 28 additions & 18 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,20 @@ 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;
305+
$isNullable = $this->isNullableProperty($class, $property);
318306

319307
try {
320308
$reflectionClass = new \ReflectionClass($class);
321309

322310
if (\PHP_VERSION_ID >= 70400) {
323-
$type = $reflectionClass->getProperty($property)->getType();
311+
$reflectionProperty = $reflectionClass->getProperty($property);
312+
$reflectionPropertyType = $reflectionProperty->getType();
324313

325-
$isNullable = null !== $type && $type->allowsNull();
314+
if (null !== $reflectionPropertyType && $types = $this->extractFromReflectionType($reflectionPropertyType, $reflectionProperty->getDeclaringClass())) {
315+
return $types;
316+
}
326317
}
327318
} catch (\ReflectionException $e) {
328319
return null;
@@ -376,6 +367,25 @@ private function resolveTypeName(string $name, \ReflectionClass $declaringClass)
376367
return $name;
377368
}
378369

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

0 commit comments

Comments
 (0)