Skip to content

Commit de85f20

Browse files
Code refactoring after code review
1 parent 9c50ff0 commit de85f20

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/**
22
${PARAM_DOC}
3-
#if (${RETURN_VARIABLES})* @return array
4-
#end
53
*/
64
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
75
{
86
// TODO: Implement plugin method.
9-
#if (${RETURN_VARIABLES})return [${RETURN_VARIABLES}]; #end
7+
#if (${RETURN_VARIABLES})return [${RETURN_VARIABLES}];#else return [];#end
108
}

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import com.jetbrains.php.lang.psi.elements.Parameter;
1818
import com.jetbrains.php.lang.psi.elements.PhpClass;
1919
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
20-
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
2120
import com.magento.idea.magento2plugin.actions.generation.data.code.PluginMethodData;
2221
import com.magento.idea.magento2plugin.actions.generation.generator.code.util.ConvertPluginParamsToPhpDocStringUtil;
2322
import com.magento.idea.magento2plugin.actions.generation.generator.code.util.ConvertPluginParamsToString;
23+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
2424
import com.magento.idea.magento2plugin.magento.files.Plugin;
2525
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
26+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collection;
@@ -123,12 +124,15 @@ private Properties getAccessMethodAttributes(
123124
attributes,
124125
type
125126
);
126-
this.addReturnType(attributes, pluginMethodReturnType);
127+
128+
if (pluginMethodReturnType != null) {
129+
this.addReturnType(attributes, pluginMethodReturnType);
130+
}
127131

128132
return attributes;
129133
}
130134

131-
private @NotNull String fillAttributes(
135+
private String fillAttributes(
132136
final @Nullable PhpPsiElement scopeForUseOperator,
133137
final Properties attributes,
134138
final @NotNull Plugin.PluginType type
@@ -139,18 +143,17 @@ private Properties getAccessMethodAttributes(
139143
final String pluginMethodName = type.toString().concat(methodSuffix);
140144

141145
attributes.setProperty("NAME", pluginMethodName);
142-
final PhpReturnType targetMethodReturnType = myMethod.getReturnType();
143-
String returnType = "";
146+
String returnType = PhpTypeMetadataParserUtil.getMethodReturnType(this.myMethod);
147+
148+
if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
149+
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
150+
}
144151

145-
if (targetMethodReturnType != null
146-
&& (type.equals(Plugin.PluginType.after) || type.equals(Plugin.PluginType.around))
147-
) {
148-
returnType = targetMethodReturnType.getText();
149-
} else if (type.equals(Plugin.PluginType.before)) {
152+
if (type.equals(Plugin.PluginType.before)) {
150153
returnType = MagentoPhpClass.ARRAY_TYPE;
151154
}
152155

153-
final Collection<PsiElement> parameters = new ArrayList();
156+
final Collection<PsiElement> parameters = new ArrayList<>();
154157
parameters.add(myTargetClass);
155158
parameters.addAll(Arrays.asList(myMethod.getParameters()));
156159
attributes.setProperty("PARAM_DOC", ConvertPluginParamsToPhpDocStringUtil.execute(

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
import com.jetbrains.php.lang.psi.elements.Method;
1414
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
1515
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
16-
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
1716
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
1817
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
1918
import com.magento.idea.magento2plugin.magento.files.Plugin;
2019
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
20+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
2121
import java.util.Collection;
2222
import org.jetbrains.annotations.NotNull;
2323
import org.jetbrains.annotations.Nullable;
@@ -43,7 +43,19 @@ public static String execute(
4343
final Method myMethod
4444
) {
4545
final StringBuilder stringBuilder = new StringBuilder(155);
46-
final PhpReturnType returnType = myMethod.getReturnType();
46+
String returnType;
47+
48+
if (type.equals(Plugin.PluginType.before)) {
49+
returnType = MagentoPhpClass.ARRAY_TYPE;
50+
} else {
51+
returnType = PhpTypeMetadataParserUtil.getMethodReturnType(myMethod);
52+
53+
if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
54+
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
55+
}
56+
}
57+
58+
final PhpType returnPhpType = new PhpType().add(returnType);
4759
int increment = 0;
4860

4961
for (final PsiElement parameter : parameters) {
@@ -81,12 +93,12 @@ public static String execute(
8193

8294
if (type.equals(Plugin.PluginType.after) && increment == 0) {
8395
if (returnType != null) {
84-
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
96+
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
8597
stringBuilder.append("\n* @param null $result");
8698
} else {
8799
final String returnTypeStr = getStringTypePresentation(
88100
project,
89-
returnType.getType(),
101+
returnPhpType,
90102
null
91103
);
92104
stringBuilder.append("\n* @param ")
@@ -102,13 +114,13 @@ public static String execute(
102114
increment++;
103115
}
104116

105-
if (!type.equals(Plugin.PluginType.before) && returnType != null) {
106-
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
117+
if (returnType != null) {
118+
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
107119
stringBuilder.append("\n* @return ").append(MagentoPhpClass.VOID_RETURN_TYPE);
108120
} else {
109121
final String returnTypeStr = getStringTypePresentation(
110122
project,
111-
returnType.getType(),
123+
returnPhpType,
112124
null
113125
);
114126
stringBuilder.append("\n* @return ").append(returnTypeStr);

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import com.jetbrains.php.lang.psi.elements.Parameter;
1818
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
1919
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
20-
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
2120
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
2221
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
2322
import com.magento.idea.magento2plugin.magento.files.Plugin;
2423
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
2524
import com.magento.idea.magento2plugin.magento.packages.Package;
25+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
2626
import java.util.Collection;
2727
import java.util.Iterator;
2828
import java.util.Set;
@@ -49,7 +49,11 @@ public static String execute(
4949
final Method myMethod
5050
) {
5151
final StringBuilder buf = new StringBuilder(155);
52-
final PhpReturnType returnType = myMethod.getReturnType();
52+
String returnType = PhpTypeMetadataParserUtil.getMethodReturnType(myMethod);
53+
54+
if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
55+
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
56+
}
5357
final Iterator<PsiElement> parametersIterator = parameters.iterator();
5458
int iterator = 0;
5559

@@ -62,7 +66,7 @@ public static String execute(
6266
if (element instanceof Parameter) {
6367
String parameterText = PhpCodeUtil.paramToString(element);
6468

65-
if (parameterText.indexOf(Package.fqnSeparator, 1) > 0) {
69+
if (parameterText.contains(Package.fqnSeparator)) {
6670
final String[] fqnArray = parameterText.split("\\\\");
6771
parameterText = fqnArray[fqnArray.length - 1];
6872
}
@@ -92,8 +96,8 @@ public static String execute(
9296

9397
if (type.equals(Plugin.PluginType.after) && iterator == 0) {
9498
if (returnType != null
95-
&& !returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
96-
buf.append(", ").append(returnType.getText()).append(" $result");
99+
&& !returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
100+
buf.append(", ").append(returnType).append(" $result");
97101
} else {
98102
buf.append(", $result");
99103
}

src/com/magento/idea/magento2plugin/util/php/PhpTypeMetadataParserUtil.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ public static String getMethodDefinitionForInterface(
230230
public static List<String> getMethodDefinitionPhpTypes(final @NotNull Method method) {
231231
final List<String> types = new ArrayList<>(getMethodParametersTypes(method));
232232

233-
final String returnType = getMethodReturnPhpType(method);
233+
final String returnType = getMethodReturnType(method);
234234

235-
if (returnType != null) {
236-
types.add(returnType);
235+
if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)
236+
&& !PhpLangUtil.isReturnTypeHint(returnType, method.getProject())) {
237+
types.add(StringUtils.stripStart(returnType, "\\"));
237238
}
238239
types.addAll(getMethodExceptionsTypes(method));
239240

@@ -275,16 +276,20 @@ public static List<String> getMethodParametersTypes(final @NotNull Method method
275276
*
276277
* @return String
277278
*/
278-
public static String getMethodReturnPhpType(final @NotNull Method method) {
279+
public static String getMethodReturnType(final @NotNull Method method) {
279280
if (method.getContainingClass() == null) {
280281
return null;
281282
}
282283
final List<String> returnedTypes =
283284
extractMultipleTypesFromString(method.getType().toString());
284285

285286
for (final String returnedType : returnedTypes) {
287+
if (PhpLangUtil.isReturnTypeHint(returnedType, method.getProject())) {
288+
return returnedType;
289+
}
290+
286291
if (PhpLangUtil.isFqn(returnedType)) {
287-
return StringUtils.stripStart(returnedType, "\\");
292+
return returnedType;
288293
}
289294
final String guessedByName =
290295
getUsedInPhpClassTypeByName(returnedType, method.getContainingClass());

0 commit comments

Comments
 (0)