Skip to content

Commit 1346ece

Browse files
committed
fix phpstan
1 parent 772833c commit 1346ece

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

rules/CodeQuality/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
namespace Rector\CodeQuality\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\ComplexType;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\ConstFetch;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\IntersectionType;
1013
use PhpParser\Node\Name;
1114
use PhpParser\Node\NullableType;
1215
use PhpParser\Node\Param;
@@ -77,7 +80,7 @@ public function refactor(Node $node): ClassMethod|Function_|null
7780

7881
$previousParam = $node->params[$key - 1] ?? null;
7982
if ($previousParam instanceof Param && $previousParam->default instanceof Expr) {
80-
$hasChanged = false;
83+
$hasChanged = true;
8184

8285
$param->default = new ConstFetch(new Name('null'));
8386
$paramType = $param->type;
@@ -90,10 +93,18 @@ public function refactor(Node $node): ClassMethod|Function_|null
9093
continue;
9194
}
9295

93-
if ($paramType instanceof UnionType) {
94-
$paramType->types[] = new ConstFetch(new Name('null'));
95-
$paramType->types = array_unique($paramType->types, SORT_REGULAR);
96+
if ($paramType instanceof UnionType || $paramType instanceof IntersectionType) {
97+
foreach ($paramType->types as $unionedType) {
98+
if ($unionedType instanceof Identifier && $this->isName($unionedType, 'null')) {
99+
continue 2;
100+
}
101+
}
96102

103+
$paramType->types[] = new Identifier('null');
104+
continue;
105+
}
106+
107+
if ($paramType instanceof ComplexType) {
97108
continue;
98109
}
99110

0 commit comments

Comments
 (0)