-
Notifications
You must be signed in to change notification settings - Fork 519
Infer private properties in constructor, even if typehinted #374
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
Infer private properties in constructor, even if typehinted #374
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 :) |
@@ -10615,6 +10615,41 @@ public function testInferPrivatePropertyTypeFromConstructor( | |||
); | |||
} | |||
|
|||
public function dataInferPrivateTypedPropertyTypeFromConstructor(): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more modern way of testing such stuff is done in NodeScopeResolverTest::testFileAsserts(), please rework your tests based on that.
public function __construct(string ...$typedArray) | ||
{ | ||
$this->genericFoo = $this->newGenericFoo(\stdClass::class); | ||
$this->typedArray = $typedArray; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 justDatabaseClient
for the$db
property.