Skip to content

Commit

Permalink
fix template usage extraction issues with inline method reference usi…
Browse files Browse the repository at this point in the history
…ng wrong type cast #1410
  • Loading branch information
Haehnchen committed Jan 2, 2020
1 parent 2d9e51f commit dbe29d4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void visitMethodReference(@NotNull MethodReference methodReference) {
}

if (parameters[0] instanceof StringLiteralExpression) {
resolveString(methodReference, parameters[0]);
addStringLiteralScope(methodReference, (StringLiteralExpression) parameters[0]);
} else if(parameters[0] instanceof TernaryExpression) {
// render(true === true ? 'foo.twig.html' : 'foobar.twig.html')
for (PhpPsiElement phpPsiElement : new PhpPsiElement[]{((TernaryExpression) parameters[0]).getTrueVariant(), ((TernaryExpression) parameters[0]).getFalseVariant()}) {
Expand All @@ -322,16 +322,16 @@ private void visitMethodReference(@NotNull MethodReference methodReference) {
}

if (phpPsiElement instanceof StringLiteralExpression) {
resolveString(methodReference, phpPsiElement);
addStringLiteralScope(methodReference, (StringLiteralExpression) phpPsiElement);
} else if(phpPsiElement instanceof PhpReference) {
resolvePhpReference(methodReference, phpPsiElement);
}
}
} else if(parameters[0] instanceof AssignmentExpression) {
// $this->render($template = 'foo.html.twig')
PhpPsiElement value = ((AssignmentExpression) parameters[0]).getValue();
if(value != null) {
resolveString(methodReference, value);
if(value instanceof StringLiteralExpression) {
addStringLiteralScope(methodReference, (StringLiteralExpression) value);
}
} else if(parameters[0] instanceof PhpReference) {
resolvePhpReference(methodReference, parameters[0]);
Expand All @@ -340,28 +340,13 @@ private void visitMethodReference(@NotNull MethodReference methodReference) {
PsiElement phpPsiElement = ((BinaryExpression) parameters[0]).getRightOperand();

if (phpPsiElement instanceof StringLiteralExpression) {
resolveString(methodReference, phpPsiElement);
addStringLiteralScope(methodReference, (StringLiteralExpression) phpPsiElement);
} else if(phpPsiElement instanceof PhpReference) {
resolvePhpReference(methodReference, phpPsiElement);
}
}
}

private void resolveString(@NotNull MethodReference methodReference, PsiElement parameter) {
// foo('foo.html.twig')
String contents = ((StringLiteralExpression) parameter).getContents();
if (StringUtils.isBlank(contents) || !contents.endsWith(".twig")) {
return;
}

Function parentOfType = PsiTreeUtil.getParentOfType(methodReference, Function.class);
if(parentOfType == null) {
return;
}

addTemplateWithScope(contents, parentOfType, methodReference);
}

private void resolvePhpReference(@NotNull MethodReference methodReference, PsiElement parameter) {
for (PhpNamedElement phpNamedElement : ((PhpReference) parameter).resolveLocal()) {
// foo(self::foo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void testThatTwigRenderMethodsAreInIndex() {
" $foo->render('@!Foo/overwrite.html.twig');\n" +
" $foo->renderView('foo-renderView.html.twig');\n" +
" $foo->renderView($t = 'foo-var-assignment-expression.html.twig');\n" +
" $foo->renderView($t = $foo->foo());\n" +
" $foo->renderResponse('foo-renderResponse.html.twig');\n" +
" $foo->render(self::FOO);\n" +
" $foo->render($var);\n" +
Expand Down

0 comments on commit dbe29d4

Please sign in to comment.