Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow readonly property write in __unserialize #929

Merged
merged 1 commit into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}

}