Skip to content

Commit 437db74

Browse files
author
m.mezhensky
committed
#156: fix issue with wrong namespaces
1 parent 36074fb commit 437db74

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/com/magento/idea/magento2plugin/actions/generation/generator/code/PluginMethodsGenerator.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
1818
import com.magento.idea.magento2plugin.actions.generation.data.code.PluginMethodData;
1919
import com.magento.idea.magento2plugin.magento.files.Plugin;
20+
import com.magento.idea.magento2plugin.magento.packages.Package;
2021
import org.jetbrains.annotations.NotNull;
2122
import org.jetbrains.annotations.Nullable;
23+
2224
import java.util.*;
2325
import java.util.regex.Pattern;
2426

@@ -152,7 +154,17 @@ private String getParameterDoc(Collection<PsiElement> parameters, @NotNull Plugi
152154
}
153155

154156
sb.append("* @param ");
155-
String typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), PhpCodeInsightUtil.findScopeForUseOperator(element));
157+
158+
String typeStr;
159+
if (i == 0) {
160+
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), null);
161+
} else {
162+
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), PhpCodeInsightUtil.findScopeForUseOperator(element));
163+
if (typeStr.indexOf(Package.FQN_SEPARATOR, 1) > 0) {
164+
String[] fqnArray = typeStr.split("\\\\");
165+
typeStr = fqnArray[fqnArray.length - 1];
166+
}
167+
}
156168

157169
if (!typeStr.isEmpty()) {
158170
sb.append(typeStr).append(' ');
@@ -202,9 +214,19 @@ protected String getParameterList(Collection<PsiElement> parameters, @NotNull Pl
202214
}
203215

204216
if (element instanceof Parameter) {
205-
buf.append(PhpCodeUtil.paramToString(element));
217+
String parameterText = PhpCodeUtil.paramToString(element);
218+
if (parameterText.indexOf(Package.FQN_SEPARATOR, 1) > 0) {
219+
String[] fqnArray = parameterText.split("\\\\");
220+
parameterText = fqnArray[fqnArray.length - 1];
221+
}
222+
buf.append(parameterText);
206223
} else {
207-
String typeHint = this.getTypeHint(element);
224+
Boolean globalType = true;
225+
if (i == 0) {
226+
globalType = false;
227+
}
228+
229+
String typeHint = this.getTypeHint(element, globalType);
208230
if (typeHint != null && !typeHint.isEmpty()) {
209231
buf.append(typeHint).append(' ');
210232
}
@@ -257,21 +279,21 @@ protected String getReturnVariables(@NotNull Plugin.PluginType type) {
257279
}
258280

259281
@Nullable
260-
private String getTypeHint(@NotNull PhpNamedElement element) {
282+
private String getTypeHint(@NotNull PhpNamedElement element, Boolean globalType) {
261283
PhpType filedType = element.getType().global(this.pluginClass.getProject());
262284
Set<String> typeStrings = filedType.getTypes();
263285
String typeString = null;
264286
if (typeStrings.size() == 1) {
265-
typeString = this.convertTypeToString(element, typeStrings);
287+
typeString = this.convertTypeToString(element, typeStrings, globalType);
266288
}
267289

268290
if (typeStrings.size() == 2) {
269291
PhpType filteredNullType = filterNullCaseInsensitive(filedType);
270292
if (filteredNullType.getTypes().size() == 1) {
271293
if (PhpLanguageFeature.NULLABLES.isSupported(element.getProject())) {
272-
typeString = "?" + this.convertTypeToString(element, filteredNullType.getTypes());
294+
typeString = "?" + this.convertTypeToString(element, filteredNullType.getTypes(), globalType);
273295
} else {
274-
typeString = this.convertTypeToString(element, filteredNullType.getTypes());
296+
typeString = this.convertTypeToString(element, filteredNullType.getTypes(), globalType);
275297
}
276298
}
277299
}
@@ -280,11 +302,11 @@ private String getTypeHint(@NotNull PhpNamedElement element) {
280302
}
281303

282304
@Nullable
283-
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings) {
305+
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings, Boolean globalType) {
284306
String simpleType = typeStrings.iterator().next();
285307
simpleType = StringUtil.trimStart(simpleType, "\\");
286308
if (!PhpType.isPrimitiveType(simpleType) || PhpLanguageFeature.SCALAR_TYPE_HINTS.isSupported(element.getProject()) || "array".equalsIgnoreCase(simpleType) || "callable".equalsIgnoreCase(simpleType)) {
287-
String typeString = simpleType.endsWith("]") ? "array" : this.getFieldTypeString(element, filterNullCaseInsensitive(element.getType()));
309+
String typeString = simpleType.endsWith("]") ? "array" : this.getFieldTypeString(element, filterNullCaseInsensitive(element.getType()), globalType);
288310
if (!typeString.isEmpty()) {
289311
return typeString;
290312
}
@@ -293,8 +315,9 @@ private String convertTypeToString(@NotNull PhpNamedElement element, Set<String>
293315
return null;
294316
}
295317

296-
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type) {
297-
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, PhpCodeInsightUtil.findScopeForUseOperator(element));
318+
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type, Boolean globalType) {
319+
PhpPsiElement scope = (globalType) ? PhpCodeInsightUtil.findScopeForUseOperator(element) : null;
320+
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, scope);
298321
}
299322

300323
private static PhpType filterNullCaseInsensitive(PhpType filedType) {

0 commit comments

Comments
 (0)