Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom getRepository()-like methods #1276

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider3;
import fr.adrienbrault.idea.symfony2plugin.Settings;
Expand All @@ -29,6 +26,8 @@ public class ObjectRepositoryTypeProvider implements PhpTypeProvider3 {
new MethodMatcher.CallToSignature("\\Doctrine\\Common\\Persistence\\ObjectManager", "getRepository"),
};

private static String repositoryClass = "\\Doctrine\\Common\\Persistence\\ObjectRepository";

final public static char TRIM_KEY = '\u0185';

@Override
Expand All @@ -43,11 +42,10 @@ public PhpType getType(PsiElement e) {
return null;
}

if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getRepository")) {
if(!(e instanceof MethodReference)) {
return null;
}


String refSignature = ((MethodReference)e).getSignature();
if(StringUtil.isEmpty(refSignature)) {
return null;
Expand Down Expand Up @@ -80,7 +78,13 @@ public Collection<? extends PhpNamedElement> getBySignature(String expression, S
return phpNamedElementCollections;
}

if (!PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, GET_REPOSITORIES_SIGNATURES)) {
PhpReturnType returnType = ((Method) phpNamedElement).getReturnType();
String returnTypeName = returnType == null ? null : returnType.getType().toString();

boolean isMethod = PhpElementsUtil.isMethodInstanceOf((Method) phpNamedElement, GET_REPOSITORIES_SIGNATURES);
boolean isReturnType = returnType != null && PhpElementsUtil.isInstanceOf(project, returnTypeName, repositoryClass);

if (!isMethod && !isReturnType) {
return phpNamedElementCollections;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void testGetRepositoryResolveByRepository() {
"$em->getRepository(\\Foo\\Bar::class)->b<caret>ar();",
PlatformPatterns.psiElement(Method.class).withName("bar")
);

}

/**
Expand All @@ -61,6 +60,55 @@ public void testGetRepositoryResolveByRepositoryApiClassConstantCompatibility()
);
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider
*/
public void testGetRepositoryResolveByReturnTypeMethod() {
assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Foo\\BarController $cont */\n" +
"$cont->getRepo('\\Foo\\Bar')->b<caret>ar();",
PlatformPatterns.psiElement(Method.class).withName("bar")
);

assertPhpReferenceSignatureContains(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Foo\\BarController $cont */\n" +
"$cont->getRepo('\\Foo\\Bar')->b<caret>ar();",
"#M#" + '\u0151' + "#M#C\\Foo\\BarController.getRepo" + '\u0185' + "\\Foo\\Bar.bar"
);

assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Foo\\BarController $cont */\n" +
"$cont->getRepo(\\Foo\\Bar::class)->b<caret>ar();",
PlatformPatterns.psiElement(Method.class).withName("bar")
);

}

public void testGetRepositoryNotResolveByWrongMethod() {
assertPhpReferenceNotResolveTo(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Foo\\BarController $cont */\n" +
"$cont->getBoo(\\Foo\\Bar::class)->b<caret>ar();",
PlatformPatterns.psiElement(Method.class).withName("bar")
);
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider
*/
public void testGetRepositoryResolveByReturnTypeMethodApiClassConstantCompatibility() {
String result = "#M#" + '\u0151' + "#M#C\\Foo\\BarController.getRepo" + '\u0185' + "#K#C\\Foo\\Bar.class.bar";

assertPhpReferenceSignatureContains(PhpFileType.INSTANCE, "<?php" +
"/** @var \\Foo\\BarController $cont */\n" +
"$cont->getRepo(\\Foo\\Bar::class)->b<caret>ar();",
result
);
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.ObjectRepositoryTypeProvider
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ function getRepository();
}

namespace Foo {

use Doctrine\Common\Persistence\ObjectRepository;

abstract class Bar implements ObjectRepository {}

class BarRepository {
class BarRepository implements ObjectRepository {
public function bar() {}
}

class WrongClass {}

class BarController {
public function getRepo($name): BarRepository {}
public function getBoo($name): WrongClass {}
}
}

namespace Doctrine\Common\Persistence
Expand Down