Skip to content

Commit 649c374

Browse files
committed
add method type hint class importer for events Haehnchen#564
1 parent bbabdbb commit 649c374

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/fr/adrienbrault/idea/symfony2plugin/config/yaml/inspection/EventMethodCallInspection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ public StringBuilder getStringBuilder() {
261261

262262
String parameter = "";
263263
if(taggedEventMethodParameter != null) {
264+
String s = PhpElementsUtil.insertUseIfNecessary(phpClass, taggedEventMethodParameter);
265+
if(s != null) {
266+
taggedEventMethodParameter = s;
267+
}
268+
264269
parameter = taggedEventMethodParameter + " $event";
265270
}
266271

src/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,4 +1383,28 @@ public static PhpClass getMethodTypeHintParameterPhpClass(@NotNull Method method
13831383
return PhpElementsUtil.getClassInterface(method.getProject(), className);
13841384
}
13851385

1386+
@Nullable
1387+
public static String insertUseIfNecessary(@NotNull PsiElement phpClass, @NotNull String fqnClasName) {
1388+
if(!fqnClasName.startsWith("\\")) {
1389+
fqnClasName = "\\" + fqnClasName;
1390+
}
1391+
1392+
PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(phpClass);
1393+
if(scopeForUseOperator == null) {
1394+
return null;
1395+
}
1396+
1397+
if(!PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).values().contains(fqnClasName)) {
1398+
PhpAliasImporter.insertUseStatement(fqnClasName, scopeForUseOperator);
1399+
}
1400+
1401+
for (Map.Entry<String, String> entry : PhpCodeInsightUtil.getAliasesInScope(scopeForUseOperator).entrySet()) {
1402+
if(fqnClasName.equals(entry.getValue())) {
1403+
return entry.getKey();
1404+
}
1405+
}
1406+
1407+
return null;
1408+
}
1409+
13861410
}

tests/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,26 @@ public void testIsInstanceOf() {
135135
));
136136
}
137137
}
138+
139+
/**
140+
* @see fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil#insertUseIfNecessary
141+
*/
142+
public void testUseImports() {
143+
assertEquals("Bar", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
144+
"namespace Foo;\n" +
145+
"class Foo{}\n"
146+
), "\\Foo\\Bar"));
147+
148+
assertEquals("Bar", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
149+
"namespace Foo;\n" +
150+
"use Foo\\Bar;\n" +
151+
"class Foo{}\n"
152+
), "\\Foo\\Bar"));
153+
154+
assertEquals("Apple", PhpElementsUtil.insertUseIfNecessary(PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
155+
"namespace Bar\\Bar;\n" +
156+
"use Foo as Car;\n" +
157+
"class Foo{}\n"
158+
), "Foo\\Cool\\Apple"));
159+
}
138160
}

0 commit comments

Comments
 (0)