Skip to content

Commit

Permalink
Fix infinite loop when resolving method call with superclass cycle (#656
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pynicolas authored Aug 24, 2020
1 parent dcbf6cb commit 5951b3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package org.sonar.php.tree.symbols;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.sonar.php.symbols.ClassSymbol;
import org.sonar.php.symbols.ClassSymbolIndex;
import org.sonar.php.symbols.FunctionSymbol;
Expand Down Expand Up @@ -99,7 +101,8 @@ private void resolveMethodCall(FunctionCallTree tree, MemberAccessTree callee) {
String methodName = ((NameIdentifierTree) callee.member()).text();
MethodSymbol methodSymbol = receiverSymbol.getDeclaredMethod(methodName);
Optional<ClassSymbol> superClass = receiverSymbol.superClass();
while (superClass.isPresent() && methodSymbol.isUnknownSymbol()) {
Set<ClassSymbol> processedClasses = new HashSet<>();
while (superClass.isPresent() && methodSymbol.isUnknownSymbol() && processedClasses.add(superClass.get())) {
methodSymbol = superClass.get().getDeclaredMethod(methodName);
superClass = superClass.get().superClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void resolve_this_with_inheritance() {
"class B extends A { function f(){ $this->f(); } }")).isKnown("B::f");
}

@Test
public void resolve_with_superclass_cycle() {
assertThat(callSymbol("<?php ",
"class A extends B { }",
"class B extends A { function g(){ $this->f(); } }")).isUnknown();
}

@Test
public void resolve_self_and_static() {
assertThat(callSymbol("<?php class A { function f(){} function g(){ self::f(); } }")).isKnown("A::f");
Expand Down

0 comments on commit 5951b3f

Please sign in to comment.