Skip to content

Commit 3499f93

Browse files
authored
Merge pull request #1775 from Haehnchen/feature/twig-class
support class constants for Twig extension navigation
2 parents a60e585 + 27f91c1 commit 3499f93

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigExtensionParser.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private static void parseFunctions(@NotNull Method method, @NotNull Map<String,
149149
*/
150150
@Nullable
151151
private static String getCallableSignature(PsiElement psiElement, Method method) {
152+
// @TODO can be replaced by PhpStorm function now; much smarter
152153
// array($this, 'getUrl')
153154
if(psiElement instanceof ArrayCreationExpression) {
154155
List<PsiElement> arrayValues = (List<PsiElement>) PsiElementUtils.getChildrenOfTypeAsList(psiElement, PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE));
@@ -162,6 +163,17 @@ private static String getCallableSignature(PsiElement psiElement, Method method)
162163
return String.format("#M#C\\%s.%s", phpClass.getPresentableFQN(), methodName);
163164
}
164165
}
166+
} else if (firstChild instanceof ClassConstantReference) {
167+
String classConstantPhpFqn = PhpElementsUtil.getClassConstantPhpFqn((ClassConstantReference) firstChild);
168+
if (StringUtils.isNotEmpty(classConstantPhpFqn)) {
169+
String methodName = PhpElementsUtil.getStringValue(arrayValues.get(1).getFirstChild());
170+
if(StringUtils.isNotBlank(methodName)) {
171+
PhpClass phpClass = method.getContainingClass();
172+
if(phpClass != null) {
173+
return String.format("#M#C\\%s.%s", classConstantPhpFqn, methodName);
174+
}
175+
}
176+
}
165177
}
166178
}
167179
} else {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/util/TwigExtensionParserTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public void testExtensionAreCollected() {
5353
"OPERATOR",
5454
TwigExtensionParser.getOperators(getProject()).get("or").getType()
5555
);
56+
57+
assertEquals(
58+
"#M#C\\ClassInstance.getFoobar",
59+
TwigExtensionParser.getFunctions(getProject()).get("class_instance_foobar").getSignature()
60+
);
5661
}
5762

5863
public void testExtensionAreCollectedForDeprecated() {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/util/fixtures/twig_extensions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
function foo_test() {}
66
class My_Node_Test {}
77

8+
class ClassInstance {
9+
public function getFoobar() {}
10+
}
11+
812
interface Twig_ExtensionInterface
913
{
1014
public function getTokenParsers();
@@ -104,6 +108,7 @@ public function getFunctions()
104108
'hwi_oauth_login_url' => new \Twig_Function_Method($this, 'foobar'),
105109
new \Twig_Function('max_2', 'max'),
106110
new TwigFunction('max_3', 'max'),
111+
new TwigFunction('class_instance_foobar', [\ClassInstance::class, 'getFoobar']),
107112
];
108113
}
109114

0 commit comments

Comments
 (0)