Skip to content

Commit

Permalink
Check additional assign of promoted readonly property
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 2, 2023
1 parent b0b8eeb commit 8fe4401
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ private function processStmtNode(
$scope->getClassReflection()->isReadOnly(),
false,
), $methodScope);
$methodScope = $methodScope->assignExpression(new PropertyInitializationExpr($param->var->name), new MixedType(), new MixedType());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Node/ClassPropertiesNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function getUninitializedProperties(
$classReflection = $scope->getClassReflection();

$properties = [];
$originalProperties = [];
foreach ($this->getProperties() as $property) {
if ($property->isStatic()) {
continue;
Expand All @@ -109,6 +110,7 @@ public function getUninitializedProperties(
if ($property->getDefault() !== null) {
continue;
}
$originalProperties[$property->getName()] = $property;
if ($property->isPromoted()) {
continue;
}
Expand Down Expand Up @@ -139,7 +141,6 @@ public function getUninitializedProperties(
$methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $this->methodCalls, $constructors);
$prematureAccess = [];
$additionalAssigns = [];
$originalProperties = $properties;

foreach ($this->getPropertyUsages() as $usage) {
$fetch = $usage->getFetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function testRule(): void
'Readonly property MissingReadOnlyPropertyAssign\FooTraitClass::$doubleAssigned is already assigned.',
149,
],
[
'Readonly property MissingReadOnlyPropertyAssign\AdditionalAssignOfReadonlyPromotedProperty::$x is already assigned.',
188,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,13 @@ class BarClass
use BarTrait;

}

class AdditionalAssignOfReadonlyPromotedProperty
{

public function __construct(private readonly int $x)
{
$this->x = 2;
}

}

0 comments on commit 8fe4401

Please sign in to comment.