Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace recursive visiting in getFirstVariableTypeInScope with controlflow #2358

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading