Skip to content

Commit

Permalink
Allow readonly property write in __unserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk authored and ondrejmirtes committed Jan 17, 2022
1 parent b81066d commit a712907
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Expand Up @@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}

if (strtolower($scopeMethod->getName()) === '__construct') {
if (strtolower($scopeMethod->getName()) === '__construct' || strtolower($scopeMethod->getName()) === '__unserialize') {
if (!$scope->getType($propertyFetch->var) instanceof ThisType) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))->build();
}
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign.php
Expand Up @@ -164,3 +164,23 @@ public function doFoo(Foo $foo, int $i)
}

}

class Unserialization
{

private readonly int $foo;

public function __construct(int $foo)
{
$this->foo = $foo; // constructor - fine
}

/**
* @param array<int, int> $data
*/
public function __unserialize(array $data) : void
{
[$this->foo] = $data; // __unserialize - fine
}

}

0 comments on commit a712907

Please sign in to comment.