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

isset() on readonly properties in constructors & inheritance #9863

Closed
levu42 opened this issue Sep 8, 2023 · 0 comments · Fixed by phpstan/phpstan-src#3049
Closed

isset() on readonly properties in constructors & inheritance #9863

levu42 opened this issue Sep 8, 2023 · 0 comments · Fixed by phpstan/phpstan-src#3049
Labels

Comments

@levu42
Copy link

levu42 commented Sep 8, 2023

Bug report

Disclaimer I'm not even sure whether there's a PHP bug as well inside, but there's at least one PHPStan bug in it.

My basic objective is to have a class with a readonly property that's assigned once in the constructor, and a child class having a constructor with a default value for that readonly property.

(Real world example: class Unit, new Unit('kg'), class WeightUnit extends Unit, new WeightUnit() with 'kg' being the default.)

I couldn't find anything in the PHP specs regarding readonly & inheritance, so I just assumed that the child constructor is called, the property set, then the parent constructor is called, and it needs to check whether the readonly property is already set (if it's called from a child constructor) or not (called directly) before trying to set it itself.

So I think one PHPStan at least is to not report the usage of isset() in a constructor on a readonly property when the class isn't final. The other bug (feature request?) is to recognize the PHP fatal error when a constructor calls a parent constructor that sets a readonly property after the child constructor already set it.

Code snippet that reproduces the problem

https://phpstan.org/r/56181865-dda9-4e35-bf25-571ef2deb066

Expected output

  • I expected an error in line 15 (parent::__construct($foo)) because PHP throws a Fatal error there, but none was reported

  • I expected no errors in the WithIsset part, because that seems to work with PHP 8.2

PS: BTW: I figured out, how to do it way cleaner, but this was an intermediate step, and I wanted to report it, so I don't need any advice in that regard, but PHPStan's reported errors should still align with PHP's fatal errors :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants