Skip to content

Commit

Permalink
Add more explicit nullable types for default null values
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Mar 18, 2024
1 parent cc1fb23 commit 0ba1fa4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Tests/Compiler/AutowirePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\MultipleArgumentsOptionalScalarNotReallyOptional;
use Symfony\Component\DependencyInjection\Tests\Fixtures\OptionalParameter;
use Symfony\Component\DependencyInjection\Tests\Fixtures\WithTarget;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Contracts\Service\Attribute\Required;
Expand Down Expand Up @@ -405,6 +406,9 @@ public function testResolveParameter()
$this->assertEquals(Foo::class, $container->getDefinition('bar')->getArgument(0));
}

/**
* @group legacy
*/
public function testOptionalParameter()
{
$container = new ContainerBuilder();
Expand Down
4 changes: 2 additions & 2 deletions Tests/Fixtures/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Bar implements BarInterface
{
public $quz;

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

public static function create(\NonExistent $nonExistent = null, $factory = null)
public static function create(?\NonExistent $nonExistent = null, $factory = null)
{
}
}
2 changes: 1 addition & 1 deletion Tests/Fixtures/CheckTypeDeclarationsPass/BarMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setFoosVariadic(Foo $foo, Foo ...$foos)
$this->foo = $foo;
}

public function setFoosOptional(Foo $foo, Foo $fooOptional = null)
public function setFoosOptional(Foo $foo, ?Foo $fooOptional = null)
{
$this->foo = $foo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BarOptionalArgument
{
public $foo;

public function __construct(\stdClass $foo = null)
public function __construct(?\stdClass $foo = null)
{
$this->foo = $foo;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/CheckTypeDeclarationsPass/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function createBar()
return new Bar(new \stdClass());
}

public static function createBarArguments(\stdClass $stdClass, \stdClass $stdClassOptional = null)
public static function createBarArguments(\stdClass $stdClass, ?\stdClass $stdClassOptional = null)
{
return new Bar($stdClass);
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/Fixtures/OptionalParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Tests\Compiler\A;
use Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface;
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;

class OptionalParameter
{
public function __construct(?CollisionInterface $c = null, A $a, ?Foo $f = null)
{
}
}
2 changes: 1 addition & 1 deletion Tests/Fixtures/Prototype/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#[When(env: 'dev')]
class Foo implements FooInterface, Sub\BarInterface
{
public function __construct($bar = null, iterable $foo = null, object $baz = null)
public function __construct($bar = null, ?iterable $foo = null, ?object $baz = null)
{
}

Expand Down
17 changes: 5 additions & 12 deletions Tests/Fixtures/includes/autowiring_classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __construct(A $a, DInterface $d)

class E
{
public function __construct(D $d = null)
public function __construct(?D $d = null)
{
}
}
Expand Down Expand Up @@ -155,13 +155,6 @@ public function __construct(Dunglas $j, Dunglas $k)
}
}

class OptionalParameter
{
public function __construct(CollisionInterface $c = null, A $a, Foo $f = null)
{
}
}

class BadTypeHintedArgument
{
public function __construct(Dunglas $k, NotARealClass $r)
Expand Down Expand Up @@ -195,7 +188,7 @@ public function __construct(A $k, $foo, Dunglas $dunglas, array $bar)

class MultipleArgumentsOptionalScalar
{
public function __construct(A $a, $foo = 'default_val', Lille $lille = null)
public function __construct(A $a, $foo = 'default_val', ?Lille $lille = null)
{
}
}
Expand All @@ -211,7 +204,7 @@ public function __construct(A $a, Lille $lille, $foo = 'some_val')
*/
class ClassForResource
{
public function __construct($foo, Bar $bar = null)
public function __construct($foo, ?Bar $bar = null)
{
}

Expand Down Expand Up @@ -350,7 +343,7 @@ public function setBar()
{
}

public function setOptionalNotAutowireable(NotARealClass $n = null)
public function setOptionalNotAutowireable(?NotARealClass $n = null)
{
}

Expand Down Expand Up @@ -399,7 +392,7 @@ class DecoratorImpl implements DecoratorInterface

class Decorated implements DecoratorInterface
{
public function __construct($quz = null, \NonExistent $nonExistent = null, DecoratorInterface $decorated = null, array $foo = [])
public function __construct($quz = null, ?\NonExistent $nonExistent = null, ?DecoratorInterface $decorated = null, array $foo = [])
{
}
}
Expand Down

0 comments on commit 0ba1fa4

Please sign in to comment.