Skip to content

Commit a2e38e9

Browse files
committed
Simplify ExtbasePersistenceCompletionContributor
1 parent 1666e91 commit a2e38e9

File tree

2 files changed

+89
-97
lines changed

2 files changed

+89
-97
lines changed

src/main/java/com/cedricziel/idea/typo3/extbase/ExtbaseUtils.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
import com.intellij.psi.PsiElement;
55
import com.jetbrains.php.PhpClassHierarchyUtils;
66
import com.jetbrains.php.PhpIndex;
7+
import com.jetbrains.php.lang.psi.elements.Field;
8+
import com.jetbrains.php.lang.psi.elements.Method;
79
import com.jetbrains.php.lang.psi.elements.PhpClass;
810
import com.jetbrains.php.lang.psi.elements.PhpClassMember;
911
import org.jetbrains.annotations.NotNull;
1012
import org.jetbrains.annotations.Nullable;
1113

14+
import java.util.Arrays;
1215
import java.util.Collection;
1316
import java.util.Iterator;
1417

1518
import static com.cedricziel.idea.typo3.extbase.persistence.ExtbasePersistenceCompletionContributor.ExtbaseRepositoryMagicMethodsCompletionProvider.TYPO3_CMS_EXTBASE_PERSISTENCE_REPOSITORY;
1619

1720
public class ExtbaseUtils {
18-
public static final String TYPO3_CMS_EXTBASE_DOMAIN_OBJECT_ABSTRACT_ENTITY = "TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity";
21+
public static final String EXTBASE_ABSTRACT_ENTITY_FQN = "TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity";
22+
23+
public static String EXTBASE_QUERY_INTERFACE_FQN = "TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface";
24+
25+
private static final String OBJECT_STORAGE_FQN = "TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage";
1926

2027
public static final String[] NON_QUERYABLE_ENTITY_FIELDS = {
2128
"_isClone",
@@ -46,13 +53,31 @@ public static boolean isRepositoryClass(@NotNull PhpClass phpClass) {
4653
return PhpClassHierarchyUtils.isSuperClass(baseRepositoryClass, phpClass, true);
4754
}
4855

56+
public static boolean isObjectStorage(@NotNull Field field) {
57+
return field.getDeclaredType().toString().contains(OBJECT_STORAGE_FQN);
58+
}
59+
60+
public static boolean isNonQueryableField(@NotNull String name) {
61+
return Arrays.asList(ExtbaseUtils.NON_QUERYABLE_ENTITY_FIELDS).contains(name);
62+
}
63+
64+
public static boolean fieldHasMagicFinders(@NotNull Field field) {
65+
return !field.isConstant() && !field.isInternal() && !ExtbaseUtils.isNonQueryableField(field.getName()) && !ExtbaseUtils.isObjectStorage(field);
66+
}
67+
68+
public static boolean methodInstanceOf(@NotNull String className, @NotNull Method method) {
69+
PhpClass containingClass = method.getContainingClass();
70+
71+
return containingClass != null && containingClass.getPresentableFQN().equals(className);
72+
}
73+
4974
private boolean isEntityClass(PsiElement psiElement) {
5075
PhpClass containingClass = ((PhpClassMember) psiElement).getContainingClass();
5176
if (containingClass == null) {
5277
return false;
5378
}
5479

55-
Collection<PhpClass> classesByFQN = PhpIndex.getInstance(psiElement.getProject()).getClassesByFQN(TYPO3_CMS_EXTBASE_DOMAIN_OBJECT_ABSTRACT_ENTITY);
80+
Collection<PhpClass> classesByFQN = PhpIndex.getInstance(psiElement.getProject()).getClassesByFQN(EXTBASE_ABSTRACT_ENTITY_FQN);
5681
if (classesByFQN.isEmpty()) {
5782
return false;
5883
}

src/main/java/com/cedricziel/idea/typo3/extbase/persistence/ExtbasePersistenceCompletionContributor.java

Lines changed: 62 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.intellij.psi.PsiElement;
1111
import com.intellij.psi.util.PsiTreeUtil;
1212
import com.intellij.util.ProcessingContext;
13-
import com.jetbrains.php.PhpClassHierarchyUtils;
1413
import com.jetbrains.php.PhpIcons;
1514
import com.jetbrains.php.PhpIndex;
1615
import com.jetbrains.php.completion.PhpLookupElement;
@@ -22,7 +21,6 @@
2221

2322
import java.util.Arrays;
2423
import java.util.Collection;
25-
import java.util.Iterator;
2624

2725
public class ExtbasePersistenceCompletionContributor extends CompletionContributor {
2826
public ExtbasePersistenceCompletionContributor() {
@@ -34,10 +32,6 @@ public static class ExtbaseRepositoryMagicMethodsCompletionProvider extends Comp
3432

3533
public static final String TYPO3_CMS_EXTBASE_PERSISTENCE_REPOSITORY = "TYPO3\\CMS\\Extbase\\Persistence\\Repository";
3634
private static final String QUERY_RESULT_INTERFACE = "TYPO3\\CMS\\Extbase\\Persistence\\QueryResultInterface";
37-
private static final String OBJECT_STORAGE = "TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage";
38-
39-
private ExtbaseRepositoryMagicMethodsCompletionProvider() {
40-
}
4135

4236
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
4337
PsiElement position = parameters.getPosition().getOriginalElement();
@@ -46,124 +40,98 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
4640
return;
4741
}
4842

49-
Iterator<PhpClass> iterator = PhpIndex.getInstance(position.getProject()).getClassesByFQN(TYPO3_CMS_EXTBASE_PERSISTENCE_REPOSITORY).iterator();
50-
if (!iterator.hasNext()) {
51-
return;
52-
}
53-
54-
PhpClass repositoryClass = iterator.next();
55-
if (repositoryClass == null) {
56-
return;
57-
}
58-
5943
if (!(position.getParent().getFirstChild() instanceof Variable) && !(position.getParent().getFirstChild() instanceof FieldReference)) {
6044
return;
6145
}
6246

63-
PhpTypedElement variable;
64-
if (position.getParent().getFirstChild() instanceof Variable) {
65-
variable = (PhpTypedElement) position.getParent().getFirstChild();
66-
} else {
67-
variable = (PhpTypedElement) position.getParent().getFirstChild();
68-
}
47+
PhpTypedElement variable = (PhpTypedElement) position.getParent().getFirstChild();
6948

7049
PhpType type = variable.getType();
7150
type.getTypes().forEach(t -> {
7251
Collection<PhpClass> classesByFQN = PhpIndex.getInstance(position.getProject()).getClassesByFQN(t);
73-
classesByFQN.forEach(c -> {
74-
if (PhpClassHierarchyUtils.isSuperClass(repositoryClass, c, true)) {
75-
createLookupElementsForRepository(variable, c, result);
76-
}
77-
});
52+
classesByFQN
53+
.stream()
54+
.filter(ExtbaseUtils::isRepositoryClass)
55+
.forEach(c -> createLookupElementsForRepository(variable, c, result));
7856
});
7957
}
8058

8159
private void createLookupElementsForRepository(@NotNull PhpTypedElement position, @NotNull PhpClass repositoryClass, @NotNull CompletionResultSet result) {
82-
String repositoryClassFqn = repositoryClass.getFQN();
83-
84-
String potentialModelClass = ExtbaseUtility.convertRepositoryFQNToEntityFQN(repositoryClassFqn);
60+
String potentialModelClass = ExtbaseUtility.convertRepositoryFQNToEntityFQN(repositoryClass.getFQN());
8561

8662
Collection<PhpClass> classesByFQN = PhpIndex.getInstance(position.getProject()).getClassesByFQN(potentialModelClass);
8763
classesByFQN.forEach(c -> {
8864
c.getFields().forEach(f -> {
89-
if (f.isConstant() || f.isInternal() || f.getName().equals("_cleanProperties") || f.getName().equals("_isClone")) {
65+
if (!ExtbaseUtils.fieldHasMagicFinders(f)) {
9066
return;
9167
}
9268

9369
// findBy* matches on single fields only and not on collections
94-
if (!f.getDeclaredType().toString().contains(OBJECT_STORAGE)) {
95-
result.addElement(new LookupElement() {
96-
@NotNull
97-
@Override
98-
public String getLookupString() {
99-
return "findBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
100-
}
101-
102-
@Override
103-
public void renderElement(LookupElementPresentation presentation) {
104-
super.renderElement(presentation);
105-
106-
presentation.setItemText("findBy" + StringUtils.capitalize(f.getName()));
107-
presentation.setTypeText("findBy" + StringUtils.capitalize(f.getName()));
108-
presentation.setIcon(PhpIcons.METHOD_ICON);
109-
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
110-
presentation.setTypeText(c.getName() + "[]|" + QUERY_RESULT_INTERFACE);
111-
}
112-
});
113-
}
70+
result.addElement(new LookupElement() {
71+
@NotNull
72+
@Override
73+
public String getLookupString() {
74+
return "findBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
75+
}
76+
77+
@Override
78+
public void renderElement(LookupElementPresentation presentation) {
79+
super.renderElement(presentation);
80+
81+
presentation.setItemText("findBy" + StringUtils.capitalize(f.getName()));
82+
presentation.setTypeText("findBy" + StringUtils.capitalize(f.getName()));
83+
presentation.setIcon(PhpIcons.METHOD_ICON);
84+
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
85+
presentation.setTypeText(c.getName() + "[]|" + QUERY_RESULT_INTERFACE);
86+
}
87+
});
11488

11589
// countBy* matches on single fields only and not on collections
116-
if (!f.getDeclaredType().toString().contains(OBJECT_STORAGE)) {
117-
result.addElement(new LookupElement() {
118-
@NotNull
119-
@Override
120-
public String getLookupString() {
121-
return "countBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
122-
}
123-
124-
@Override
125-
public void renderElement(LookupElementPresentation presentation) {
126-
super.renderElement(presentation);
127-
128-
presentation.setItemText("countBy" + StringUtils.capitalize(f.getName()));
129-
presentation.setTypeText("countBy" + StringUtils.capitalize(f.getName()));
130-
presentation.setIcon(PhpIcons.METHOD_ICON);
131-
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
132-
presentation.setTypeText("int");
133-
}
134-
});
135-
}
90+
result.addElement(new LookupElement() {
91+
@NotNull
92+
@Override
93+
public String getLookupString() {
94+
return "countBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
95+
}
96+
97+
@Override
98+
public void renderElement(LookupElementPresentation presentation) {
99+
super.renderElement(presentation);
100+
101+
presentation.setItemText("countBy" + StringUtils.capitalize(f.getName()));
102+
presentation.setTypeText("countBy" + StringUtils.capitalize(f.getName()));
103+
presentation.setIcon(PhpIcons.METHOD_ICON);
104+
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
105+
presentation.setTypeText("int");
106+
}
107+
});
136108

137109
// findOneBy* matches on single fields only and not on collections
138-
if (!f.getDeclaredType().toString().contains(OBJECT_STORAGE)) {
139-
result.addElement(new LookupElement() {
140-
@NotNull
141-
@Override
142-
public String getLookupString() {
143-
return "findOneBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
144-
}
145-
146-
@Override
147-
public void renderElement(LookupElementPresentation presentation) {
148-
super.renderElement(presentation);
149-
150-
presentation.setItemText("findOneBy" + StringUtils.capitalize(f.getName()));
151-
presentation.setTypeText("findOneBy" + StringUtils.capitalize(f.getName()));
152-
presentation.setIcon(PhpIcons.METHOD_ICON);
153-
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
154-
presentation.setTypeText("null|" + c.getName());
155-
}
156-
});
157-
}
110+
result.addElement(new LookupElement() {
111+
@NotNull
112+
@Override
113+
public String getLookupString() {
114+
return "findOneBy" + StringUtils.capitalize(f.getName()) + "($" + f.getName() + ")";
115+
}
116+
117+
@Override
118+
public void renderElement(LookupElementPresentation presentation) {
119+
super.renderElement(presentation);
120+
121+
presentation.setItemText("findOneBy" + StringUtils.capitalize(f.getName()));
122+
presentation.setTypeText("findOneBy" + StringUtils.capitalize(f.getName()));
123+
presentation.setIcon(PhpIcons.METHOD_ICON);
124+
presentation.setTailText("(" + f.getName() + " : " + f.getDeclaredType() + ")", true);
125+
presentation.setTypeText("null|" + c.getName());
126+
}
127+
});
158128
});
159129
});
160130
}
161131
}
162132

163133
public static class ExtbaseQueryBuilderCompletionProvider extends CompletionProvider<CompletionParameters> {
164134

165-
public static String QUERY_BUILDER = "TYPO3\\CMS\\Extbase\\Persistence\\QueryInterface";
166-
167135
public static String[] QUERY_BUILDER_METHODS = {
168136
"equals",
169137
"like",
@@ -201,8 +169,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
201169
return;
202170
}
203171

204-
PhpClass containingClass = method.getContainingClass();
205-
if (!containingClass.getPresentableFQN().equals(QUERY_BUILDER)) {
172+
if (!ExtbaseUtils.methodInstanceOf(ExtbaseUtils.EXTBASE_QUERY_INTERFACE_FQN, method)) {
206173
return;
207174
}
208175

0 commit comments

Comments
 (0)