Skip to content

[PHPStan] improve FactoryCollection generics annotation with template-type #894

Open

Description

@edglev

Looks like there is an issue with phpstan type definition, specifically &Proxy<T> part breaking extension chain resolving to Factory<T>.

I don't understand why T&Proxy<T> is a not a subtype of object.
Is this a problem in PhpStan itself?

Minimal reproducer https://phpstan.org/r/eece4718-641c-4e5b-bac9-e4f1430f8fda

Just as a test - removing &Proxy<T> part from PersistentProxyObjectFactory makes PhpStan happy.

https://phpstan.org/r/dc467615-ce04-4f6c-8370-b6be346ec8c0

Activity

  1. nikophil commented on May 5, 2025

    @nikophil
    Member

    fixed 😄

    https://phpstan.org/r/441da82d-53cc-40ee-8b4e-4ff82b7988b6

    - @param FactoryCollection<Something, SomethingFactory> $somethingFactories
    + @param FactoryCollection<Something&Proxy<Something>, SomethingFactory> $somethingFactories

    those FactoryCollection with proxies are kinda hard to deal with. We're open to any good idea to simplify this :)

    By the way, the only use case to type hint with FactoryCollection is when dealing with data provider, but since Foundry 2.2 and PHPUnit 11.4, you can directly call YouFactory::createMany() in your data provider. you'll need to use the phpunit extension provided by Foundry. see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#phpunit-data-providers

  2. edglev commented on May 5, 2025

    @edglev
    Author

    Thank you ❤️ The use case for type hints is with data providers.

  3. reopened this on May 5, 2025
  4. edglev commented on May 5, 2025

    @edglev
    Author

    I've might have just found a way to simplify this - you can interpret the type from TFactory, example taken from here phpstan/phpstan#9053

    https://phpstan.org/r/7a4e6c92-1d39-4470-860f-890d47aee32c

    What do you think?

  5. nikophil commented on May 5, 2025

    @nikophil
    Member

    this is very cool! I didn't even know template-type exists!

    one thing I'd like, is that the current way of documenting factories still works. We've already introduced a BC break around this few months ago, and I would not want to introduce another one now...

    do you think both fashions of documenting FactoryCollection could be compatible?

  6. edglev commented on May 5, 2025

    @edglev
    Author

    I don't think so, best I can come up right now is to just leave the signature as is and ignore the T https://phpstan.org/r/d7745a05-7be3-4425-b94f-6de8fb773d41

    Tried a few ideas using Template default types from phpstan/phpstan#4801 but couldn't get any to work for both one and two template types specified. The hurdle here seems to be that the T to be removed is the first parameter. 😞

  7. nikophil commented on May 6, 2025

    @nikophil
    Member

    ok then, I'm sorry, but we're not going to integrate this now in Foundry

    BUT this could be integrated to Foundry 3, that might land at the end of the year (as for Symfony 8.0)

    BTW, there's some chances that we offer a rector set to migrate to Foundry 3, the same way we did for migration from 1 to 2

  8. nikophil commented on May 6, 2025

    @nikophil
    Member

    @edglev I'll keep this open 👍

    if at some point, you're willing to provide a PR with the changes with template-type and make the CI green that would be very nice, and we'll merge it in a future 3.x branch (but nothing hurry, this is planned for next November I think)

  9. changed the title [-]PhpStan complains about PersistentProxyObjectFactory subtypes[/-] [+][PHPStan] improve `FactoryCollection` generics annotation with `template-type`[/+] on May 6, 2025
  10. edglev commented on May 6, 2025

    @edglev
    Author

    Ah, sorry for closing this :D Sure, I'll make a PR sometime later

  11. nikophil commented on Jul 6, 2025

    @nikophil
    Member

    we may release Foundry 3, along with Symfony 8 in november. This might be the good time to think a little bit more how we can improve Factory collection, as we may allow some breaking change at this time

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions