Skip to content

Prevent TooManyTemplateParams on Factory subclasses without own template params#682

Open
alies-dev wants to merge 4 commits intomasterfrom
worktree-677-too-many-template-params
Open

Prevent TooManyTemplateParams on Factory subclasses without own template params#682
alies-dev wants to merge 4 commits intomasterfrom
worktree-677-too-many-template-params

Conversation

@alies-dev
Copy link
Copy Markdown
Collaborator

Issue to Solve

When a user-space base factory overrides new() with @return static<TModel>, Psalm resolves the return to e.g. UserFactory<User>. But UserFactory has 0 own template params (it binds TModel via @extends), so Psalm emits TooManyTemplateParams.

Related

Closes #677

Solution Description

Add an AfterMethodCallAnalysis handler that strips excess template args from Factory subclass return types. For any TGenericObject in the return type that is a Factory subclass with no own template params, it replaces it with a plain TNamedObject.

Includes a type test covering both the intermediate-factory pattern and the standard direct-subclass pattern.

Checklist

  • Tests cover the change (type test in tests/Type/)

…template params

When a user-space base factory overrides new() with `@return static<TModel>`,
Psalm resolves the return to e.g. `UserFactory<User>`. But UserFactory has 0
own template params (it binds TModel via @extends), triggering TooManyTemplateParams.

Add AfterMethodCallAnalysis handler that strips excess template args by replacing
TGenericObject with TNamedObject for Factory subclasses with no own template params.

Closes #677
@alies-dev alies-dev self-assigned this Apr 7, 2026
@alies-dev alies-dev requested a review from Copilot April 7, 2026 12:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Psalm hook to prevent TooManyTemplateParams errors when Factory::new() return types get resolved into ChildFactory<Model> even though the child factory declares no template params of its own (binding via @extends).

Changes:

  • Register a new AfterMethodCallAnalysis hook to strip excess template args from Factory subclass return types when the subclass declares zero own templates.
  • Add a new handler (FactoryReturnTypeProvider) implementing the template-guard logic.
  • Add a type test covering both an intermediate base-factory pattern and the direct-subclass pattern.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/Type/tests/FactoryTooManyTemplateParamsTest.phpt Adds a regression test ensuring factory subclasses without own template params don’t end up as ChildFactory<Model> in inferred types.
src/Plugin.php Registers the new factory return-type guard hook.
src/Handlers/Eloquent/FactoryReturnTypeProvider.php Implements the AfterMethodCallAnalysis handler that replaces invalid TGenericObject factory subclasses with TNamedObject.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@alies-dev alies-dev requested a review from Copilot April 7, 2026 12:20
@alies-dev alies-dev added the release:fix for PRs only (used by release-drafter) label Apr 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix for PRs only (used by release-drafter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TooManyTemplateParams on child Factory classes without own template params

3 participants