Skip to content

Commit 1042c53

Browse files
committed
fixup! Impl. Expression Language Injections for PHP
1 parent 6133ea9 commit 1042c53

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/lang/LanguageInjection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public Builder matchingConstructorCallArgument(@NotNull String classFQN, @NotNul
9191
return matchingPattern(LanguageInjectionPatterns.getConstructorCallArgumentPattern(classFQN, argumentName, argumentIndex));
9292
}
9393

94+
public Builder matchingConstructorCallWithArrayArgumentPattern(@NotNull String classFQN, @NotNull String argumentName, int argumentIndex, @NotNull String keyName) {
95+
return matchingPattern(LanguageInjectionPatterns.getConstructorCallWithArrayArgumentPattern(classFQN, argumentName, argumentIndex, keyName));
96+
}
97+
9498
public Builder matchingFunctionCallArgument(@NotNull String functionFQN, @NotNull String argumentName, int argumentIndex) {
9599
return matchingPattern(LanguageInjectionPatterns.getFunctionCallArgumentPattern(functionFQN, argumentName, argumentIndex));
96100
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/lang/LanguageInjectionPatterns.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.jetbrains.php.lang.documentation.phpdoc.lexer.PhpDocTokenTypes;
1010
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
1111
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
12+
import com.jetbrains.php.lang.parser.PhpElementTypes;
13+
import com.jetbrains.php.lang.patterns.PhpPatterns;
1214
import com.jetbrains.php.lang.psi.elements.*;
1315
import com.jetbrains.php.lang.psi.elements.impl.ParameterListImpl;
1416
import de.espend.idea.php.annotation.util.AnnotationUtil;
@@ -76,6 +78,34 @@ public static ElementPattern<? extends PsiElement> getConstructorCallArgumentPat
7678
);
7779
}
7880

81+
public static ElementPattern<? extends PsiElement> getConstructorCallWithArrayArgumentPattern(
82+
@NotNull String classFQN,
83+
@NotNull String argumentName,
84+
int argumentIndex,
85+
@NotNull String keyName
86+
) {
87+
return PlatformPatterns.psiElement()
88+
.withParent(PlatformPatterns
89+
.psiElement(PhpPsiElement.class)
90+
.withElementType(PhpElementTypes.ARRAY_VALUE)
91+
.withParent(PlatformPatterns
92+
.psiElement(ArrayHashElement.class)
93+
.with(new IsArrayHashElementKey(keyName))
94+
.withParent(PlatformPatterns
95+
.psiElement(ArrayCreationExpression.class)
96+
.with(new IsArgument(argumentName, argumentIndex))
97+
.withParent(PlatformPatterns
98+
.psiElement(ParameterList.class)
99+
.withParent(
100+
PlatformPatterns.psiElement(NewExpression.class)
101+
.with(new IsConstructorReference(classFQN))
102+
)
103+
)
104+
)
105+
)
106+
);
107+
}
108+
79109
public static ElementPattern<? extends PsiElement> getFunctionCallArgumentPattern(
80110
@NotNull String functionFQN,
81111
@NotNull String argumentName,
@@ -178,6 +208,26 @@ public boolean accepts(@NotNull PhpAttribute phpAttribute, ProcessingContext con
178208
}
179209
}
180210

211+
private static class IsArrayHashElementKey extends PatternCondition<ArrayHashElement> {
212+
@NotNull
213+
private final String name;
214+
215+
public IsArrayHashElementKey(@NotNull String name) {
216+
super(String.format("IsArrayHashElementKey(%s)", name));
217+
this.name = name;
218+
}
219+
220+
@Override
221+
public boolean accepts(@NotNull ArrayHashElement arrayHashElement, ProcessingContext context) {
222+
var key = arrayHashElement.getKey();
223+
if (key instanceof StringLiteralExpression) {
224+
return ((StringLiteralExpression) key).getContents().equals(name);
225+
}
226+
227+
return false;
228+
}
229+
}
230+
181231
private static class IsArgument extends PatternCondition<PsiElement> {
182232
@NotNull
183233
private final String name;

src/main/java/fr/adrienbrault/idea/symfony2plugin/lang/StringLiteralLanguageInjector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class StringLiteralLanguageInjector implements MultiHostInjector {
4141
.build(),
4242
new LanguageInjection.Builder(LANGUAGE_ID_EXPRESSION_LANGUAGE)
4343
.matchingConstructorCallArgument("\\Symfony\\Component\\ExpressionLanguage\\Expression", "expression", 0)
44+
.matchingConstructorCallWithArrayArgumentPattern("\\Symfony\\Component\\Validator\\Constraints\\Expression", "expression", 0, "expression")
4445
.matchingFunctionCallArgument("\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0)
4546
.matchingMethodCallArgument("\\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage", "evaluate", "expression", 0)
4647
.matchingMethodCallArgument("\\Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage", "compile", "expression", 0)

0 commit comments

Comments
 (0)