idea-php-symfony2-plugin icon indicating copy to clipboard operation
idea-php-symfony2-plugin copied to clipboard

Allow custom getRepository()-like methods

Open theredled opened this issue 6 years ago • 2 comments

getRepository() currently only support direct calls to ManagerRegistry::getRepository or ObjectManager::getRepository. This PR supports other methods - typically shortcuts in Controllers - as soon as they use ObjectRepository or a child class as a PHP code return type.

Note : seems hard to support PHPDoc.

Example :

Class MyClass {
  public function getRepo($class): ObjectRepository {}
}

$c = new MyClass();
$c->getRepo(MyEntity::class)-> // Gives access to MyEntity repository methods

theredled avatar Jan 20 '19 02:01 theredled

I'm only wondering about that part in ObjectRepositoryTypeProvider :

public PhpType getType(PsiElement e) {
// ...
        if(!(e instanceof MethodReference)
                /*|| !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getRepository") */
        ) {
            return null;
        }
// ...
}

Maybe removing that commented line as I did is too permissive and can cause performance issues? If so, we could narrow the matching methods to "get*" for example...

theredled avatar Jan 20 '19 03:01 theredled

@theredled yes this mentioned method check is for performance and also to not overflow the phpstorm signatures for every method. By removing this simply we are saying we are responsible for all methods.

We need some filtering here. But for now i just see to support it via a configuration.

Another way would be by using the "php7 returns" and index every file to get "getRepo" automaticcally. But as the index is build inside "getType" we dont have access to it at this point. So this must then be cached. So would be a bigger task.

Haehnchen avatar Jan 21 '19 14:01 Haehnchen