Skip to content

Commit

Permalink
[DowngradePhp74] Do not remove non-null default value on nullable on …
Browse files Browse the repository at this point in the history
…DowngradeTypedPropertyRector (#1687)
  • Loading branch information
samsonasik committed Jan 16, 2022
1 parent a66d1fb commit ca696bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector\Fixture;

class NullableTypeWithDefaultNotNull
{
protected ?string $test = "test";
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector\Fixture;

class NullableTypeWithDefaultNotNull
{
/**
* @var string|null
*/
protected $test = "test";
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
Expand Down Expand Up @@ -66,7 +67,8 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->type instanceof NullableType) {
$default = $node->props[0]->default;
if ($node->type instanceof NullableType && $default instanceof Expr && $this->valueResolver->isNull($default)) {
$node->props[0]->default = null;
}

Expand Down

0 comments on commit ca696bc

Please sign in to comment.