Skip to content

Commit

Permalink
fixup! Impl. Expression Language Injections for PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Sep 18, 2021
1 parent 71f6acd commit 5d0e72d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public Builder matchingConstructorCallArgument(@NotNull String classFQN, @NotNul
return matchingPattern(LanguageInjectionPatterns.getConstructorCallArgumentPattern(classFQN, argumentName, argumentIndex));
}

public Builder matchingConstructorCallWithArrayArgumentPattern(@NotNull String classFQN, @NotNull String argumentName, int argumentIndex, @NotNull String keyName) {
return matchingPattern(LanguageInjectionPatterns.getConstructorCallWithArrayArgumentPattern(classFQN, argumentName, argumentIndex, keyName));
}

public Builder matchingFunctionCallArgument(@NotNull String functionFQN, @NotNull String argumentName, int argumentIndex) {
return matchingPattern(LanguageInjectionPatterns.getFunctionCallArgumentPattern(functionFQN, argumentName, argumentIndex));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.jetbrains.php.lang.documentation.phpdoc.lexer.PhpDocTokenTypes;
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.patterns.PhpPatterns;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.ParameterListImpl;
import de.espend.idea.php.annotation.util.AnnotationUtil;
Expand Down Expand Up @@ -76,6 +78,34 @@ public static ElementPattern<? extends PsiElement> getConstructorCallArgumentPat
);
}

public static ElementPattern<? extends PsiElement> getConstructorCallWithArrayArgumentPattern(
@NotNull String classFQN,
@NotNull String argumentName,
int argumentIndex,
@NotNull String keyName
) {
return PlatformPatterns.psiElement()
.withParent(PlatformPatterns
.psiElement(PhpPsiElement.class)
.withElementType(PhpElementTypes.ARRAY_VALUE)
.withParent(PlatformPatterns
.psiElement(ArrayHashElement.class)
.with(new IsArrayHashElementKey(keyName))
.withParent(PlatformPatterns
.psiElement(ArrayCreationExpression.class)
.with(new IsArgument(argumentName, argumentIndex))
.withParent(PlatformPatterns
.psiElement(ParameterList.class)
.withParent(
PlatformPatterns.psiElement(NewExpression.class)
.with(new IsConstructorReference(classFQN))
)
)
)
)
);
}

public static ElementPattern<? extends PsiElement> getFunctionCallArgumentPattern(
@NotNull String functionFQN,
@NotNull String argumentName,
Expand Down Expand Up @@ -178,6 +208,26 @@ public boolean accepts(@NotNull PhpAttribute phpAttribute, ProcessingContext con
}
}

private static class IsArrayHashElementKey extends PatternCondition<ArrayHashElement> {
@NotNull
private final String name;

public IsArrayHashElementKey(@NotNull String name) {
super(String.format("IsArrayHashElementKey(%s)", name));
this.name = name;
}

@Override
public boolean accepts(@NotNull ArrayHashElement arrayHashElement, ProcessingContext context) {
var key = arrayHashElement.getKey();
if (key instanceof StringLiteralExpression) {
return ((StringLiteralExpression) key).getContents().equals(name);
}

return false;
}
}

private static class IsArgument extends PatternCondition<PsiElement> {
@NotNull
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class StringLiteralLanguageInjector implements MultiHostInjector {
.build(),
new LanguageInjection.Builder(LANGUAGE_ID_EXPRESSION_LANGUAGE)
.matchingConstructorCallArgument("\\Symfony\\Component\\ExpressionLanguage\\Expression", "expression", 0)
.matchingConstructorCallWithArrayArgumentPattern("\\Symfony\\Component\\Validator\\Constraints\\Expression", "expression", 0, "expression")
.matchingFunctionCallArgument("\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage", "evaluate", "expression", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage", "compile", "expression", 0)
Expand Down

0 comments on commit 5d0e72d

Please sign in to comment.