Skip to content

Commit

Permalink
given "FormInterface" variable inside template render are considered …
Browse files Browse the repository at this point in the history
…as FormView
  • Loading branch information
Haehnchen committed Jun 24, 2023
1 parent 8bac4fd commit d49e21e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,9 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {

Project project = completionParameters.getPosition().getProject();

Collection<PsiVariable> psiVariables = new ArrayList<>();
for (Map.Entry<String, PsiVariable> entry : TwigTypeResolveUtil.collectScopeVariables(originalPosition).entrySet()) {
PhpType phpType = PhpIndex.getInstance(project).completeType(project, PhpType.from(entry.getValue().getTypes().toArray(new String[0])), new HashSet<>());
if (phpType.types().noneMatch(s -> s.equals("\\Symfony\\Component\\Form\\FormView"))) {
if (!FormFieldResolver.isFormView(phpType)) {
continue;
}

Expand All @@ -1017,8 +1016,6 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
resultSet.addElement(element);
}
});

psiVariables.add(entry.getValue());
}
}
}
Expand Down Expand Up @@ -1049,7 +1046,7 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
Project project = completionParameters.getPosition().getProject();
for (Map.Entry<String, PsiVariable> entry : TwigTypeResolveUtil.collectScopeVariables(originalPosition).entrySet()) {
PhpType phpType = PhpIndex.getInstance(project).completeType(project, PhpType.from(entry.getValue().getTypes().toArray(new String[0])), new HashSet<>());
if (phpType.types().noneMatch(s -> s.equals("\\Symfony\\Component\\Form\\FormView"))) {
if (!FormFieldResolver.isFormView(phpType)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
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.Variable;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import fr.adrienbrault.idea.symfony2plugin.form.util.FormUtil;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigTypeContainer;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
Expand All @@ -18,6 +20,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -32,11 +35,24 @@ public void resolve(Collection<TwigTypeContainer> targets, Collection<TwigTypeCo
}

TwigTypeContainer twigTypeContainer = targets.iterator().next();
if (twigTypeContainer.getPhpNamedElement() instanceof PhpClass phpClass && PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Form\\FormView")) {
if (twigTypeContainer.getPhpNamedElement() instanceof PhpClass phpClass && isFormView(phpClass)) {
visitFormReferencesFields(psiVariables.iterator().next().getElement(), targets::add);
}
}

public static boolean isFormView(@NotNull PhpClass phpClass) {
return PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Form\\FormView") ||
PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Form\\FormInterface"); // form view is create converting by Symfony on template render
}

public static boolean isFormView(@NotNull PhpType phpType) {
return phpType.types()
.anyMatch(s ->
s.equals("\\Symfony\\Component\\Form\\FormView") ||
s.equals("\\Symfony\\Component\\Form\\FormInterface") // form view is create converting by Symfony on template render
);
}

public static Collection<PhpClass> getFormTypeFromFormFactory(@NotNull PsiElement formReference) {
Collection<PhpClass> phpClasses = new ArrayList<>();

Expand All @@ -61,7 +77,24 @@ public static Collection<PhpClass> getFormTypeFromFormFactory(@NotNull PsiElemen
// 'foo2' => $form2 => $form2 = $form->createView() => $this->createForm(new Type();
if (formReference instanceof Variable) {
PsiElement varDecl = ((Variable) formReference).resolve();
if (varDecl instanceof Variable) {


// 'foo2' => $form2;
if (varDecl instanceof Variable variable) {
PhpType type = PhpIndex.getInstance(variable.getProject()).completeType(variable.getProject(), variable.getType(), new HashSet<>());
if (isFormView(type)) {
PsiElement resolve = variable.resolve();
if (resolve != null) {
MethodReference nextSiblingOfType = PsiTreeUtil.getNextSiblingOfType(resolve, MethodReference.class);
if (nextSiblingOfType != null) {
PhpClass phpClass = resolveCall(nextSiblingOfType);
if (phpClass != null) {
phpClasses.add(phpClass);
}
}
}
}

MethodReference methodReference = PsiTreeUtil.getNextSiblingOfType(varDecl, MethodReference.class);
if (methodReference != null) {
PsiElement scopeVar = methodReference.getFirstChild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public void resolve(Collection<TwigTypeContainer> targets, Collection<TwigTypeCo
lastTwigTypeContainer = element;
}

for(TwigTypeContainer twigTypeContainer: lastTwigTypeContainer) {
if(twigTypeContainer.getPhpNamedElement() instanceof PhpClass) {
if(PhpElementsUtil.isInstanceOf((PhpClass) twigTypeContainer.getPhpNamedElement(), "\\Symfony\\Component\\Form\\FormView")) {
for (TwigTypeContainer twigTypeContainer: lastTwigTypeContainer) {
if (twigTypeContainer.getPhpNamedElement() instanceof PhpClass) {
if (FormFieldResolver.isFormView((PhpClass) twigTypeContainer.getPhpNamedElement())) {
attachVars(twigTypeContainer.getPhpNamedElement().getProject(), targets);
}
}

}

}

private void attachVars(Project project, Collection<TwigTypeContainer> targets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,35 @@
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class TwigFormFieldGenerator extends CodeInsightAction {
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
return Symfony2ProjectComponent.isEnabled(project) && (
boolean b = Symfony2ProjectComponent.isEnabled(project) && (
file instanceof TwigFile
|| (file instanceof HtmlFileImpl && file.getName().toLowerCase().endsWith(".twig"))
|| TwigUtil.getInjectedTwigElement(file, editor) != null
);

if (!b) {
return false;
}

PsiElement psiElement = TwigUtil.getInjectedTwigElement(file, editor);
if (psiElement == null) {
return false;
}

return TwigTypeResolveUtil.collectScopeVariables(psiElement).entrySet()
.stream()
.anyMatch(entry -> FormFieldResolver.isFormView(PhpIndex.getInstance(project).completeType(project, PhpType.from(entry.getValue().getTypes().toArray(new String[0])), new HashSet<>())));
}

protected CodeInsightActionHandler getHandler() {
Expand All @@ -53,7 +69,7 @@ public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Ps

for (Map.Entry<String, PsiVariable> entry : TwigTypeResolveUtil.collectScopeVariables(psiElement).entrySet()) {
PhpType phpType = PhpIndex.getInstance(project).completeType(project, PhpType.from(entry.getValue().getTypes().toArray(new String[0])), new HashSet<>());
if (phpType.types().noneMatch(s -> s.equals("\\Symfony\\Component\\Form\\FormView"))) {
if (!FormFieldResolver.isFormView(phpType)) {
continue;
}

Expand Down

0 comments on commit d49e21e

Please sign in to comment.