Skip to content

Commit 8fe4401

Browse files
committed
Check additional assign of promoted readonly property
1 parent b0b8eeb commit 8fe4401

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ private function processStmtNode(
546546
$scope->getClassReflection()->isReadOnly(),
547547
false,
548548
), $methodScope);
549+
$methodScope = $methodScope->assignExpression(new PropertyInitializationExpr($param->var->name), new MixedType(), new MixedType());
549550
}
550551
}
551552

src/Node/ClassPropertiesNode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function getUninitializedProperties(
9999
$classReflection = $scope->getClassReflection();
100100

101101
$properties = [];
102+
$originalProperties = [];
102103
foreach ($this->getProperties() as $property) {
103104
if ($property->isStatic()) {
104105
continue;
@@ -109,6 +110,7 @@ public function getUninitializedProperties(
109110
if ($property->getDefault() !== null) {
110111
continue;
111112
}
113+
$originalProperties[$property->getName()] = $property;
112114
if ($property->isPromoted()) {
113115
continue;
114116
}
@@ -139,7 +141,6 @@ public function getUninitializedProperties(
139141
$methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $this->methodCalls, $constructors);
140142
$prematureAccess = [];
141143
$additionalAssigns = [];
142-
$originalProperties = $properties;
143144

144145
foreach ($this->getPropertyUsages() as $usage) {
145146
$fetch = $usage->getFetch();

tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ public function testRule(): void
108108
'Readonly property MissingReadOnlyPropertyAssign\FooTraitClass::$doubleAssigned is already assigned.',
109109
149,
110110
],
111+
[
112+
'Readonly property MissingReadOnlyPropertyAssign\AdditionalAssignOfReadonlyPromotedProperty::$x is already assigned.',
113+
188,
114+
],
111115
]);
112116
}
113117

tests/PHPStan/Rules/Properties/data/missing-readonly-property-assign.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,13 @@ class BarClass
179179
use BarTrait;
180180

181181
}
182+
183+
class AdditionalAssignOfReadonlyPromotedProperty
184+
{
185+
186+
public function __construct(private readonly int $x)
187+
{
188+
$this->x = 2;
189+
}
190+
191+
}

0 commit comments

Comments
 (0)