8000 bug #20152 [HttpKernel] Fix nullable types handling (nicolas-grekas) · symfony/symfony@6f8e5b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f8e5b0

Browse files
committed
bug #20152 [HttpKernel] Fix nullable types handling (nicolas-grekas)
This PR was merged into the 3.1 branch. Discussion ---------- [HttpKernel] Fix nullable types handling | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19784 | License | MIT | Doc PR | - Commits ------- 0884518 [HttpKernel] Fix nullable types handling
2 parents 810c1e9 + 0884518 commit 6f8e5b0

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
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: 11 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,16 @@ 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+
$typeName = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
117+
if ('array' === $typeName && !$type->isBuiltin()) {
118+
// Special case for HHVM with variadics
119+
return;
120+
}
121+
122+
return $typeName;
131123
}
132124

133125
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

0 commit comments

Comments
 (0)
0