Skip to content

Commit

Permalink
Merge pull request #2208 from Haehnchen/feature/variadic-doctrine-where
Browse files Browse the repository at this point in the history
support Doctrine querybuilder "where" methods to be "variadic"
  • Loading branch information
Haehnchen authored Aug 12, 2023
2 parents 0ef0fcb + da12529 commit eacb705
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
return;
}

MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement.getContext(), 0)
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement.getContext())
.withSignature(WHERES)
.match();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,21 @@ private void collectJoins(QueryBuilderScopeContext qb, MethodReference methodRef
}

private void collectParameter(QueryBuilderScopeContext qb, MethodReference methodReference, String name) {

if(!collectParameter || !Arrays.asList("where", "andWhere").contains(name)) {
if (!collectParameter || !Arrays.asList("where", "andWhere").contains(name)) {
return;
}

String value = PsiElementUtils.getMethodParameterAt(methodReference, 0);
if(value != null) {
for (PsiElement parameter : methodReference.getParameters()) {
String value = PsiElementUtils.getMethodParameter(parameter);
if (value == null) {
continue;
}

Matcher matcher = Pattern.compile(":(\\w+)", Pattern.MULTILINE).matcher(value);
while(matcher.find()){
while (matcher.find()){
qb.addParameter(matcher.group(1));
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static MethodMatcher.MethodMatchParameter matchJoin(PsiElement psiElement

@Nullable
public static MethodMatcher.MethodMatchParameter matchWhere(PsiElement psiElement) {
return new MethodMatcher.StringParameterMatcher(psiElement, 0)
return new MethodMatcher.StringParameterMatcher(psiElement)
.withSignature("\\Doctrine\\ORM\\QueryBuilder", "andWhere")
.withSignature("\\Doctrine\\ORM\\QueryBuilder", "where")
.withSignature("\\Doctrine\\ORM\\QueryBuilder", "orWhere")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ public StringParameterMatcher(PsiElement psiElement, int parameterIndex) {
super(psiElement, parameterIndex);
}

public StringParameterMatcher(PsiElement psiElement) {
super(psiElement, -1);
}

@Nullable
public MethodMatchParameter match() {

if (!Symfony2ProjectComponent.isEnabled(psiElement)) {
return null;
}

MethodReferenceBag bag = PhpElementsUtil.getMethodParameterReferenceBag(psiElement, this.parameterIndex);
MethodReferenceBag bag = this.parameterIndex >= 0
? PhpElementsUtil.getMethodParameterReferenceBag(psiElement, this.parameterIndex)
: PhpElementsUtil.getMethodParameterReferenceBag(psiElement);

if(bag == null) {
return null;
}
Expand Down

0 comments on commit eacb705

Please sign in to comment.