[Feature Request] Overriding dynamic service return types

Open
#301 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
php, symfony
Domain
tooling

Research direction

Start by reading ServiceDynamicReturnTypeExtension and DynamicMethodReturnTypeExtension to understand how the Symfony extension and custom extensions contribute service return types. Determine how a service-specific type or opt-out could take precedence without breaking existing dynamic return types. Done means a documented configuration or service definition mechanism is covered by tests and preserves the more specific custom type.

Written by the indexing model from the issue text.

Description

Currently there is no way to override dynamic service return types on the container that are specified in ServiceDynamicReturnTypeExtension.

This is useful to provide more specific information e.g. if the service implement a generic class there is currently no way to teach phpstan that a specific service is of a specific type.

I already build a custom DynamicMethodReturnTypeExtension that adds the generic types, but in the end phpstan makes a union for all types returned by any DynamicMethodReturnTypeExtension. This leads to the less specific type of the smyfony extension "winning" and in fact overriding my more specific type again.

I would like to the a feature where you can specify the phpstan type of a service, or at least mark a service to be ignored by the symfony phpstan extension, so you can write your custom DynamicMethodReturnTypeExtension for those services.

Happy to contribute such a feature, but I'm currently not sure how to best implement this. The only way I see now is adding a custom tag to the service definition and specifying the type there and that type will then take precedence over the class name of the service.

Dominant language
PHP
Stars
800
Forks
104
Avg merge
4m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-symfony

All issues in phpstan/phpstan-symfony

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.