Infer private properties in constructor, even if typehinted#374
Infer private properties in constructor, even if typehinted#374ondrejmirtes merged 2 commits intophpstan:masterfrom
Conversation
a11f084 to
446647a
Compare
5855971 to
29d4260
Compare
|
Hi @ondrejmirtes, as this PR started as a draft, I don't know if you received a notification when I marked it as ready for review, so I am just raising a hand in case :) |
There was a problem hiding this comment.
The more modern way of testing such stuff is done in NodeScopeResolverTest::testFileAsserts(), please rework your tests based on that.
There was a problem hiding this comment.
I'm missing a test scenario here - the property has native type int, the constructor assigns type string. I'm interested in what type the final property has - should be int.
There was a problem hiding this comment.
Nice catch, I added some cases, it seems TypehintHelper is doing a very good job.
Do you see other scenario where it could misbehave?
Inference could provide a more precise type, eg: generic type, than the native typehint.
29d4260 to
565d41a
Compare
|
Thank you! |
|
Thank you for building and maintaining this awesome project :) |
|
I'm sorry, I had to revert this. It led to some undesired behaviour that's hard to avoid (while keeping the rest of the functionality): phpstan/phpstan#4234 |
|
No problem, sorry for the troubles :( |
|
Damn, that was great feature to reduce phpdocs. @ondrejmirtes do you have a plan to bring it back with fixes? |
|
@snapshotpl It still works when the property doesn't have a native typehint. I don't plan to bring it back when the property has a native typehint, the need for PHPDocs is reduced greatly in that case, and the feature led to some false positives... |
Hi!
When using the flag
inferPrivatePropertyTypeFromConstructor: true, the inference is disabled if the property is natively typehinted.However the inference could provide a more specific type (eg: generic).
A usecase is ORM:
This PR allows PHPStan to infer
DatabaseClient<User>instead of justDatabaseClientfor the$dbproperty.