Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
using isInstanceOf to check of Repository classes
  • Loading branch information
Haehnchen committed Apr 30, 2013
commit dec9b34f170adb2a305310bdd24f238ec4eaabe4
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected boolean isImplementationOfInterface(PhpClass phpClass, PhpClass phpInt
return isImplementationOfInterface(phpClass.getSuperClass(), phpInterface);
}

protected boolean isInstanceOf(PhpClass subjectClass, PhpClass expectedClass) {
public boolean isInstanceOf(PhpClass subjectClass, PhpClass expectedClass) {
if (subjectClass == expectedClass) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.elements.*;
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineEntityLookupElement;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -48,13 +47,20 @@ public Object[] getVariants() {
PhpIndex phpIndex = PhpIndex.getInstance(getElement().getProject());

Symfony2ProjectComponent symfony2ProjectComponent = getElement().getProject().getComponent(Symfony2ProjectComponent.class);
Map<String, String> em = symfony2ProjectComponent.getEntityNamespacesMap();
Map<String, String> entityNamespaces = symfony2ProjectComponent.getEntityNamespacesMap();

List<LookupElement> results = new ArrayList<LookupElement>();
for (String shortcutName : em.keySet()) {

// find Repository interface to filter RepositoryClasses out
PhpClass repositoryClass = getRepositoryClass(phpIndex);
if(null == repositoryClass) {
return results.toArray();
}

for (Map.Entry<String, String> entry : entityNamespaces.entrySet()) {

// search for classes that match the symfony2 namings
Collection<PhpNamespace> entities = phpIndex.getNamespacesByName(em.get(shortcutName));
Collection<PhpNamespace> entities = phpIndex.getNamespacesByName(entry.getValue());

// @TODO: it looks like PhpIndex cant search for classes like \ns\Path\*\...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing / with \ should work, right ?

// temporary only use flat entities and dont support "MyBundle:Folder\Entity"
Expand All @@ -63,22 +69,41 @@ public Object[] getVariants() {
// build our symfony2 shortcut
String filename = entity_files.getContainingFile().getName();
String className = filename.substring(0, filename.lastIndexOf('.'));
String repoName = shortcutName + ':' + className;
String repoName = entry.getKey() + ':' + className;

// dont add Repository classes and abstract entities
if(!className.endsWith("Repository") && !className.equals("Repository")) {
for (PhpClass entityClass : phpIndex.getClassesByFQN(em.get(shortcutName) + "\\" + className)) {
if(!entityClass.isAbstract()) {
results.add(new DoctrineEntityLookupElement(repoName, entityClass));
}
}
PhpClass entityClass = getClass(phpIndex, entityNamespaces.get(entry.getKey()) + "\\" + className);
if(null != entityClass && isEntity(entityClass, repositoryClass)) {
results.add(new DoctrineEntityLookupElement(repoName, entityClass));
}

}
}

}

return results.toArray();
}

@Nullable
protected PhpClass getRepositoryClass(PhpIndex phpIndex) {
Collection<PhpClass> classes = phpIndex.getInterfacesByFQN("\\Doctrine\\Common\\Persistence\\ObjectRepository");
return classes.isEmpty() ? null : classes.iterator().next();
}

@Nullable
protected PhpClass getClass(PhpIndex phpIndex, String className) {
Collection<PhpClass> classes = phpIndex.getClassesByFQN(className);
return classes.isEmpty() ? null : classes.iterator().next();
}

protected boolean isEntity(PhpClass entityClass, PhpClass repositoryClass) {

if(entityClass.isAbstract()) {
return false;
}

Symfony2InterfacesUtil symfony2Util = new Symfony2InterfacesUtil();
return !symfony2Util.isInstanceOf(entityClass, repositoryClass);
}

}