Skip to content

Commit 0ba1fa4

Browse files
Add more explicit nullable types for default null values
1 parent cc1fb23 commit 0ba1fa4

File tree

8 files changed

+38
-18
lines changed

8 files changed

+38
-18
lines changed

Tests/Compiler/AutowirePassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2929
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
3030
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\MultipleArgumentsOptionalScalarNotReallyOptional;
31+
use Symfony\Component\DependencyInjection\Tests\Fixtures\OptionalParameter;
3132
use Symfony\Component\DependencyInjection\Tests\Fixtures\WithTarget;
3233
use Symfony\Component\DependencyInjection\TypedReference;
3334
use Symfony\Contracts\Service\Attribute\Required;
@@ -405,6 +406,9 @@ public function testResolveParameter()
405406
$this->assertEquals(Foo::class, $container->getDefinition('bar')->getArgument(0));
406407
}
407408

409+
/**
410+
* @group legacy
411+
*/
408412
public function testOptionalParameter()
409413
{
410414
$container = new ContainerBuilder();

Tests/Fixtures/Bar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class Bar implements BarInterface
1515
{
1616
public $quz;
1717

18-
public function __construct($quz = null, \NonExistent $nonExistent = null, BarInterface $decorated = null, array $foo = [], iterable $baz = [])
18+
public function __construct($quz = null, ?\NonExistent $nonExistent = null, ?BarInterface $decorated = null, array $foo = [], iterable $baz = [])
1919
{
2020
$this->quz = $quz;
2121
}
2222

23-
public static function create(\NonExistent $nonExistent = null, $factory = null)
23+
public static function create(?\NonExistent $nonExistent = null, $factory = null)
2424
{
2525
}
2626
}

Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function setFoosVariadic(Foo $foo, Foo ...$foos)
2020
$this->foo = $foo;
2121
}
2222

23-
public function setFoosOptional(Foo $foo, Foo $fooOptional = null)
23+
public function setFoosOptional(Foo $foo, ?Foo $fooOptional = null)
2424
{
2525
$this->foo = $foo;
2626
}

Tests/Fixtures/CheckTypeDeclarationsPass/BarOptionalArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BarOptionalArgument
66
{
77
public $foo;
88

9-
public function __construct(\stdClass $foo = null)
9+
public function __construct(?\stdClass $foo = null)
1010
{
1111
$this->foo = $foo;
1212
}

Tests/Fixtures/CheckTypeDeclarationsPass/Foo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static function createBar()
99
return new Bar(new \stdClass());
1010
}
1111

12-
public static function createBarArguments(\stdClass $stdClass, \stdClass $stdClassOptional = null)
12+
public static function createBarArguments(\stdClass $stdClass, ?\stdClass $stdClassOptional = null)
1313
{
1414
return new Bar($stdClass);
1515
}

Tests/Fixtures/OptionalParameter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\DependencyInjection\Tests\Fixtures;
13+
14+
use Symfony\Component\DependencyInjection\Tests\Compiler\A;
15+
use Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface;
16+
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
17+
18+
class OptionalParameter
19+
{
20+
public function __construct(?CollisionInterface $c = null, A $a, ?Foo $f = null)
21+
{
22+
}
23+
}

Tests/Fixtures/Prototype/Foo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#[When(env: 'dev')]
99
class Foo implements FooInterface, Sub\BarInterface
1010
{
11-
public function __construct($bar = null, iterable $foo = null, object $baz = null)
11+
public function __construct($bar = null, ?iterable $foo = null, ?object $baz = null)
1212
{
1313
}
1414

Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function __construct(A $a, DInterface $d)
9999

100100
class E
101101
{
102-
public function __construct(D $d = null)
102+
public function __construct(?D $d = null)
103103
{
104104
}
105105
}
@@ -155,13 +155,6 @@ public function __construct(Dunglas $j, Dunglas $k)
155155
}
156156
}
157157

158-
class OptionalParameter
159-
{
160-
public function __construct(CollisionInterface $c = null, A $a, Foo $f = null)
161-
{
162-
}
163-
}
164-
165158
class BadTypeHintedArgument
166159
{
167160
public function __construct(Dunglas $k, NotARealClass $r)
@@ -195,7 +188,7 @@ public function __construct(A $k, $foo, Dunglas $dunglas, array $bar)
195188

196189
class MultipleArgumentsOptionalScalar
197190
{
198-
public function __construct(A $a, $foo = 'default_val', Lille $lille = null)
191+
public function __construct(A $a, $foo = 'default_val', ?Lille $lille = null)
199192
{
200193
}
201194
}
@@ -211,7 +204,7 @@ public function __construct(A $a, Lille $lille, $foo = 'some_val')
211204
*/
212205
class ClassForResource
213206
{
214-
public function __construct($foo, Bar $bar = null)
207+
public function __construct($foo, ?Bar $bar = null)
215208
{
216209
}
217210

@@ -350,7 +343,7 @@ public function setBar()
350343
{
351344
}
352345

353-
public function setOptionalNotAutowireable(NotARealClass $n = null)
346+
public function setOptionalNotAutowireable(?NotARealClass $n = null)
354347
{
355348
}
356349

@@ -399,7 +392,7 @@ class DecoratorImpl implements DecoratorInterface
399392

400393
class Decorated implements DecoratorInterface
401394
{
402-
public function __construct($quz = null, \NonExistent $nonExistent = null, DecoratorInterface $decorated = null, array $foo = [])
395+
public function __construct($quz = null, ?\NonExistent $nonExistent = null, ?DecoratorInterface $decorated = null, array $foo = [])
403396
{
404397
}
405398
}

0 commit comments

Comments
 (0)