Skip to content

Commit

Permalink
bug #5548 NullableTypeDeclarationForDefaultNullValueFixer - fix handl…
Browse files Browse the repository at this point in the history
…ing promoted properties (jrmajor, keradus)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

NullableTypeDeclarationForDefaultNullValueFixer - fix handling promoted properties

Fixes #5547

**Before:**
```diff
  public function __construct(
-     public ?array $x = null,
-     ?array $y = null,
+     public array $x = null,
+     array $y = null,
  ) { }
```
**After:**
```diff
  public function __construct(
      public ?array $x = null,
-     ?array $y = null,
+     array $y = null,
  ) { }
```

Commits
-------

077eb6a NullableTypeDeclarationForDefaultNullValueFixer - fix handling promoted properties
  • Loading branch information
keradus committed Mar 19, 2021
2 parents fc4791c + 077eb6a commit c64d3f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ private function fixFunctionParameters(Tokens $tokens, array $arguments)
}

$argumentTypeInfo = $argumentInfo->getTypeAnalysis();

if (
\PHP_VERSION_ID >= 80000
&& false === $this->configuration['use_nullable_type_declaration']
) {
$visibility = $tokens[$tokens->getPrevMeaningfulToken($argumentTypeInfo->getStartIndex())];

if ($visibility->isGivenKind([
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
])) {
continue;
}
}

if (true === $this->configuration['use_nullable_type_declaration']) {
if (!$argumentTypeInfo->isNullable() && 'mixed' !== $argumentTypeInfo->getName()) {
$tokens->insertAt($argumentTypeInfo->getStartIndex(), new Token([CT::T_NULLABLE_TYPE, '?']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,25 +406,52 @@ public function provideFixPhp74Cases()
}

/**
* @param string $expected
* @param null|string $expected
* @param string $input
*
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80($input, $expected = null)
{
if (null === $expected) {
$this->doTest($input);
} else {
$this->doTest($expected, $input);
}
}

/**
* @param null|string $input
* @param string $expected
*
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80($expected, $input = null)
public function testFixInverse80($expected, $input = null)
{
$this->fixer->configure(['use_nullable_type_declaration' => false]);

$this->doTest($expected, $input);
}

public function provideFix80Cases()
{
yield 'trailing comma' => [
'<?php function foo(?string $param = null,) {}',
'<?php function foo(string $param = null,) {}',
'<?php function foo(?string $param = null,) {}',
];

yield 'property promotion' => [
'<?php class Foo {
public function __construct(
public ?string $paramA = null,
protected ?string $paramB = null,
private ?string $paramC = null,
string $paramD = null,
$a = []
) {}
}',
'<?php class Foo {
public function __construct(
public ?string $paramA = null,
Expand Down

0 comments on commit c64d3f2

Please sign in to comment.