Skip to content

Hooked properties cannot be both final and private #3830

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

Merged
merged 22 commits into from
Feb 26, 2025

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 16, 2025

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

This pull request has been marked as ready for review.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Even non-hooked property cannot be final&private: https://3v4l.org/fAXFZ

Also please check the same error for methods and constants (it might already be implemented, not sure)

Also, please add an error that would report final properties on PHP < 8.4.

Also, please add error for final properties in interfaces.

Also, please add error for final hooks in abstract properties (in both class and interface): https://3v4l.org/hSO0C#v8.4.4

Also, abstract + final property together is not allowed.

@staabm
Copy link
Contributor Author

staabm commented Feb 19, 2025

memo to me: more cases to consider

additional: follow discussion in php/php-src#17861

@staabm
Copy link
Contributor Author

staabm commented Feb 22, 2025

Even non-hooked property cannot be final&private: 3v4l.org/fAXFZ

done

Also please check the same error for methods and constants (it might already be implemented, not sure)

methods are already covered and tests exist for it

constants are missing (separate PR? new rule similar to FinalPrivateMethodRule?)

Also, please add an error that would report final properties on PHP < 8.4.

done

Also, please add error for final properties in interfaces.

should I add a new PropertyInInterfaceRule ?

Also, please add error for final hooks in abstract properties (in both class and interface): 3v4l.org/hSO0C#v8.4.4

done

Also, abstract + final property together is not allowed.

php-src doesn't error on https://3v4l.org/2fSaq#v8.4.4

@ondrejmirtes
Copy link
Member

should I add a new PropertyInInterfaceRule ?

There is already PropertiesInInterfaceRule.

@ondrejmirtes
Copy link
Member

Reported bug to PHP php/php-src#17916

@ondrejmirtes
Copy link
Member

Please add an error for final abstract properties as it's now fixed and reported in PHP.

@staabm
Copy link
Contributor Author

staabm commented Feb 25, 2025

I think we are running now in a php-parser bug: nikic/PHP-Parser#1071. will look further

@ondrejmirtes
Copy link
Member

It's fine if it's a parse error. It means we don't need to implement a custom rule to detect this invalid code.

@ondrejmirtes
Copy link
Member

One more thing to report - final hook without a body in a class: https://phpstan.org/r/1d253f61-25a3-4654-8ef0-4f3c4268c770

@staabm
Copy link
Contributor Author

staabm commented Feb 26, 2025

return [
RuleErrorBuilder::message('Property in interface cannot be explicitly abstract.')
->nonIgnorable()
->identifier('property.hookedStatic')
Copy link
Member

Choose a reason for hiding this comment

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

wrong identifier here

Copy link
Member

Choose a reason for hiding this comment

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

Still wrong? property.hookedStatic does not make sense for Property in interface cannot be explicitly abstract.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

argh, sorry

return [
RuleErrorBuilder::message('Property in interface cannot be explicitly abstract.')
->nonIgnorable()
->identifier('property.hookedStatic')
Copy link
Member

Choose a reason for hiding this comment

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

Still wrong? property.hookedStatic does not make sense for Property in interface cannot be explicitly abstract.?

@ondrejmirtes ondrejmirtes merged commit 49e49b0 into phpstan:2.1.x Feb 26, 2025
100 checks passed
@ondrejmirtes
Copy link
Member

Perfect, thank you!

@staabm staabm deleted the private-final branch February 26, 2025 15:55
@ondrejmirtes
Copy link
Member

Something you could do next: abstract + private (https://3v4l.org/es9gD)

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.

3 participants