Fork me on GitHub
a Sensio Labs Product

I ate three cookies πŸͺ (v3.40.2) edition

Rule nullable_type_declaration_for_default_null_valueΒΆ

Adds or removes ? before single type declarations or |null at the end of union types when parameters have a default null value.

DescriptionΒΆ

Rule is applied only in a PHP 7.1+ environment.

ConfigurationΒΆ

use_nullable_type_declarationΒΆ

Whether to add or remove ? or |null to parameters with a default null value.

Allowed types: bool

Default value: true

ExamplesΒΆ

Example #1ΒΆ

Default configuration.

--- Original
+++ New
 <?php
-function sample(string $str = null)
+function sample(?string $str = null)
 {}

Example #2ΒΆ

With configuration: ['use_nullable_type_declaration' => false].

--- Original
+++ New
 <?php
-function sample(?string $str = null)
+function sample(string $str = null)
 {}

Example #3ΒΆ

Default configuration.

--- Original
+++ New
 <?php
-function sample(string|int $str = null)
+function sample(string|int|null $str = null)
 {}

Example #4ΒΆ

With configuration: ['use_nullable_type_declaration' => false].

--- Original
+++ New
 <?php
-function sample(string|int|null $str = null)
+function sample(string|int $str = null)
 {}

Example #5ΒΆ

Default configuration.

--- Original
+++ New
 <?php
-function sample(\Foo&\Bar $str = null)
+function sample((\Foo&\Bar)|null $str = null)
 {}

Example #6ΒΆ

With configuration: ['use_nullable_type_declaration' => false].

--- Original
+++ New
 <?php
-function sample((\Foo&\Bar)|null $str = null)
+function sample(\Foo&\Bar $str = null)
 {}

Rule setsΒΆ

The rule is part of the following rule set:

  • @Symfony with config:

    ['use_nullable_type_declaration' => false]

Source classΒΆ

PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer