8000 [HttpKernel][PropertyInfo] Fix nullable types handling · symfony/symfony@bdcceea · GitHub
[go: up one dir, main page]

Skip to content
< 8000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_remote-form_dist_index_js-node_modules_delegated-events_dist_inde-94fd67-99b04cc350b5.js" defer="defer">

Commit bdcceea

Browse files
[HttpKernel][PropertyInfo] Fix nullable types handling
1 parent 70a620d commit bdcceea

File tree

5 files changed

+15
-30
lines changed

5 files changed

+15
-30
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/DefaultValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class DefaultValueResolver implements ArgumentValueResolverInterface
2727
*/
2828
public function supports(Request $request, ArgumentMetadata $argument)
2929
{
30-
return $argument->hasDefaultValue() || $argument->isNullable();
30+
return $argument->hasDefaultValue() || ($argument->isNullable() && !$argument->isVariadic());
3131
}
3232

3333
/**

src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct($name, $type, $isVariadic, $hasDefaultValue, $defaul
4040
$this->isVariadic = $isVariadic;
4141
$this->hasDefaultValue = $hasDefaultValue;
4242
$this->defaultValue = $defaultValue;
43-
$this->isNullable = (bool) $isNullable;
43+
$this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
4444
}
4545

4646
/**
@@ -88,7 +88,7 @@ public function hasDefaultValue()
8888
}
8989

9090
/**
91-
* Returns whether the argument is nullable in PHP 7.1 or higher.
91+
* Returns whether the argument accepts null values.
9292
*
9393
* @return bool
9494
*/

src/Symfony/Component/HttpKernel/ControllerMetadata/ArgumentMetadataFactory.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function createArgumentMetadata($controller)
5858
}
5959

6060
foreach ($reflection->getParameters() as $param) {
61-
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $this->isNullable($param));
61+
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
6262
}
6363

6464
return $arguments;
@@ -88,23 +88,6 @@ private function hasDefaultValue(\ReflectionParameter $parameter)
8888
return $parameter->isDefaultValueAvailable();
8989
}
9090

91-
/**
92-
* Returns if the argument is allowed to be null but is still mandatory.
93-
*
94-
* @param \ReflectionParameter $parameter
95-
*
96-
* @return bool
97-
*/
98-
private function isNullable(\ReflectionParameter $parameter)
99-
{
100-
if ($this->supportsParameterType) {
101-
return null !== ($type = $parameter->getType()) && $type->allowsNull();
102-
}
103-
104-
// fallback for supported php 5.x versions
105-
return $this->hasDefaultValue($parameter) && null === $this->getDefaultValue($parameter);
106-
}
107-
10891
/**
10992
* Returns a default value if available.
11093
*
@@ -127,7 +110,11 @@ private function getDefaultValue(\ReflectionParameter $parameter)
127110
private function getType(\ReflectionParameter $parameter)
128111
{
129112
if ($this->supportsParameterType) {
130-
return $parameter->hasType() ? (string) $parameter->getType() : null;
113+
if (!$type = $parameter->getType()) {
114+
return;
115+
}
116+
117+
return $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
131118
}
132119

133120
if ($parameter->isArray()) {

src/Symfony/Component/HttpKernel/Tests/ControllerMetadata/ArgumentMetadataFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testNullableTypesSignature()
121121
new ArgumentMetadata('foo', 'string', false, false, null, true),
122122
new ArgumentMetadata('bar', \stdClass::class, false, false, null, true),
123123
new ArgumentMetadata('baz', 'string', false, true, 'value', true),
124-
new ArgumentMetadata('mandatory', null, false, false, null),
124+
new ArgumentMetadata('mandatory', null, false, false, null, true),
125125
), $arguments);
126126
}
127127

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,13 @@ private function extractFromAccessor($class, $property)
231231
*/
232232
private function extractFromReflectionType(\ReflectionType $reflectionType)
233233
{
234-
$phpTypeOrClass = method_exists($reflectionType, 'getName') ? $reflectionType->getName() : (string) $reflectionType;
234+
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : $reflectionType->__toString();
235235
$nullable = $reflectionType->allowsNull();
236236

237-
if ($reflectionType->isBuiltin()) {
238-
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
239-
$type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
240-
} else {
241-
$type = new Type($phpTypeOrClass, $nullable);
242-
}
237+
if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
238+
$type = new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true);
239+
} elseif ($reflectionType->isBuiltin()) {
240+
$type = new Type($phpTypeOrClass, $nullable);
243241
} else {
244242
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass);
245243
}

0 commit comments

Comments
 (0)
0