Skip to content

Commit aedbfdc

Browse files
authored
Merge pull request #1472 from Haehnchen/feature/doctrine-fqn-resolving
Revert some Doctrine class resolving; as its "\" is not needed for a full fqn name
2 parents 3c3823b + e12fb27 commit aedbfdc

File tree

5 files changed

+71
-45
lines changed

5 files changed

+71
-45
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/DoctrineUtil.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.jetbrains.php.lang.psi.PhpFile;
1515
import com.jetbrains.php.lang.psi.elements.PhpClass;
1616
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
17+
import de.espend.idea.php.annotation.util.AnnotationUtil;
1718
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.AnnotationElementWalkingVisitor;
1819
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
1920
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
@@ -26,8 +27,6 @@
2627
import java.util.ArrayList;
2728
import java.util.Collection;
2829
import java.util.Set;
29-
import java.util.regex.Matcher;
30-
import java.util.regex.Pattern;
3130

3231
/**
3332
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -151,28 +150,19 @@ public static String getAnnotationRepositoryClass(@NotNull PhpDocTag phpDocTag,
151150
// @TODO: use annotation plugin
152151
// repositoryClass="Foobar"
153152
String text = phpDocAttributeList.getText();
154-
Matcher[] matches = new Matcher[] {
155-
Pattern.compile("repositoryClass\\s*=\\s*\"([^\"]*)\"").matcher(text), // repositoryClass="Foobar"
156-
Pattern.compile("repositoryClass\\s*=\\s*([^\\s:]*)::class").matcher(text), // repositoryClass=Foobar::class
157-
};
158153

159-
for (Matcher matcher : matches) {
160-
if (!matcher.find()) {
161-
continue;
162-
}
154+
String repositoryClass = EntityHelper.resolveDoctrineLikePropertyClass(
155+
phpClass,
156+
text,
157+
"repositoryClass",
158+
aVoid -> AnnotationUtil.getUseImportMap(phpDocTag)
159+
);
163160

164-
String group = matcher.group(1);
165-
if (StringUtils.isBlank(group)) {
166-
continue;
167-
}
168-
169-
String clazz = EntityHelper.getAnnotationRepositoryClass(phpClass, group);
170-
if(clazz != null) {
171-
return StringUtils.stripStart(clazz, "\\");
172-
}
161+
if (repositoryClass == null) {
162+
return null;
173163
}
174164

175-
return null;
165+
return StringUtils.stripStart(repositoryClass, "\\");
176166
}
177167

178168
/**

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/EntityHelper.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import com.intellij.psi.util.PsiTreeUtil;
1111
import com.intellij.psi.xml.XmlFile;
1212
import com.intellij.psi.xml.XmlTag;
13+
import com.intellij.util.Function;
1314
import com.intellij.util.containers.ContainerUtil;
1415
import com.jetbrains.php.PhpIndex;
1516
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
1617
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocParamTag;
1718
import com.jetbrains.php.lang.psi.elements.*;
18-
import de.espend.idea.php.annotation.dict.PhpDocCommentAnnotation;
1919
import de.espend.idea.php.annotation.util.AnnotationUtil;
2020
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.DocumentNamespacesParser;
2121
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.EntityNamesServiceParser;
@@ -664,26 +664,11 @@ public static void attachAnnotationInformation(@NotNull PhpClass phpClass, @NotN
664664
matcher = Pattern.compile("((Many|One)To(Many|One))\\(").matcher(text);
665665
if (matcher.find()) {
666666
doctrineModelField.setRelationType(matcher.group(1));
667+
String clazz = resolveDoctrineLikePropertyClass(phpClass, text, "targetEntity", aVoid -> useImportMap);
667668

668-
// targetEntity name
669-
Matcher[] matches = new Matcher[] {
670-
Pattern.compile("targetEntity\\s*=\\s*\"([^\"]*)\"").matcher(text), // targetEntity="Foobar"
671-
Pattern.compile("targetEntity\\s*=\\s*([^\\s:]*)::class").matcher(text), // targetEntity=Foobar::class
672-
};
673-
674-
boolean matched = false;
675-
for (Matcher targetEntityMatch : matches) {
676-
if (targetEntityMatch.find()) {
677-
matched = true;
678-
679-
String group = targetEntityMatch.group(1);
680-
if (org.apache.commons.lang.StringUtils.isNotBlank(group)) {
681-
doctrineModelField.setRelation(AnnotationBackportUtil.getFqnClassNameFromScope(phpClass, group, useImportMap));
682-
}
683-
}
684-
}
685-
686-
if (!matched) {
669+
if (clazz != null) {
670+
doctrineModelField.setRelation(clazz);
671+
} else {
687672
// @TODO: external split
688673
// FLOW shortcut:
689674
// @var "\DateTime" is targetEntity
@@ -814,4 +799,40 @@ public static Map<String, String> getWeakBundleNamespaces(Project project, Map<S
814799
return missingMap;
815800
}
816801

802+
/**
803+
* Resolve class instances from annotation based on the class context.
804+
*
805+
* 'repositoryClass="Foo"', 'repostoryClass=Foo::class'
806+
*
807+
* - "Foo\Bar" is resolved as a "\Foo\Bar" on Doctrine
808+
* - "Bar" append to the namespace name
809+
*/
810+
public static String resolveDoctrineLikePropertyClass(@NotNull PhpClass phpClass, @NotNull String text, @NotNull String propertyName, @NotNull Function<Void, Map<String, String>> useImportMap) {
811+
Map<String, Matcher> matches = new HashMap<String, Matcher>() {{
812+
put("string", Pattern.compile(propertyName + "\\s*=\\s*\"([^\"]*)\"").matcher(text)); // targetEntity="Foobar"
813+
put("class", Pattern.compile(propertyName + "\\s*=\\s*([^\\s:]*)::class").matcher(text)); // targetEntity=Foobar::class
814+
}};
815+
816+
for (Map.Entry<String, Matcher> pair : matches.entrySet()) {
817+
Matcher targetEntityMatch = pair.getValue();
818+
if (!targetEntityMatch.find()) {
819+
continue;
820+
}
821+
822+
String targetEntity = targetEntityMatch.group(1);
823+
if (org.apache.commons.lang.StringUtils.isBlank(targetEntity)) {
824+
continue;
825+
}
826+
827+
if ("class".equals(pair.getKey())) {
828+
return AnnotationBackportUtil.getFqnClassNameFromScope(phpClass, targetEntity, useImportMap.fun(null));
829+
}
830+
831+
return targetEntity.contains("\\")
832+
? targetEntity // "Foo\Bar" is resolved as a "\Foo\Bar" on Doctrine
833+
: phpClass.getNamespaceName() + targetEntity; // "Bar" append to the namespace name
834+
}
835+
836+
return null;
837+
}
817838
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/DoctrineUtilTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void testGetClassRepositoryPairForStringValue() {
3636
Pair<String, String> next = classRepositoryPair.iterator().next();
3737

3838
assertEquals("Foo\\Apple", next.getFirst());
39-
assertEquals("Foo\\MyBundle\\Entity\\Repository\\AddressRepository", next.getSecond());
39+
assertEquals("MyBundle\\Entity\\Repository\\AddressRepository", next.getSecond());
4040
}
4141

4242
/**
@@ -104,7 +104,11 @@ public void testGetClassRepositoryPairForClassConstanta() {
104104
"/**\n" +
105105
" * @ORM\\Entity(repositoryClass=\"BarAlias\\Foobar\")\n" +
106106
" */\n" +
107-
"class Black {}\n"
107+
"class Black {}\n" +
108+
"/**\n" +
109+
" * @ORM\\Entity(repositoryClass=\"Foobar\")\n" +
110+
" */\n" +
111+
"class White {}\n"
108112
);
109113

110114
Collection<Pair<String, String>> classRepositoryPair = DoctrineUtil.getClassRepositoryPair(psiFileFromText);
@@ -119,6 +123,9 @@ public void testGetClassRepositoryPairForClassConstanta() {
119123
assertEquals("Bar\\Foobar", yellow.getSecond());
120124

121125
Pair<String, String> black = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\Black".equals(stringStringPair.getFirst())).findFirst().get();
122-
assertEquals("Bar\\Foobar", yellow.getSecond());
126+
assertEquals("BarAlias\\Foobar", black.getSecond());
127+
128+
Pair<String, String> white = classRepositoryPair.stream().filter(stringStringPair -> "Foo\\White".equals(stringStringPair.getFirst())).findFirst().get();
129+
assertEquals("Foo\\Foobar", white.getSecond());
123130
}
124131
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/metadata/driver/DoctrinePhpMappingDriverTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ public void testPhpAnnotationsMetadata() {
4343
assertEquals("\\Doctrine\\Orm\\Apple", metadata.getField("apple").getRelation());
4444

4545
assertEquals("ManyToMany", metadata.getField("egg").getRelationType());
46-
assertEquals("\\Doctrine\\Egg", metadata.getField("egg").getRelation());
46+
assertEquals("\\Doctrine\\Orm\\Egg", metadata.getField("egg").getRelation());
4747

4848
assertEquals("ManyToMany", metadata.getField("egg").getRelationType());
49-
assertEquals("\\Doctrine\\Egg", metadata.getField("egg").getRelation());
49+
assertEquals("\\Doctrine\\Orm\\Egg", metadata.getField("egg").getRelation());
5050

5151
assertEquals("ManyToMany", metadata.getField("eggClass").getRelationType());
5252
assertEquals("\\Doctrine\\Egg", metadata.getField("eggClass").getRelation());
5353

5454
assertEquals("ManyToMany", metadata.getField("eggSelfAlias").getRelationType());
5555
assertEquals("\\Doctrine\\Egg", metadata.getField("eggSelfAlias").getRelation());
5656

57+
assertEquals("ManyToMany", metadata.getField("eggSelfAliasFooBar").getRelationType());
58+
assertEquals("\\Doctrine\\Egg\\Foo\\Bar", metadata.getField("eggSelfAliasFooBar").getRelation());
59+
5760
assertEquals("ManyToOne", metadata.getField("appleTrait").getRelationType());
5861
assertEquals("\\Doctrine\\Apple", metadata.getField("appleTrait").getRelation());
5962

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/metadata/driver/fixtures/classes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class Annotation extends EntityExtends {
9494
* @ORM\ManyToMany(targetEntity=SelfAlias::class)
9595
*/
9696
public $eggSelfAlias;
97+
98+
/**
99+
* @ORM\ManyToMany(targetEntity=SelfAlias\Foo\Bar::class)
100+
*/
101+
public $eggSelfAliasFooBar;
97102
};
98103
}
99104

0 commit comments

Comments
 (0)