Skip to content

Commit

Permalink
minor #54308 Add more explicit nullable types for default null values…
Browse files Browse the repository at this point in the history
… (alexandre-daubois)

This PR was merged into the 5.4 branch.

Discussion
----------

Add more explicit nullable types for default null values

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Continuing to bring deprecation-free support for PHP 8.4 (https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated)

If this gets merged, I'll take care of adding missing ones in upper branches if not done during upmerge 🙂

Commits
-------

e9d30ba Add more explicit nullable types for default null values
  • Loading branch information
nicolas-grekas committed Mar 19, 2024
2 parents 8c9979b + e9d30ba commit b9b35dc
Show file tree
Hide file tree
Showing 35 changed files with 80 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ContainerAwareFixture implements FixtureInterface, ContainerAwareInterface
{
public $container;

public function setContainer(ContainerInterface $container = null)
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StringWrapper
{
private $string;

public function __construct(string $string = null)
public function __construct(?string $string = null)
{
$this->string = $string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public function testBaselineFileWriteError()
$this->expectException(\ErrorException::class);
$this->expectExceptionMessageMatches('/[Ff]ailed to open stream: Permission denied/');

set_error_handler(static function (int $errno, string $errstr, string $errfile = null, int $errline = null): bool {
set_error_handler(static function (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null): bool {
if ($errno & (E_WARNING | E_WARNING)) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function connect(array $params, $username = null, $password = null, array
return $this->driver->connect($params, $username, $password, $driverOptions);
}

public function getDatabasePlatform(ServerVersionProvider $versionProvider = null): AbstractPlatform
public function getDatabasePlatform(?ServerVersionProvider $versionProvider = null): AbstractPlatform
{
return $this->driver->getDatabasePlatform($versionProvider);
}
Expand Down
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
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)
{
}
}
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
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
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 <fabien@symfony.com>
*
* 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)
{
}
}
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MockStream
* @param string|null $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
* opened_path should be set to the full path of the file/resource that was actually opened
*/
public function stream_open(string $path, string $mode, int $options, string &$opened_path = null): bool
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

class DeprecatedChoiceListFactory implements ChoiceListFactoryInterface
{
public function createListFromChoices(iterable $choices, callable $value = null): ChoiceListInterface
public function createListFromChoices(iterable $choices, ?callable $value = null): ChoiceListInterface
{
}

public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null): ChoiceListInterface
public function createListFromLoader(ChoiceLoaderInterface $loader, ?callable $value = null): ChoiceListInterface
{
}

public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, callable $index = null, callable $groupBy = null, $attr = null): ChoiceListView
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, ?callable $index = null, ?callable $groupBy = null, $attr = null): ChoiceListView
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable,
{
private $array;

public function __construct(array $array = null)
public function __construct(?array $array = null)
{
$this->array = $array ?: [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(array $translations)
$this->translations = $translations;
}

public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{
return $this->translations[$id] ?? $id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($varToClone)
$this->varToClone = $varToClone;
}

public function collect(Request $request, Response $response, \Throwable $exception = null)
public function collect(Request $request, Response $response, ?\Throwable $exception = null)
{
$this->data = $this->cloneVar($this->varToClone);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NonTraversableArrayObject implements \ArrayAccess, \Countable, \Serializab
{
private $array;

public function __construct(array $array = null)
public function __construct(?array $array = null)
{
$this->array = $array ?: [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TraversableArrayObject implements \ArrayAccess, \IteratorAggregate, \Count
{
private $array;

public function __construct(array $array = null)
public function __construct(?array $array = null)
{
$this->array = $array ?: [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function getA()
*
* @param ParentDummy|null $parent
*/
public function setB(ParentDummy $parent = null)
public function setB(?ParentDummy $parent = null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class RedirectableUrlMatcher extends UrlMatcher implements RedirectableUrlMatcherInterface
{
public function redirect(string $path, string $route, string $scheme = null): array
public function redirect(string $path, string $route, ?string $scheme = null): array
{
return [
'_controller' => 'Some controller reference...',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ public function getAllowedAttributes($classOrObject, array $context, bool $attri
/**
* {@inheritdoc}
*/
public function normalize($object, string $format = null, array $context = [])
public function normalize($object, ?string $format = null, array $context = [])
{
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, string $format = null): bool
public function supportsNormalization($data, ?string $format = null): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function denormalize($data, string $type, string $format = null, array $context = [])
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, string $type, string $format = null): bool
public function supportsDenormalization($data, string $type, ?string $format = null): bool
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class DenormalizableDummy implements DenormalizableInterface
{
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = [])
{
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Dummy implements NormalizableInterface, DenormalizableInterface
public $baz;
public $qux;

public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = [])
public function normalize(NormalizerInterface $normalizer, ?string $format = null, array $context = [])
{
return [
'foo' => $this->foo,
Expand All @@ -33,7 +33,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null
];
}

public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = [])
{
$this->foo = $data['foo'];
$this->bar = $data['bar'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DummyString implements DenormalizableInterface
/** @var string $value */
public $value;

public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = [])
{
$this->value = $data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EnvelopeNormalizer implements NormalizerInterface
{
private $serializer;

public function normalize($envelope, string $format = null, array $context = []): array
public function normalize($envelope, ?string $format = null, array $context = []): array
{
$xmlContent = $this->serializer->serialize($envelope->message, 'xml');

Expand All @@ -31,7 +31,7 @@ public function normalize($envelope, string $format = null, array $context = [])
];
}

public function supportsNormalization($data, string $format = null, array $context = []): bool
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return $data instanceof EnvelopeObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
class EnvelopedMessageNormalizer implements NormalizerInterface
{
public function normalize($message, string $format = null, array $context = []): array
public function normalize($message, ?string $format = null, array $context = []): array
{
return [
'text' => $message->text,
];
}

public function supportsNormalization($data, string $format = null, array $context = []): bool
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return $data instanceof EnvelopedMessage;
}
Expand Down
Loading

0 comments on commit b9b35dc

Please sign in to comment.