Skip to content

Commit

Permalink
replace recursive visiting in getFirstVariableTypeInScope with contro…
Browse files Browse the repository at this point in the history
…lflow
  • Loading branch information
Haehnchen committed Apr 27, 2024
1 parent f3c707a commit 8372a39
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
return super.buildVisitor(holder, isOnTheFly);
}

return new MyPsiRecursiveElementVisitor(holder);
return new MyPsiElementVisitor(holder);
}

private static class MyPsiRecursiveElementVisitor extends PsiElementVisitor {
private static class MyPsiElementVisitor extends PsiElementVisitor {

@NotNull
private final ProblemsHolder holder;

MyPsiRecursiveElementVisitor(@NotNull ProblemsHolder holder) {
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
this.holder = holder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
return super.buildVisitor(holder, isOnTheFly);
}

return new MyPsiRecursiveElementVisitor(holder);
return new MyPsiElementVisitor(holder);
}

private static class MyPsiRecursiveElementVisitor extends PsiElementVisitor {
private static class MyPsiElementVisitor extends PsiElementVisitor {

@NotNull
private final ProblemsHolder holder;

MyPsiRecursiveElementVisitor(@NotNull ProblemsHolder holder) {
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
this.holder = holder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,23 +1493,20 @@ public static Collection<String> getMethodParameterTypeHints(@NotNull Method met
*/
@Nullable
public static String getFirstVariableTypeInScope(@NotNull Variable variable) {

// parent search scope, eg Method else fallback to a grouped statement
PsiElement searchScope = PsiTreeUtil.getParentOfType(variable, Function.class);
if(searchScope == null) {
searchScope = PsiTreeUtil.getParentOfType(variable, GroupStatement.class);
}

PhpScopeHolder searchScope = PsiTreeUtil.getParentOfType(variable, PhpScopeHolder.class);
if(searchScope == null) {
return null;
}

final String name = variable.getName();
final String[] result = {null};
searchScope.acceptChildren(new PsiRecursiveElementVisitor() {

PhpControlFlowUtil.processFlow(searchScope.getControlFlow(), new PhpInstructionProcessor() {
@Override
public void visitElement(PsiElement element) {
if(element instanceof Variable && name.equals(((Variable) element).getName())) {
public boolean processAccessVariableInstruction(PhpAccessVariableInstruction instruction) {
PhpPsiElement element = instruction.getAnchor();
if(element instanceof Variable variableVisit && name.equals(variableVisit.getName())) {
PsiElement assignmentExpression = element.getParent();
if(assignmentExpression instanceof AssignmentExpression) {
PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
Expand All @@ -1525,51 +1522,7 @@ public void visitElement(PsiElement element) {
}
}

super.visitElement(element);
}
});

return result[0];
}

/**
* Find first variable declaration in parent scope of a given variable:
*
* function() {
* $event = new FooEvent();
* dispatch('foo', $event);
* }
*/
@Nullable
public static PhpPsiElement getFirstVariableAssignmentInScope(@NotNull Variable variable) {

// parent search scope, eg Method else fallback to a grouped statement
PsiElement searchScope = PsiTreeUtil.getParentOfType(variable, Function.class);
if(searchScope == null) {
searchScope = PsiTreeUtil.getParentOfType(variable, GroupStatement.class);
}

if(searchScope == null) {
return null;
}

final String name = variable.getName();
final PhpPsiElement[] result = {null};

searchScope.acceptChildren(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if(element instanceof Variable && name.equals(((Variable) element).getName())) {
PsiElement assignmentExpression = element.getParent();
if(assignmentExpression instanceof AssignmentExpression) {
PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
if(value != null) {
result[0] = value;
}
}
}

super.visitElement(element);
return super.processAccessVariableInstruction(instruction);
}
});

Expand Down

0 comments on commit 8372a39

Please sign in to comment.