Skip to content

Commit a712907

Browse files
ruudkondrejmirtes
authored andcommitted
Allow readonly property write in __unserialize
Fixes phpstan/phpstan#6384
1 parent b81066d commit a712907

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Rules/Properties/ReadOnlyPropertyAssignRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
6767
throw new ShouldNotHappenException();
6868
}
6969

70-
if (strtolower($scopeMethod->getName()) === '__construct') {
70+
if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
7171
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
7272
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
7373
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,23 @@ public function doFoo(Foo $foo, int $i)
164164
}
165165

166166
}
167+
168+
class Unserialization
169+
{
170+
171+
private readonly int $foo;
172+
173+
public function __construct(int $foo)
174+
{
175+
$this->foo = $foo; // constructor - fine
176+
}
177+
178+
/**
179+
* @param array<int, int> $data
180+
*/
181+
public function __unserialize(array $data) : void
182+
{
183+
[$this->foo] = $data; // __unserialize - fine
184+
}
185+
186+
}

0 commit comments

Comments
 (0)