feat: infer property types from constructor assignments#81
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #81 +/- ##
=======================================
Coverage 87.02% 87.03%
=======================================
Files 153 153
Lines 93589 93673 +84
=======================================
+ Hits 81450 81526 +76
- Misses 12139 12147 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
d61bad6 to
c0906f2
Compare
c0906f2 to
981d252
Compare
|
Interesting addition, personally I think projects should just hint there code properly since there's a lot of pitfalls in trying to infer from code what the signature should be, but this is nicely contained. I added an extra check to make sure it doesn't trigger if there's any other forms of types on the property. I also hooked it up to the PHPDoc generation logic which adds a nice syngery effect where the LSP can now generate plausible types which the developer can then correct in necessary. |
Properties without explicit type declarations can now have their types inferred when they’re assigned using
new ClassName().This works in both cases:
Promoted constructor parameters with defaults (e.g. private $repo = new Repository())
Assignments inside the constructor (e.g. $this->repo = new Repository())
As a result, features like go-to-definition and autocomplete work correctly for chained calls such as $this->repo->findById(), even when the property itself isn’t typed. This is especially useful for legacy codebases and modern PHP 8.1+ patterns that rely on inline object instantiation.