Skip to content

Commit

Permalink
Merge pull request #2359 from Haehnchen/feature/recursive-visit-varia…
Browse files Browse the repository at this point in the history
…ble-collect

use controlflow to collect variables
  • Loading branch information
Haehnchen authored Apr 27, 2024
2 parents f441115 + 6fbe368 commit cd5cfef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static void visitAttributeForeach(@NotNull Method method, @NotNull Consu
return;
}

for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[2])) {
for (Variable variable : PhpElementsUtil.getVariablesInScopeByName(method, parameters[2].getName())) {
// foreach ($attributes as $attribute)
PsiElement psiElement = PsiTreeUtil.nextVisibleLeaf(variable);
if (psiElement != null && psiElement.getNode().getElementType() == PhpTokenTypes.kwAS) {
Expand All @@ -182,9 +182,8 @@ private static void visitAttributeForeach(@NotNull Method method, @NotNull Consu
continue;
}

PhpPsiElement variableDecl = variable.getNextPsiSibling();
if (variableDecl instanceof Variable) {
for (Variable variable1 : PhpElementsUtil.getVariablesInScope(parent, (Variable) variableDecl)) {
if (variable.getNextPsiSibling() instanceof Variable variableDecl) {
for (Variable variable1 : PhpElementsUtil.getVariablesInScope(parent, variableDecl)) {
visitVariable(variable1, consumer);
}
}
Expand Down Expand Up @@ -213,7 +212,7 @@ private static void visitAttribute(@NotNull Method method, @NotNull Consumer<Pai
return;
}

for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[0])) {
for (Variable variable : PhpElementsUtil.getVariablesInScopeByName(method, parameters[0].getName())) {
visitVariable(variable, consumer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static List<PsiElement> collectPossibleTemplateArrays(@NotNull Function
private static Map<String, PsiVariable> collectOnVariableReferences(@NotNull Function function, @NotNull Variable variable) {
Map<String, PsiVariable> collectedTypes = new HashMap<>();

for (Variable scopeVar : PhpElementsUtil.getVariablesInScope(function, variable)) {
for (Variable scopeVar : PhpElementsUtil.getVariablesInScopeByName(function, variable.getName())) {
PsiElement parent = scopeVar.getParent();
if (parent instanceof ArrayAccessExpression) {
// $template['variable'] = $foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ public static Collection<Variable> getVariableReferencesInScope(@NotNull Variabl

final List<Variable> variables = new ArrayList<>();

for (Variable variableRef : PhpElementsUtil.getVariablesInScope(function, variableDecl)) {
for (Variable variableRef : PhpElementsUtil.getVariablesInScopeByName(function, variableDecl.getName())) {
if (!variableRef.equals(variable)) {
variables.add(variableRef);
}
Expand Down Expand Up @@ -1506,7 +1506,7 @@ public static String getFirstVariableTypeInScope(@NotNull Variable variable) {
@Override
public boolean processAccessVariableInstruction(PhpAccessVariableInstruction instruction) {
PhpPsiElement element = instruction.getAnchor();
if(element instanceof Variable variableVisit && name.equals(variableVisit.getName())) {
if (name.contentEquals(instruction.getVariableName()) && element instanceof Variable) {
PsiElement assignmentExpression = element.getParent();
if(assignmentExpression instanceof AssignmentExpression) {
PhpPsiElement value = ((AssignmentExpression) assignmentExpression).getValue();
Expand Down Expand Up @@ -1786,6 +1786,23 @@ public static Set<Variable> getVariablesInScope(@NotNull PsiElement psiElement,
return MyVariableRecursiveElementVisitor.visit(psiElement, variable.getName());
}

public static Set<Variable> getVariablesInScopeByName(@NotNull PhpScopeHolder phpScopeHolder, @NotNull String name) {
Set<Variable> variables = new HashSet<>();

PhpControlFlowUtil.processFlow(phpScopeHolder.getControlFlow(), new PhpInstructionProcessor() {
@Override
public boolean processAccessVariableInstruction(PhpAccessVariableInstruction instruction) {
if (name.contentEquals(instruction.getVariableName()) && instruction.getAnchor() instanceof Variable variable) {
variables.add(variable);
}

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

return variables;
}

/**
* Provide array key pattern. we need incomplete array key support, too.
*
Expand Down

0 comments on commit cd5cfef

Please sign in to comment.