Skip to content

Key the other-method query builder cache by the bound declaring class - #821

Open
giosh94mhz wants to merge 1 commit into
phpstan:2.1.xfrom
giosh94mhz:fix-other-method-template-cache
Open

giosh94mhz wants to merge 1 commit into
phpstan:2.1.xfrom
giosh94mhz:fix-other-method-template-cache

Conversation

@giosh94mhz

Copy link
Copy Markdown

Repositories extending a generic base repository (@template T of object) can get the wrong entity for T in their query builders: when two subclasses bind T to EntityA and EntityB, both end up analysed as EntityA, because the query builders of the inherited method are cached once per declaring class. QueryBuilderDqlRule then reports false DQL errors, depending on the analysis order.

Details

OtherMethodQueryBuilderParser caches the query builders returned by a method under file-DeclaringClass-method. When that method is inherited from a generic base repository, the declaring class reflection is already bound to the caller's template types, so the builders it returns depend on that binding, but the cache key ignores it. The first repository analysed decides the entity for all its siblings.

The new fixture tests/Rules/Doctrine/ORM/data/query-builder-dql-generic-repository.php reproduces it:

  1. AbstractGenericRepository<T of GenericRepositorySuperclass> has a protected createSharedQueryBuilder(),
  2. and GenericRepositoryA / GenericRepositoryB bind T to two entities with different fields
  3. and query e.onlyA and e.onlyB on it.

Without the fix, QueryBuilderDqlRuleTest::testQueryBuilderInheritedFromGenericRepository() fails with:

46: QueryBuilder: [Semantical Error] ... Error: Class PHPStan\Rules\Doctrine\ORM\GenericRepositoryEntityA has no field or association named onlyB

The error depends on the analysis order, so it comes and goes between runs and between machines. When the abstract class calls the method itself first, the builder is cached with T resolved to its bound, and every subclass gets FROM the bound class.

Fix

The cache key now also includes the active template type map of a generic declaring class, described with VerbosityLevel::cache(). Non-generic classes keep the same key as before.

Testing

I've done testing locally so far. The make lint, make cs and make phpstan are clean. The test suite passes on PHP 7.4 and 8.5 with both lowest and highest dependencies, and on ORM 3 / DBAL 4 with the compatibility patches.

For every other test I'll wait your CI results

OtherMethodQueryBuilderParser cached the query builders of an inherited
method by its declaring class name, while the builders depend on the
template types the caller binds. The first repository analysed decided
the entity for every sibling, so a generic base repository made the DQL
rule report fields of the wrong entity, varying with analysis order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant