Skip to content

Commit

Permalink
SONARPHP-1029: Return UnknownClassSymbol instead of throwing an excep…
Browse files Browse the repository at this point in the history
…tion (#655)

* Return UnknownClassSymbol instead of throwing an exception

* Use isUnknownSymbol instead of isInstanceOf
  • Loading branch information
nils-werner-sonarsource authored Aug 24, 2020
1 parent 5ce1ec7 commit dcbf6cb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 3 additions & 2 deletions php-frontend/src/main/java/org/sonar/php/symbols/Symbols.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.sonar.plugins.php.api.tree.expression.AnonymousClassTree;
import org.sonar.plugins.php.api.tree.expression.FunctionCallTree;

import static org.sonar.plugins.php.api.symbols.QualifiedName.qualifiedName;

/**
* Utility class to retrieve symbols from the AST.
* We can drop this class as soon as we expose an equivalent API directly on the AST interfaces.
Expand All @@ -47,12 +49,11 @@ public static FunctionSymbol get(FunctionCallTree functionCallTree) {
return ((FunctionCallTreeImpl) functionCallTree).symbol();
}

@Deprecated
public static ClassSymbol getClass(NamespaceNameTree namespaceNameTree) {
if (namespaceNameTree instanceof ClassNamespaceNameTreeImpl) {
return ((ClassNamespaceNameTreeImpl) namespaceNameTree).symbol();
}
throw new IllegalStateException("No class symbol available on " + namespaceNameTree);
return new UnknownClassSymbol(qualifiedName(namespaceNameTree.qualifiedName()));
}

public static ClassSymbol get(AnonymousClassTree anonymousClassTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.sonar.plugins.php.api.visitors.PhpFile;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.php.tree.TreeUtils.firstDescendant;
import static org.sonar.plugins.php.api.symbols.QualifiedName.qualifiedName;

Expand Down Expand Up @@ -124,9 +123,7 @@ public void non_class_namespace_name() {
PhpFile file1 = file("file1.php", "<?php namespace ns1; class A {}");
Tree ast = parser.parse(file1.contents());
NamespaceNameTree namespaceNameTree = firstDescendant(ast, NamespaceNameTree.class).get();
assertThatThrownBy(
() -> Symbols.getClass(namespaceNameTree))
.isInstanceOf(IllegalStateException.class);
assertThat(Symbols.getClass(namespaceNameTree).isUnknownSymbol()).isTrue();
}

@Test
Expand Down

0 comments on commit dcbf6cb

Please sign in to comment.