Skip to content

Allow write access to offsets of readonly ArrayAccess properties #3795

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace PHPStan\Rules\Properties;

use ArrayAccess;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -89,6 +93,14 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$expr = $node->getAssignedExpr();
if (
(new ObjectType(ArrayAccess::class))->isSuperTypeOf($propertyReflection->getNativeType())->yes()
&& (($expr instanceof SetOffsetValueTypeExpr) || ($expr instanceof UnsetOffsetExpr))
) {
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong fix. You can't say that in general.

If I have private readonly ArrayAcess $a and I do $this->a = new Foo() twice, PHP does not allow that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 of course - you're right.

I have pushed a patch to expand the test to test for your case and to also check that we're dealing with a SetOffsetValueTypeExpr

}

$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of the constructor.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->identifier('property.readOnlyAssignNotInConstructor')
->build();
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,18 @@ public function testBug6773(): void
]);
}

public function testBug8929(): void
{
if (PHP_VERSION_ID < 80100) {
self::markTestSkipped('Test requires PHP 8.1');
}

$this->analyse([__DIR__ . '/data/bug-8929.php'], [
[
'Readonly property Bug8929\Test::$cache is assigned outside of the constructor.',
19,
],
]);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-8929.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug8929;

class Test
{
/** @var \WeakMap<object, mixed> */
protected readonly \WeakMap $cache;

public function __construct()
{
$this->cache = new \WeakMap();
}

public function add(object $key, mixed $value): void
{
$this->cache[$key] = $value; // valid offset access
unset($this->cache[$key]); // valid offset access
$this->cache = new \WeakMap(); // reassigning is invalid however
}
}
Loading