|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +package com.magento.idea.magento2plugin.actions.generation.ImportReferences; |
| 6 | + |
| 7 | +import com.intellij.openapi.project.Project; |
| 8 | +import com.intellij.psi.PsiElement; |
| 9 | +import com.jetbrains.php.codeInsight.PhpCodeInsightUtil; |
| 10 | +import com.jetbrains.php.lang.PhpLangUtil; |
| 11 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment; |
| 12 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocType; |
| 13 | +import com.jetbrains.php.lang.lexer.PhpTokenTypes; |
| 14 | +import com.jetbrains.php.lang.psi.PhpPsiElementFactory; |
| 15 | +import com.jetbrains.php.lang.psi.PhpPsiUtil; |
| 16 | +import com.jetbrains.php.lang.psi.elements.ClassReference; |
| 17 | +import com.jetbrains.php.lang.psi.elements.PhpNamespace; |
| 18 | +import com.jetbrains.php.lang.psi.elements.PhpPsiElement; |
| 19 | +import com.jetbrains.php.lang.psi.elements.Statement; |
| 20 | +import com.jetbrains.php.lang.psi.resolve.types.PhpType; |
| 21 | +import com.jetbrains.php.refactoring.PhpAliasImporter; |
| 22 | +import gnu.trove.THashMap; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Map.Entry; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | +import org.jetbrains.annotations.Nullable; |
| 29 | + |
| 30 | +public class PhpClassReferenceResolver extends PhpClassReferenceExtractor { |
| 31 | + private final PhpClassReferenceStorage myCandidatesToImportStorage = new PhpClassReferenceStorage(); |
| 32 | + |
| 33 | + public PhpClassReferenceResolver() { |
| 34 | + } |
| 35 | + |
| 36 | + public void processElement(@NotNull PsiElement element) { |
| 37 | + super.processElement(element); |
| 38 | + } |
| 39 | + |
| 40 | + protected void processReference(@NotNull String name, @NotNull String fqn, @NotNull PsiElement identifier) { |
| 41 | + if (!PhpType.isPrimitiveType(name)) { |
| 42 | + this.myCandidatesToImportStorage.processReference(name, fqn, identifier); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + private static String alreadyImported(@NotNull PhpPsiElement scopeHolder, @NotNull Map<String, String> aliases, @NotNull String fqn, @NotNull String name) { |
| 48 | + boolean isSameNamespace = PhpCodeInsightUtil.isSameNamespace(scopeHolder, fqn); |
| 49 | + if (isSameNamespace && PhpLangUtil.equalsClassNames(PhpLangUtil.toShortName(fqn), name)) { |
| 50 | + return name; |
| 51 | + } |
| 52 | + boolean isNonCompound = isNonCompoundUseName(scopeHolder, fqn, name); |
| 53 | + if (isNonCompound) { |
| 54 | + return name; |
| 55 | + } |
| 56 | + Iterator entryIterator = aliases.entrySet().iterator(); |
| 57 | + |
| 58 | + Entry alias; |
| 59 | + do { |
| 60 | + if (!entryIterator.hasNext()) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + alias = (Entry) entryIterator.next(); |
| 65 | + } while (!PhpLangUtil.equalsClassNames((CharSequence) alias.getValue(), fqn)); |
| 66 | + |
| 67 | + return (String) alias.getKey(); |
| 68 | + } |
| 69 | + |
| 70 | + public static boolean isNonCompoundUseName(@NotNull PhpPsiElement scopeHolder, @NotNull String fqn, @NotNull String name) { |
| 71 | + String currentNamespaceName = scopeHolder instanceof PhpNamespace ? ((PhpNamespace)scopeHolder).getFQN() : ""; |
| 72 | + return "\\".equals(currentNamespaceName) && PhpLangUtil.equalsClassNames(fqn, PhpLangUtil.toFQN(name)); |
| 73 | + } |
| 74 | + |
| 75 | + public void importReferences(@NotNull PhpPsiElement scopeHolder, @NotNull List<PsiElement> movedElements) { |
| 76 | + Map<String, String> referencesToReplace = this.importWithConflictResolve(scopeHolder); |
| 77 | + if (referencesToReplace.isEmpty()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + PhpClassReferenceResolver.PhpClassReferenceRenamer renamer = new PhpClassReferenceResolver.PhpClassReferenceRenamer(scopeHolder.getProject(), referencesToReplace); |
| 81 | + renamer.processElements(movedElements); |
| 82 | + } |
| 83 | + |
| 84 | + private Map<String, String> importWithConflictResolve(PhpPsiElement scopeHolder) { |
| 85 | + Map<String, String> aliases = PhpCodeInsightUtil.getAliasesInScope(scopeHolder); |
| 86 | + Map<String, String> referencesToReplace = new THashMap(); |
| 87 | + boolean autoImport = PhpCodeInsightUtil.isAutoImportEnabled(scopeHolder); |
| 88 | + |
| 89 | + for (String name : this.myCandidatesToImportStorage.getNames()) { |
| 90 | + String originalFqn = this.myCandidatesToImportStorage.getFqnByName(name); |
| 91 | + assert originalFqn != null; |
| 92 | + |
| 93 | + String alias = alreadyImported(scopeHolder, aliases, originalFqn, name); |
| 94 | + if (alias != null) { |
| 95 | + if (!PhpLangUtil.equalsClassNames(name, alias)) { |
| 96 | + referencesToReplace.put(name, alias); |
| 97 | + } |
| 98 | + } else if (!autoImport) { |
| 99 | + referencesToReplace.put(name, originalFqn); |
| 100 | + } else { |
| 101 | + String importedFqn = aliases.get(name); |
| 102 | + if (!PhpLangUtil.equalsClassNames(importedFqn, originalFqn)) { |
| 103 | + if (importedFqn != null) { |
| 104 | + String originalName = PhpLangUtil.toShortName(originalFqn); |
| 105 | + String fqnForOriginalName = aliases.get(originalName); |
| 106 | + if (fqnForOriginalName != null && !PhpLangUtil.equalsClassNames(fqnForOriginalName, originalFqn)) { |
| 107 | + referencesToReplace.put(name, originalFqn); |
| 108 | + } else { |
| 109 | + referencesToReplace.put(name, originalName); |
| 110 | + if (fqnForOriginalName == null) { |
| 111 | + insertUseStatement(scopeHolder, originalName, originalFqn); |
| 112 | + } |
| 113 | + } |
| 114 | + } else { |
| 115 | + insertUseStatement(scopeHolder, name, originalFqn); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return referencesToReplace; |
| 122 | + } |
| 123 | + |
| 124 | + private static void insertUseStatement(PhpPsiElement scopeHolder, String name, String originalFqn) { |
| 125 | + String originalName = PhpLangUtil.toShortName(originalFqn); |
| 126 | + if (PhpLangUtil.equalsClassNames(originalName, name)) { |
| 127 | + PhpAliasImporter.insertUseStatement(originalFqn, scopeHolder); |
| 128 | + return; |
| 129 | + } |
| 130 | + PhpAliasImporter.insertUseStatement(originalFqn, name, scopeHolder); |
| 131 | + } |
| 132 | + |
| 133 | + private static class PhpClassReferenceRenamer extends PhpClassReferenceExtractor { |
| 134 | + private final Project myProject; |
| 135 | + private final Map<String, String> myRefToRename; |
| 136 | + |
| 137 | + private PhpClassReferenceRenamer(Project project, @NotNull Map<String, String> replaceWithFqn) { |
| 138 | + super(); |
| 139 | + this.myProject = project; |
| 140 | + this.myRefToRename = replaceWithFqn; |
| 141 | + } |
| 142 | + |
| 143 | + protected void processReference(@NotNull String name, @NotNull String reference, @NotNull PsiElement identifier) { |
| 144 | + if (this.myRefToRename.containsKey(name)) { |
| 145 | + String fqn = this.myRefToRename.get(name); |
| 146 | + Object newReference; |
| 147 | + PsiElement oldReference; |
| 148 | + if (!PhpPsiUtil.isOfType(identifier, PhpTokenTypes.IDENTIFIER) && !(identifier instanceof ClassReference)) { |
| 149 | + newReference = PhpPsiElementFactory.createPhpDocType(this.myProject, fqn); |
| 150 | + oldReference = PhpPsiUtil.getParentByCondition(identifier, false, PhpDocType.INSTANCEOF, PhpDocComment.INSTANCEOF); |
| 151 | + } else { |
| 152 | + newReference = PhpPsiElementFactory.createClassReference(this.myProject, fqn); |
| 153 | + oldReference = PhpPsiUtil.getParentByCondition(identifier, false, ClassReference.INSTANCEOF, Statement.INSTANCEOF); |
| 154 | + } |
| 155 | + |
| 156 | + assert oldReference != null; |
| 157 | + |
| 158 | + PsiElement added = oldReference.addRange(((PsiElement)newReference).getFirstChild(), ((PsiElement)newReference).getLastChild()); |
| 159 | + oldReference.deleteChildRange(oldReference.getFirstChild(), added.getPrevSibling()); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments