Skip to content

Commit f0a09fc

Browse files
author
Vitaliy
authored
Merge pull request #185 from mmezhensky/issue-156-with-wrong-namespaces
#156: fix issue with wrong namespaces
2 parents 36074fb + 6a4b2d0 commit f0a09fc

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

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

Lines changed: 30 additions & 12 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,15 @@ 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 = i != 0;
225+
String typeHint = this.getTypeHint(element, globalType);
208226
if (typeHint != null && !typeHint.isEmpty()) {
209227
buf.append(typeHint).append(' ');
210228
}
@@ -257,21 +275,20 @@ protected String getReturnVariables(@NotNull Plugin.PluginType type) {
257275
}
258276

259277
@Nullable
260-
private String getTypeHint(@NotNull PhpNamedElement element) {
278+
private String getTypeHint(@NotNull PhpNamedElement element, Boolean globalType) {
261279
PhpType filedType = element.getType().global(this.pluginClass.getProject());
262280
Set<String> typeStrings = filedType.getTypes();
263281
String typeString = null;
264282
if (typeStrings.size() == 1) {
265-
typeString = this.convertTypeToString(element, typeStrings);
283+
typeString = this.convertTypeToString(element, typeStrings, globalType);
266284
}
267285

268286
if (typeStrings.size() == 2) {
269287
PhpType filteredNullType = filterNullCaseInsensitive(filedType);
270288
if (filteredNullType.getTypes().size() == 1) {
289+
typeString = this.convertTypeToString(element, filteredNullType.getTypes(), globalType);
271290
if (PhpLanguageFeature.NULLABLES.isSupported(element.getProject())) {
272-
typeString = "?" + this.convertTypeToString(element, filteredNullType.getTypes());
273-
} else {
274-
typeString = this.convertTypeToString(element, filteredNullType.getTypes());
291+
typeString = "?" + typeString;
275292
}
276293
}
277294
}
@@ -280,11 +297,11 @@ private String getTypeHint(@NotNull PhpNamedElement element) {
280297
}
281298

282299
@Nullable
283-
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings) {
300+
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings, Boolean globalType) {
284301
String simpleType = typeStrings.iterator().next();
285302
simpleType = StringUtil.trimStart(simpleType, "\\");
286303
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()));
304+
String typeString = simpleType.endsWith("]") ? "array" : this.getFieldTypeString(element, filterNullCaseInsensitive(element.getType()), globalType);
288305
if (!typeString.isEmpty()) {
289306
return typeString;
290307
}
@@ -293,8 +310,9 @@ private String convertTypeToString(@NotNull PhpNamedElement element, Set<String>
293310
return null;
294311
}
295312

296-
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type) {
297-
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, PhpCodeInsightUtil.findScopeForUseOperator(element));
313+
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type, Boolean globalType) {
314+
PhpPsiElement scope = (globalType) ? PhpCodeInsightUtil.findScopeForUseOperator(element) : null;
315+
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, scope);
298316
}
299317

300318
private static PhpType filterNullCaseInsensitive(PhpType filedType) {

0 commit comments

Comments
 (0)