Skip to content

Conversation

Marmelatze
Copy link

Allows the usage of EntityRepositories by interface name.
So EntityRepositories can be passed around that implement a common interface and return entities that have a common interface.

Currently the extension throws a MappingException, as the interface can not be found by the doctrine ClassMetadataLoader

@ruudk
Copy link
Contributor

ruudk commented Jan 24, 2022

@Marmelatze Could you rebase this? It would be great to have support like this merged.
@ondrejmirtes Is it something that can be accepted?

@ondrejmirtes
Copy link
Member

Can you please explain what's the current situation on phpstan-doctrine 1.2.6 and what kind of code pattern doesn't work and what's the error message from PHPStan?

@Marmelatze
Copy link
Author

For example:

<?php
interface SomethingInterface {
    
}

class Something implements SomethingInterface {
    
}

class Factory
{
    /**
     * @return class-string<SomethingInterface>
     */
    public function getSomethingClass(): string
    {
        return Something::class;
    }
}

$factory = new Factory();
$entityManager->getRepository($factory->getSomethingClass())->find(1);

Will result in:

Internal error: Internal error: Class 'SomethingInterface' does not exist in file xxx
     Run PHPStan with --debug option and post the stack trace to:
     https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
     Child process error (exit code 1):

With debug:

Uncaught Doctrine\Persistence\Mapping\MappingException: Class 'SomethingInterface' does not exist in vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:94

@ondrejmirtes
Copy link
Member

This exception might already be prevented in phpstan-doctrine 1.2.6. If it still throws for you, then try phpstan-doctrine dev-master. Thanks :)

@Marmelatze
Copy link
Author

It work with 1.2.6 but only if using the default repository methods (find, findBy). Custom repository methods are not recognized

@ondrejmirtes
Copy link
Member

@Marmelatze Have you set your base repository class per https://github.com/phpstan/phpstan-doctrine#configuration ?

@Marmelatze
Copy link
Author

Yes. Maybe my problem is somewhat special: I have some Entities that implement one interface. All these entities have a repository that share a common Interface. phpstan-doctrine will not recognize calls to $entityManager->getRepository($interfacedEntity)->specialMethod(). Here all repositories for entities that implement the entity interface also implement an interface that has specialMethod.

I worked around this by specifying a type alias in phpstan.neon that contains all entites that implement said interface.

@ondrejmirtes
Copy link
Member

Alright ✌️

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.

3 participants