Skip to content

Commit b67256c

Browse files
phpstan-botclaude
andcommitted
Preserve PropertyInitializationExpr for properties with default values
When entering a set hook or a method called from the constructor, only remove PropertyInitializationExpr for properties without default values. Properties with defaults (e.g. `private int $bar = 1`) are always initialized, so isset() should report "not nullable nor uninitialized" rather than just "not nullable". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent db70c6c commit b67256c

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/Analyser/MutatingScope.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,13 @@ public function enterPropertyHook(
16081608
);
16091609

16101610
if ($hookName === 'set') {
1611-
$scope->exitPropertyInitialization($propertyName);
1611+
$classReflection = $this->getClassReflection();
1612+
if (
1613+
!$classReflection->hasNativeProperty($propertyName)
1614+
|| !$classReflection->getNativeProperty($propertyName)->getNativeReflection()->hasDefaultValue()
1615+
) {
1616+
$scope->exitPropertyInitialization($propertyName);
1617+
}
16121618
}
16131619

16141620
return $scope;

src/Analyser/NodeScopeResolver.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,14 @@ public function processStmtNode(
721721
if (!$expr instanceof PropertyInitializationExpr) {
722722
continue;
723723
}
724-
$methodScope = $methodScope->exitPropertyInitialization($expr->getPropertyName());
724+
$propertyName = $expr->getPropertyName();
725+
if (
726+
$classReflection->hasNativeProperty($propertyName)
727+
&& $classReflection->getNativeProperty($propertyName)->getNativeReflection()->hasDefaultValue()
728+
) {
729+
continue;
730+
}
731+
$methodScope = $methodScope->exitPropertyInitialization($propertyName);
725732
}
726733
}
727734
}

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public function testBug13473(): void
569569

570570
$this->analyse([__DIR__ . '/data/bug-13473.php'], [
571571
[
572-
'Property Bug13473\FooWithDefault::$bar (int) in isset() is not nullable.',
572+
'Property Bug13473\FooWithDefault::$bar in isset() is not nullable nor uninitialized.',
573573
28,
574574
],
575575
]);
@@ -581,7 +581,7 @@ public function testBug13473Method(): void
581581

582582
$this->analyse([__DIR__ . '/data/bug-13473-method.php'], [
583583
[
584-
'Property Bug13473Method\FooWithDefault::$bar (int) in isset() is not nullable.',
584+
'Property Bug13473Method\FooWithDefault::$bar in isset() is not nullable nor uninitialized.',
585585
34,
586586
],
587587
]);

0 commit comments

Comments
 (0)