Narrow Cake\ORM\Association::find() return to SelectQuery<TargetEntity>#68
Open
dereuromark wants to merge 2 commits into
Open
Narrow Cake\ORM\Association::find() return to SelectQuery<TargetEntity>#68dereuromark wants to merge 2 commits into
dereuromark wants to merge 2 commits into
Conversation
Cake core declares Association::find() as SelectQuery<EntityInterface|array>, losing the target table's entity type. That forces consumers to write inline var annotations on every $this->Bar->Foos->find()->first() chain just to avoid property.notFound / property.nonObject errors. This change adds AssociationFindDynamicReturnTypeExtension, which reads the association's target table type — already exposed as BelongsTo<UsersTable> etc. by TableAssociationTypeNodeResolverExtension — derives the entity class via the standard CakePHP naming convention, and returns SelectQuery<TargetEntity> instead. The entity-derivation logic that was inlined in RepositoryEntityDynamicReturnTypeExtension is extracted to a new EntityClassFromTableClassTrait so both extensions stay consistent. Downstream narrowing of first() / firstOrFail() to Entity|null / Entity depends on Cake core's per-method return annotations (cakephp/cakephp#19439, merged for the 5.3.6 release); this extension is forward-compatible and contributes the SelectQuery template needed for that chain to resolve. Includes a unit test that asserts the narrowed SelectQuery template on the existing test_app fixture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Narrows
Cake\ORM\Association::find()fromSelectQuery<EntityInterface|array>toSelectQuery<TargetEntity>, eliminating the need for inlinevarannotations on every$this->Bar->Foos->find()->first()chain.Problem
Cake core declares
Association::find()asSelectQuery<EntityInterface|array>. The target entity type is lost the moment a query is built through an association property, so PHPStan reportsproperty.notFound/property.nonObjecton any subsequent->idaccess:Real codebases work around this by sprinkling inline var annotations on every callsite — tedious, noisy, and easy to forget.
Solution
AssociationFindDynamicReturnTypeExtensionreads the association's target table type — already exposed asBelongsTo<UsersTable>etc. by the existingTableAssociationTypeNodeResolverExtension— derives the entity class via the standard CakePHP naming convention, and returnsSelectQuery<TargetEntity>instead.The entity-derivation logic that was inlined in
RepositoryEntityDynamicReturnTypeExtensionis extracted to a newEntityClassFromTableClassTraitso both extensions stay consistent.Cake version interaction
Downstream narrowing of
first()/firstOrFail()toEntity|null/Entitydepends on Cake core's per-method return annotations from cakephp/cakephp#19439, merged for the 5.3.6 release. This extension is forward-compatible: on Cake< 5.3.6it still narrows theSelectQuerygeneric correctly (verified by the new test), and thefirst()/firstOrFail()narrowing comes online automatically once consumers upgrade.Real-world impact
Applied locally to a CakePHP 5.3 app: stripped 10+ inline var annotations across controllers and tables, all the call sites that go through association traversal. PHPStan stays clean at
level: 7withtreatPhpDocTypesAsCertain: false.Files
src/Type/AssociationFindDynamicReturnTypeExtension.php— new extensionsrc/Traits/EntityClassFromTableClassTrait.php— shared entity-class resolutionsrc/Type/RepositoryEntityDynamicReturnTypeExtension.php— uses the new trait, drops the duplicated methodextension.neon— registers the new extensiontests/TestCase/Type/Fake/AssociationFindCorrectUsage.php— fixture usingassertTypeon the existingNotesTable/MyUsersTabletest apptests/TestCase/Type/AssociationFindDynamicReturnTypeExtensionTest.php— runs PHPStan against the fixtureNote on the suppressed PHPStan rule
One
phpstan-ignore-next-line phpstanApi.instanceofTypeoninstanceof GenericObjectType: there is no non-deprecated equivalent in PHPStan 2.x for reading template parameters from a typed type. Happy to switch to a different approach if the maintainers have a preferred pattern.