Skip to content

Commit

Permalink
feature: Introduce NullableTypeDeclarationFixer (#7002)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jul 4, 2023
1 parent d7b5c39 commit f503187
Show file tree
Hide file tree
Showing 15 changed files with 798 additions and 5 deletions.
13 changes: 13 additions & 0 deletions doc/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,19 @@ List of Available Rules
Part of rule sets `@PER <./ruleSets/PER.rst>`_ `@PER-CS1.0 <./ruleSets/PER-CS1.0.rst>`_ `@PSR12 <./ruleSets/PSR12.rst>`_ `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_

`Source PhpCsFixer\\Fixer\\Whitespace\\NoWhitespaceInBlankLineFixer <./../src/Fixer/Whitespace/NoWhitespaceInBlankLineFixer.php>`_
- `nullable_type_declaration <./rules/language_construct/nullable_type_declaration.rst>`_

Nullable single type declaration should be standardised using configured syntax.

Configuration options:

- | ``syntax``
| Whether to use question mark (`?`) or explicit `null` union for nullable type.
| Allowed values: ``'question_mark'`` and ``'union'``
| Default value: ``'question_mark'``

`Source PhpCsFixer\\Fixer\\LanguageConstruct\\NullableTypeDeclarationFixer <./../src/Fixer/LanguageConstruct/NullableTypeDeclarationFixer.php>`_
- `nullable_type_declaration_for_default_null_value <./rules/function_notation/nullable_type_declaration_for_default_null_value.rst>`_

Adds or removes ``?`` before single type declarations or ``|null`` at the end of union types when parameters have a default ``null`` value.
Expand Down
3 changes: 3 additions & 0 deletions doc/rules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ Language Construct
- `no_unset_on_property <./language_construct/no_unset_on_property.rst>`_ *(risky)*

Properties should be set to ``null`` instead of using ``unset``.
- `nullable_type_declaration <./language_construct/nullable_type_declaration.rst>`_

Nullable single type declaration should be standardised using configured syntax.
- `single_space_after_construct <./language_construct/single_space_after_construct.rst>`_ *(deprecated)*

Ensures a single space after language constructs.
Expand Down
68 changes: 68 additions & 0 deletions doc/rules/language_construct/nullable_type_declaration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
==================================
Rule ``nullable_type_declaration``
==================================

Nullable single type declaration should be standardised using configured syntax.

Configuration
-------------

``syntax``
~~~~~~~~~~

Whether to use question mark (``?``) or explicit ``null`` union for nullable
type.

Allowed values: ``'question_mark'`` and ``'union'``

Default value: ``'question_mark'``

Examples
--------

Example #1
~~~~~~~~~~

*Default* configuration.

.. code-block:: diff
--- Original
+++ New
<?php
-function bar(null|int $value, null|\Closure $callable): void {}
+function bar(?int $value, ?\Closure $callable): void {}
Example #2
~~~~~~~~~~

With configuration: ``['syntax' => 'union']``.

.. code-block:: diff
--- Original
+++ New
<?php
-function baz(?int $value, ?\stdClass $obj, ?array $config): ?int {}
+function baz(null|int $value, null|\stdClass $obj, null|array $config): null|int {}
Example #3
~~~~~~~~~~

With configuration: ``['syntax' => 'question_mark']``.

.. code-block:: diff
--- Original
+++ New
<?php
class ValueObject
{
- public null|string $name;
+ public ?string $name;
public ?int $count;
- public null|bool $internal;
- public null|\Closure $callback;
+ public ?bool $internal;
+ public ?\Closure $callback;
}
2 changes: 1 addition & 1 deletion src/Fixer/ClassNotation/OrderedTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function bar(null|string|int $foo): string|int;
* {@inheritdoc}
*
* Must run before TypesSpacesFixer.
* Must run after NullableTypeDeclarationForDefaultNullValueFixer.
* Must run after NullableTypeDeclarationFixer, NullableTypeDeclarationForDefaultNullValueFixer.
*/
public function getPriority(): int
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public function isCandidate(Tokens $tokens): bool
/**
* {@inheritdoc}
*
* Must run before NoUnreachableDefaultArgumentValueFixer, OrderedTypesFixer.
* Must run before NoUnreachableDefaultArgumentValueFixer, NullableTypeDeclarationFixer, OrderedTypesFixer.
*/
public function getPriority(): int
{
return 1;
return 3;
}

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
Expand Down
Loading

0 comments on commit f503187

Please sign in to comment.