Skip to content

Commit

Permalink
SONARPHP-1570 Symbols should be created for tree inside PropertyHooks…
Browse files Browse the repository at this point in the history
… in constructor functions (#1316)
  • Loading branch information
jonas-wielage-sonarsource authored Nov 14, 2024
1 parent 91df795 commit 1eac839
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ public void visitParameter(ParameterTree tree) {
if (initValue != null) {
initValue.accept(this);
}

var propertyHookListTree = tree.propertyHookList();
if (propertyHookListTree != null) {
scan(propertyHookListTree);
}
// do not scan the children to not pass through variableIdentifier
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void constantDeclaration() {
}

@Test
void shouldCreateSymbolForFunctionInPropertyHook() {
void shouldCreateSymbolForFunctionInVariableDeclarationPropertyHook() {
SymbolTableImpl symbolTable = symbolTableFor("<?php class A { public int $prop { get { return isset($this->prop2); } } }");

Symbol symbol = symbolTable.getSymbol("isset");
Expand All @@ -561,6 +561,24 @@ void shouldCreateSymbolForFunctionInPropertyHook() {
assertThat(symbol.usages()).hasSize(1);
}

@Test
void shouldCreateSymbolForFunctionInParameterPropertyHook() {
SymbolTableImpl symbolTable = symbolTableFor("""
<?php
class A {
public function __construct(
public $prop = 42 {
get => isset($this->prop);
}
) { }
}""");

Symbol symbol = symbolTable.getSymbol("isset");
assertThat(symbol).isInstanceOf(UndeclaredSymbol.class);
assertThat(symbol.is(Symbol.Kind.FUNCTION)).isTrue();
assertThat(symbol.usages()).hasSize(1);
}

private static void assertClassSymbols(SymbolTableImpl symbolTable, String... fullyQualifiedNames) {
assertThat(symbolTable.getSymbols(Kind.CLASS)).extracting(s -> s.qualifiedName().toString())
.containsExactly(fullyQualifiedNames);
Expand Down

0 comments on commit 1eac839

Please sign in to comment.