Skip to content

Commit f441115

Browse files
authored
Merge pull request #2358 from Haehnchen/feature/recursive-visit
replace recursive visiting in getFirstVariableTypeInScope with controlflow
2 parents f3c707a + 8372a39 commit f441115

File tree

3 files changed

+13
-60
lines changed

3 files changed

+13
-60
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/inspection/TwigVariableDeprecatedInspection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
2727
return super.buildVisitor(holder, isOnTheFly);
2828
}
2929

30-
return new MyPsiRecursiveElementVisitor(holder);
30+
return new MyPsiElementVisitor(holder);
3131
}
3232

33-
private static class MyPsiRecursiveElementVisitor extends PsiElementVisitor {
33+
private static class MyPsiElementVisitor extends PsiElementVisitor {
3434

3535
@NotNull
3636
private final ProblemsHolder holder;
3737

38-
MyPsiRecursiveElementVisitor(@NotNull ProblemsHolder holder) {
38+
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
3939
this.holder = holder;
4040
}
4141

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/inspection/TwigVariablePathInspection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
2727
return super.buildVisitor(holder, isOnTheFly);
2828
}
2929

30-
return new MyPsiRecursiveElementVisitor(holder);
30+
return new MyPsiElementVisitor(holder);
3131
}
3232

33-
private static class MyPsiRecursiveElementVisitor extends PsiElementVisitor {
33+
private static class MyPsiElementVisitor extends PsiElementVisitor {
3434

3535
@NotNull
3636
private final ProblemsHolder holder;
3737

38-
MyPsiRecursiveElementVisitor(@NotNull ProblemsHolder holder) {
38+
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
3939
this.holder = holder;
4040
}
4141

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,23 +1493,20 @@ public static Collection<String> getMethodParameterTypeHints(@NotNull Method met
14931493
*/
14941494
@Nullable
14951495
public static String getFirstVariableTypeInScope(@NotNull Variable variable) {
1496-
14971496
// parent search scope, eg Method else fallback to a grouped statement
1498-
PsiElement searchScope = PsiTreeUtil.getParentOfType(variable, Function.class);
1499-
if(searchScope == null) {
1500-
searchScope = PsiTreeUtil.getParentOfType(variable, GroupStatement.class);
1501-
}
1502-
1497+
PhpScopeHolder searchScope = PsiTreeUtil.getParentOfType(variable, PhpScopeHolder.class);
15031498
if(searchScope == null) {
15041499
return null;
15051500
}
15061501

15071502
final String name = variable.getName();
15081503
final String[] result = {null};
1509-
searchScope.acceptChildren(new PsiRecursiveElementVisitor() {
1504+
1505+
PhpControlFlowUtil.processFlow(searchScope.getControlFlow(), new PhpInstructionProcessor() {
15101506
@Override
1511-
public void visitElement(PsiElement element) {
1512-
if(element instanceof Variable && name.equals(((Variable) element).getName())) {
1507+
public boolean processAccessVariableInstruction(PhpAccessVariableInstruction instruction) {
1508+
PhpPsiElement element = instruction.getAnchor();
1509+
if(element instanceof Variable variableVisit && name.equals(variableVisit.getName())) {
15131510
PsiElement assignmentExpression = element.getParent();
15141511
if(assignmentExpression instanceof AssignmentExpression) {
15151512
PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
@@ -1525,51 +1522,7 @@ public void visitElement(PsiElement element) {
15251522
}
15261523
}
15271524

1528-
super.visitElement(element);
1529-
}
1530-
});
1531-
1532-
return result[0];
1533-
}
1534-
1535-
/**
1536-
* Find first variable declaration in parent scope of a given variable:
1537-
*
1538-
* function() {
1539-
* $event = new FooEvent();
1540-
* dispatch('foo', $event);
1541-
* }
1542-
*/
1543-
@Nullable
1544-
public static PhpPsiElement getFirstVariableAssignmentInScope(@NotNull Variable variable) {
1545-
1546-
// parent search scope, eg Method else fallback to a grouped statement
1547-
PsiElement searchScope = PsiTreeUtil.getParentOfType(variable, Function.class);
1548-
if(searchScope == null) {
1549-
searchScope = PsiTreeUtil.getParentOfType(variable, GroupStatement.class);
1550-
}
1551-
1552-
if(searchScope == null) {
1553-
return null;
1554-
}
1555-
1556-
final String name = variable.getName();
1557-
final PhpPsiElement[] result = {null};
1558-
1559-
searchScope.acceptChildren(new PsiRecursiveElementVisitor() {
1560-
@Override
1561-
public void visitElement(@NotNull PsiElement element) {
1562-
if(element instanceof Variable && name.equals(((Variable) element).getName())) {
1563-
PsiElement assignmentExpression = element.getParent();
1564-
if(assignmentExpression instanceof AssignmentExpression) {
1565-
PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
1566-
if(value != null) {
1567-
result[0] = value;
1568-
}
1569-
}
1570-
}
1571-
1572-
super.visitElement(element);
1525+
return super.processAccessVariableInstruction(instruction);
15731526
}
15741527
});
15751528

0 commit comments

Comments
 (0)