Skip to content

Commit 22bdcd5

Browse files
authored
Re-ensure nullable type on constructor when param has type (#246)
* Re-ensure nullable type on constructor when param has type * fix
1 parent e7d904c commit 22bdcd5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector\Fixture;
4+
5+
final class ParamNoType
6+
{
7+
public function __construct(public $o1 = new stdClass)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector\Fixture;
17+
18+
final class ParamNoType
19+
{
20+
public function __construct(public $o1 = null)
21+
{
22+
$this->o1 = $o1 ?? new stdClass;
23+
}
24+
}
25+
26+
?>

rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function replaceNewInParams(ClassMethod|Closure|Function_ $functionLike)
176176
}
177177

178178
// recheck after
179-
if ($isConstructor) {
179+
if ($isConstructor && $param->type !== null) {
180180
$param->type = $this->ensureNullableType($param->type);
181181
}
182182

0 commit comments

Comments
 (0)