10000 bug #19811 Fixed the nullable support for php 7.1 and below (2.7, 2.8… · symfony/symfony@ce73768 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce73768

Browse files
committed
bug #19811 Fixed the nullable support for php 7.1 and below (2.7, 2.8, 3.0) (iltar)
This PR was merged into the 2.7 branch. Discussion ---------- Fixed the nullable support for php 7.1 and below (2.7, 2.8, 3.0) | Q | A | ------------- | --- | Branch? | 2.7, 2.8, 3.0 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19784 (comment) | License | MIT | Doc PR | ~ Fixes the nullable support for 2.7, 2.8 and 3.0, can probably be partially merged into 3.1 but not 100% sure. /ping @fabpot Commits ------- 9c48756 Fixed the nullable support for php 7.1 and below
2 parents bc850b6 + 9c48756 commit ce73768

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class ControllerResolver implements ControllerResolverInterface
2727
{
2828
private $logger;
2929

30+
/**
31+
* If the ...$arg functionality is available.
32+
*
33+
* Requires at least PHP 5.6.0 or HHVM 3.9.1
34+
*
35+
* @var bool
36+
*/
37+
private $supportsVariadic;
38+
3039
/**
3140
* Constructor.
3241
*
@@ -35,6 +44,8 @@ class ControllerResolver implements ControllerResolverInterface
3544
public function __construct(LoggerInterface $logger = null)
3645
{
3746
$this->logger = $logger;
47+
48+
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
3849
}
3950

4051
/**
@@ -99,13 +110,20 @@ public function getArguments(Request $request, $controller)
99110
return $this->doGetArguments($request, $controller, $r->getParameters());
100111
}
101112

113+
/**
114+
* @param Request $request
115+
* @param callable $controller
116+
* @param \ReflectionParameter[] $parameters
117+
*
118+
* @return array The arguments to use when calling the action
119+
*/
102120
protected function doGetArguments(Request $request, $controller, array $parameters)
103121
{
104122
$attributes = $request->attributes->all();
105123
$arguments = array();
106124
foreach ($parameters as $param) {
107125
if (array_key_exists($param->name, $attributes)) {
108-
if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
126+
if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
109127
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
110128
} else {
111129
$arguments[] = $attributes[$param->name];
@@ -114,6 +132,8 @@ protected function doGetArguments(Request $request, $controller, array $paramete
114132
$arguments[] = $request;
115133
} elseif ($param->isDefaultValueAvailable()) {
116134
$arguments[] = $param->getDefaultValue();
135+
} elseif ($param->allowsNull()) {
136+
$arguments[] = null;
117137
} else {
118138
if (is_array($controller)) {
119139
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);

src/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
16+
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
1617
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
1718
use Symfony\Component\HttpFoundation\Request;
1819

@@ -222,6 +223,34 @@ public function testCreateControllerCanReturnAnyCallable()
222223
$mock->getController($request);
223224
}
224225

226+
/**
227+
* @requires PHP 7.1
228+
*/
229+
public function testGetNullableArguments()
230+
{
231+
$resolver = new ControllerResolver();
232+
233+
$request = Request::create('/');
234+
$request->attributes->set('foo', 'foo');
235+
$request->attributes->set('bar', new \stdClass());
236+
$request->attributes->set('mandatory', 'mandatory');
237+
$controller = array(new NullableController(), 'action');
238+
$this->assertEquals(array('foo', new \stdClass(), 'value', 'mandatory'), $resolver->getArguments($request, $controller));
239+
}
240+
241+
/**
242+
* @requires PHP 7.1
243+
*/
244+
public function testGetNullableArgumentsWithDefaults()
245+
{
246+
$resolver = new ControllerResolver();
247+
248+
$request = Request::create('/');
249+
$request->attributes->set('mandatory', 'mandatory');
250+
$controller = array(new NullableController(), 'action');
251+
$this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller));
252+
}
253+
225254
protected function createControllerResolver(LoggerInterface $logger = null)
226255
{
227256
return new ControllerResolver($logger);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\HttpKernel\Tests\Fixtures\Controller;
13+
14+
class NullableController
15+
{
16+
public function action(?string $foo, ?\stdClass $bar, ?string $baz = 'value', $mandatory)
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)
0