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

Readonly properties cannot be unset() #3827

Merged
merged 6 commits into from
Feb 28, 2025

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 15, 2025

@staabm staabm marked this pull request as ready for review February 15, 2025 16:40
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the unset-readonly branch 2 times, most recently from 3ee6c73 to 06e46c2 Compare February 16, 2025 07:53
@staabm
Copy link
Contributor Author

staabm commented Feb 27, 2025

adjusted per feedback. thank you.

} elseif (
$node instanceof Node\Expr\PropertyFetch
&& $node->name instanceof Node\Identifier
) {
Copy link
Member

Choose a reason for hiding this comment

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

We have better ways of getting a property reflection from PropertyFetch. Two actually:

  1. PropertyReflectionFinder. FoundPropertyReflection will even tell you if it's a native one or not.
  2. Scope::getPropertyReflection()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh nice.

I was already searching how to know about property hooks so I can implement the error for unsetting a hooked property.

will be a followup PR

Copy link
Member

Choose a reason for hiding this comment

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

I wrote about property hooks here phpstan/phpstan#12337

}

if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
$type = $scope->getType($node->var);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to do this, just ask for property declaringClass getDisplayName.


return RuleErrorBuilder::message(
sprintf(
'Cannot unset %s property %s of %s.',
Copy link
Member

Choose a reason for hiding this comment

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

Usually we do Foo::$name format.

%s of %s sounds like an ancient text: Yennefer of Vengerberg 😀

@ondrejmirtes ondrejmirtes merged commit 4efa22b into phpstan:2.1.x Feb 28, 2025
417 checks passed
@ondrejmirtes
Copy link
Member

Perfect, thank you!

@staabm staabm deleted the unset-readonly branch February 28, 2025 09:30
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.

unset on a readonly property should report an error
3 participants