Skip to content

Commit

Permalink
ignore Doctrine repository return type provider on magic method patte…
Browse files Browse the repository at this point in the history
…rn if already in repository #1481
  • Loading branch information
Haehnchen committed Jun 14, 2020
1 parent 4c83f16 commit b88ab68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ private static Collection<? extends PhpNamedElement> getTypeSignatureMagic(@NotN
for (String s : signature.split("\\|")) {
int i = s.lastIndexOf(".");
if (i > 0) {
// method already exists in repository use it
if (phpIndex.getBySignature(s, null, 0).size() > 0) {
return Collections.emptyList();
}

String substring = s.substring(i + 1);
if (substring.startsWith("findOneBy")) {
s = s.substring(0, i + 1) + "findOneBy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class ObjectRepositoryResultTypeProviderTest extends SymfonyLightCodeInsightFixtureTestCase {
public void setUp() throws Exception {
super.setUp();
myFixture.copyFileToProject("ObjectRepositoryResultTypeProvider.orm.yml");
myFixture.copyFileToProject("ObjectRepositoryResultTypeProvider.php");
}

Expand Down Expand Up @@ -75,4 +76,22 @@ public void testThatClassAsStringIsResolvedForMagicMethods() {
PlatformPatterns.psiElement(Method.class).withName("getId")
);
}

public void testThatClassAsStringIsResolvedForMagicMethodsButNotWhenAlreadyExists() {
// do nothing at all here; use type from the method it self
assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $om */\n" +
"$om->getRepository('\\Foo\\Bar')->findOneByFancyStuff('foobar')[0]->get<caret>Id();",
PlatformPatterns.psiElement(Method.class).withName("getId")
);

// repository class exists but method is magic
assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
"<?php" +
"/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $om */\n" +
"$om->getRepository('\\Foo\\Bar')->findOneByFancyStuffNotMagic('foobar')->get<caret>Id();",
PlatformPatterns.psiElement(Method.class).withName("getId")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Foo\Bar:
type: entity
repositoryClass: Foo\BarRepository
fields:
name:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace Foo
{
use Doctrine\Common\Persistence\ObjectRepository;

class Bar
{
public function getId()
{
}
}

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

/**
* @return Bar[]
*/
public function findOneByFancyStuff() {}
}
}


Expand Down

0 comments on commit b88ab68

Please sign in to comment.