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 write access to offsets of readonly ArrayAccess properties #3795

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from

Conversation

pilif
Copy link
Contributor

@pilif pilif commented Jan 24, 2025

In case a readonly property is an object which implements ArrayAccess, offet accesses to said property are turned into method calls on PHP's side and are allowed even on read-only properties.

this fixes phpstan/phpstan#8929

@pilif pilif marked this pull request as draft January 24, 2025 08:49
@pilif
Copy link
Contributor Author

pilif commented Jan 24, 2025

one sec - will fix to make this work properly with old PHP versions

@pilif pilif marked this pull request as ready for review January 24, 2025 09:00
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@pilif
Copy link
Contributor Author

pilif commented Jan 24, 2025

ok. much better.

I don't think the failing rector integration test is related to this at all

@@ -89,6 +91,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ((new ObjectType(ArrayAccess::class))->isSuperTypeOf($propertyReflection->getNativeType())->yes()) {
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

@pilif pilif force-pushed the bug-8929 branch 2 times, most recently from 90de7f8 to 8b59053 Compare January 28, 2025 12:53
pilif added 2 commits January 28, 2025 14:07
In case a readonly property is an object which implements ArrayAccess,
offet accesses to said property are turned into method calls on PHP's
side and are allowed even on read-only properties.

this fixes phpstan/phpstan#8929
of course just checking for ArrayAccess isn't enough - it's only valid
for index assignment operations 🤦
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Readonly object ArrayAccess assignment detection
3 participants