Skip to content

Commit

Permalink
NullableTypeDeclarationForDefaultNullValueFixer - fix handling promot…
Browse files Browse the repository at this point in the history
…ed properties
  • Loading branch information
jrmajor authored and keradus committed Mar 19, 2021
1 parent c4b84e0 commit 077eb6a
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 077eb6a

Please sign in to comment.