diff --git a/rules-tests/Php74/Rector/If_/IfToNullCoalescingAssignRector/Fixture/skip_typed_non_nullable_property.php.inc b/rules-tests/Php74/Rector/If_/IfToNullCoalescingAssignRector/Fixture/skip_typed_non_nullable_property.php.inc new file mode 100644 index 00000000000..cad8a150748 --- /dev/null +++ b/rules-tests/Php74/Rector/If_/IfToNullCoalescingAssignRector/Fixture/skip_typed_non_nullable_property.php.inc @@ -0,0 +1,17 @@ +isNonNullableProperty($testedExpr)) { + return null; + } + // the assigned value must not reference the target, e.g. $x = $x + 1 $selfReference = $this->betterNodeFinder->findFirst( $assign->expr, @@ -112,6 +121,20 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::NULL_COALESCE_ASSIGN; } + private function isNonNullableProperty(Expr $expr): bool + { + if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) { + return false; + } + + $propertyType = $this->nodeTypeResolver->getType($expr); + if ($propertyType instanceof MixedType) { + return false; + } + + return ! TypeCombinator::containsNull($propertyType); + } + private function matchNullGuardedExpr(Expr $expr): ?Expr { // ! isset($value)