Skip to content

Commit c57327b

Browse files
committed
fix form "csrf_protection" was not found because of Symfony 3.0 interface drop; add static "FormType" fallback and visit method "setDefaultOptions", "configureOptions" for extension key
1 parent 49fc314 commit c57327b

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

src/fr/adrienbrault/idea/symfony2plugin/form/FormExtensionKeyReference.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
public class FormExtensionKeyReference extends PsiReferenceBase<PsiElement> implements PsiReference {
1717

1818
private StringLiteralExpression element;
19-
private String[] formTypes = new String[] { "form" };
19+
20+
private String[] formTypes = new String[] {
21+
"form",
22+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType",
23+
};
2024

2125
public FormExtensionKeyReference(@NotNull StringLiteralExpression element) {
2226
super(element);
2327
this.element = element;
2428
}
2529

26-
public FormExtensionKeyReference(@NotNull StringLiteralExpression element, String... formTypes) {
27-
this(element);
28-
this.formTypes = formTypes;
29-
}
30-
3130
@Nullable
3231
@Override
3332
public PsiElement resolve() {

src/fr/adrienbrault/idea/symfony2plugin/form/FormTypeReferenceContributor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
189189

190190
);
191191

192-
// FormBuilderInterface::add('underscore_method')
192+
// TODO: migrate to FormGotoCompletionRegistrar for better performance as lazy condition
193+
// $resolver->setDefaults(['csrf_protection<caret>' => 'foobar']);
193194
psiReferenceRegistrar.registerReferenceProvider(
194195
PlatformPatterns.psiElement(StringLiteralExpression.class),
195196
new PsiReferenceProvider() {
@@ -210,8 +211,11 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
210211
}
211212

212213
MethodReference method = (MethodReference) parameterList.getContext();
213-
Symfony2InterfacesUtil interfacesUtil = new Symfony2InterfacesUtil();
214-
if (!interfacesUtil.isCallTo(method, "\\Symfony\\Component\\OptionsResolver\\OptionsResolverInterface", "setDefaults")) {
214+
215+
// Symfony 2 and 3 BC fix
216+
if(!(PhpElementsUtil.isMethodReferenceInstanceOf(method, "\\Symfony\\Component\\OptionsResolver\\OptionsResolverInterface", "setDefaults") ||
217+
PhpElementsUtil.isMethodReferenceInstanceOf(method, "\\Symfony\\Component\\OptionsResolver\\OptionsResolver", "setDefaults"))
218+
) {
215219
return new PsiReference[0];
216220
}
217221

src/fr/adrienbrault/idea/symfony2plugin/form/util/FormOptionsUtil.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,28 +354,31 @@ public static LookupElement getOptionLookupElement(FormOption formOption) {
354354
}
355355

356356
@NotNull
357-
public static Collection<PsiElement> getFormExtensionsKeysTargets(StringLiteralExpression psiElement, String... formTypes) {
358-
Map<String, FormOption> test = FormOptionsUtil.getFormExtensionKeys(psiElement.getProject(), formTypes);
357+
public static Collection<PsiElement> getFormExtensionsKeysTargets(@NotNull StringLiteralExpression psiElement, String... formTypes) {
358+
Map<String, FormOption> extensionKeys = FormOptionsUtil.getFormExtensionKeys(psiElement.getProject(), formTypes);
359359
String value = psiElement.getContents();
360360

361-
if(!test.containsKey(value)) {
361+
if(!extensionKeys.containsKey(value)) {
362362
return Collections.emptyList();
363363
}
364364

365-
// @TODO: use core method find method
366-
String className = test.get(value).getFormClass().getPhpClass().getPresentableFQN();
365+
Collection<PsiElement> psiElements = new HashSet<>();
366+
PhpClass phpClass = extensionKeys.get(value).getFormClass().getPhpClass();
367367

368-
PsiElement[] psiElements = PhpElementsUtil.getPsiElementsBySignature(psiElement.getProject(), "#M#C\\" + className + ".setDefaultOptions");
369-
if(psiElements.length == 0) {
370-
return Collections.emptyList();
371-
}
368+
// Symfony <= 2.7 and > 2.7 api level search
369+
for (String methodName : new String[]{"setDefaultOptions", "configureOptions"}) {
370+
Method method = phpClass.findMethodByName(methodName);
371+
if(method == null) {
372+
continue;
373+
}
372374

373-
PsiElement keyValue = PhpElementsUtil.findArrayKeyValueInsideReference(psiElements[0], "setDefaults", value);
374-
if(keyValue != null) {
375-
return Arrays.asList(keyValue);
375+
ContainerUtil.addIfNotNull(
376+
psiElements,
377+
PhpElementsUtil.findArrayKeyValueInsideReference(method, "setDefaults", value)
378+
);
376379
}
377380

378-
return Collections.emptyList();
381+
return psiElements;
379382
}
380383

381384
public static Collection<LookupElement> getFormExtensionKeysLookupElements(Project project, String... formTypes) {

tests/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormOptionsUtilTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.intellij.psi.PsiElement;
44
import com.intellij.util.containers.ContainerUtil;
55
import com.jetbrains.php.lang.PhpFileType;
6+
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
7+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
68
import fr.adrienbrault.idea.symfony2plugin.form.dict.FormClass;
79
import fr.adrienbrault.idea.symfony2plugin.form.dict.FormClassEnum;
810
import fr.adrienbrault.idea.symfony2plugin.form.dict.FormOptionEnum;
@@ -12,6 +14,7 @@
1214
import org.jetbrains.annotations.NotNull;
1315

1416
import java.io.File;
17+
import java.util.Collection;
1518
import java.util.HashSet;
1619
import java.util.Set;
1720

@@ -77,12 +80,9 @@ public void testClassOptionsVisitorWithExtensionAndParents() {
7780

7881
final Set<String> options = new HashSet<String>();
7982

80-
FormOptionsUtil.visitFormOptions(getProject(), "foo", new FormOptionVisitor() {
81-
@Override
82-
public void visit(@NotNull PsiElement psiElement, @NotNull String option, @NotNull FormClass formClass, @NotNull FormOptionEnum optionEnum) {
83-
options.add(option);
84-
}
85-
});
83+
FormOptionsUtil.visitFormOptions(getProject(), "foo", (psiElement, option, formClass, optionEnum) ->
84+
options.add(option)
85+
);
8686

8787
assertContainsElements(options, "MyType", "BarTypeParent", "BarTypeExtension");
8888
}
@@ -101,4 +101,12 @@ public void visit(@NotNull PsiElement psiElement, @NotNull String option, @NotNu
101101
assertContainsElements(optionsClass, "BarType");
102102
}
103103

104-
}
104+
/**
105+
* @see FormOptionsUtil#getFormExtensionKeys
106+
*/
107+
public void testGetFormExtensionsKeysTargets() {
108+
StringLiteralExpression contents = PhpPsiElementFactory.createFromText(getProject(), StringLiteralExpression.class, "<?php 'BarType';");
109+
Collection<PsiElement> formExtensionsKeysTargets = FormOptionsUtil.getFormExtensionsKeysTargets(contents, "Options\\Bar\\Foobar");
110+
assertTrue(formExtensionsKeysTargets.size() > 0);
111+
}
112+
}

0 commit comments

Comments
 (0)