diff --git a/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java b/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java --- a/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java +++ b/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java @@ -52,6 +52,7 @@ import org.netbeans.modules.parsing.spi.indexing.support.IndexDocument; import org.netbeans.modules.php.api.util.StringUtils; import org.netbeans.modules.php.editor.CodeUtils; +import org.netbeans.modules.php.editor.api.AliasedName; import org.netbeans.modules.php.editor.api.ElementQuery; import org.netbeans.modules.php.editor.api.PhpElementKind; import org.netbeans.modules.php.editor.api.QualifiedName; @@ -75,9 +76,11 @@ import org.netbeans.modules.php.editor.model.Scope; import org.netbeans.modules.php.editor.model.TraitScope; import org.netbeans.modules.php.editor.model.TypeScope; +import org.netbeans.modules.php.editor.model.UseScope; import org.netbeans.modules.php.editor.model.VariableName; import org.netbeans.modules.php.editor.model.nodes.ClassDeclarationInfo; import org.netbeans.modules.php.editor.model.nodes.ClassInstanceCreationInfo; +import org.netbeans.modules.php.editor.model.nodes.NamespaceDeclarationInfo; import org.netbeans.modules.php.editor.parser.astnodes.BodyDeclaration; import org.netbeans.modules.php.editor.parser.astnodes.Expression; import org.netbeans.modules.php.editor.parser.astnodes.Variable; @@ -608,7 +611,56 @@ public Collection getTraits() { Collection result = new ArrayList<>(); for (QualifiedName qualifiedName : getUsedTraits()) { - result.addAll(IndexScopeImpl.getTraits(qualifiedName, this)); + result.addAll(filterTraits(IndexScopeImpl.getTraits(qualifiedName, this))); + } + return result; + } + + // #251045 + private Collection filterTraits(List traits) { + if (traits.size() < 2) { + // none or exactly one trait found + return traits; + } + // more traits => resolve proper type + NamespaceScope namespaceScope = retrieveNamespaceScope(); + if (namespaceScope == null) { + return traits; + } + Collection allUses = namespaceScope.getAllDeclaredSingleUses(); + for (UseScope useElement : allUses) { + AliasedName aliasName = useElement.getAliasedName(); + if (aliasName != null) { + // XXX + } else { + String fqn = NamespaceDeclarationInfo.NAMESPACE_SEPARATOR + useElement.getName(); + for (TraitScope trait : traits) { + if (fqn.equals(trait.getFullyQualifiedName().toString())) { + return Collections.singleton(trait); + } + } + } + } + // not in uses => check current namespace + QualifiedName namespaceName = getNamespaceName(); + for (TraitScope trait : traits) { + if (trait.getNamespaceName().equals(namespaceName)) { + return Collections.singleton(trait); + } + } + return traits; + } + + @CheckForNull + private NamespaceScope retrieveNamespaceScope() { + NamespaceScope result = null; + Scope inScope = getInScope(); + while (inScope != null + && !(inScope instanceof NamespaceScope)) { + inScope = inScope.getInScope(); + } + if (inScope != null) { + result = (NamespaceScope) inScope; } return result; }