Skip to content

Commit 6d8df51

Browse files
authored
Merge pull request #612 from bohdan-harniuk/fix-plugin-generation
Fixed plugin generation issues
2 parents 608a819 + 7e603ab commit 6d8df51

File tree

11 files changed

+326
-142
lines changed

11 files changed

+326
-142
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/**
22
${PARAM_DOC}
33
*/
4-
public function ${NAME}(${PARAM_LIST})
4+
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
55
{
66
// TODO: Implement plugin method.
7+
#if (${RETURN_TYPE})
8+
#if (${RETURN_VARIABLES} && ${RETURN_TYPE} != "void")return ${RETURN_VARIABLES}; #end
9+
#else
710
#if (${RETURN_VARIABLES})return ${RETURN_VARIABLES}; #end
11+
#end
812
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/**
22
${PARAM_DOC}
33
*/
4-
public function ${NAME}(${PARAM_LIST})
4+
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
55
{
66
// TODO: Implement plugin method.
7+
#if (${RETURN_TYPE})
8+
#if (${RETURN_TYPE} != "void")return #end$proceed(#if(${RETURN_VARIABLES})${RETURN_VARIABLES}#end);
9+
#else
710
return $proceed(#if(${RETURN_VARIABLES})${RETURN_VARIABLES}#end);
11+
#end
812
}
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
*/
6-
public function ${NAME}(${PARAM_LIST})
4+
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/PluginClassGenerator.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,32 @@ public String getPluginModule() {
242242
return pluginFileData.getPluginModule();
243243
}
244244

245+
/**
246+
* Get methods insert position.
247+
*
248+
* @param pluginClass PhpClass
249+
*
250+
* @return int
251+
*/
245252
private int getInsertPos(final PhpClass pluginClass) {
246253
int insertPos = -1;
254+
247255
final LeafPsiElement[] leafElements = PsiTreeUtil.getChildrenOfType(
248256
pluginClass,
249257
LeafPsiElement.class
250258
);
259+
260+
if (leafElements == null) {
261+
return insertPos;
262+
}
263+
251264
for (final LeafPsiElement leafPsiElement: leafElements) {
252265
if (!leafPsiElement.getText().equals(MagentoPhpClass.CLOSING_TAG)) {
253266
continue;
254267
}
255268
insertPos = leafPsiElement.getTextOffset();
256269
}
257-
return insertPos;
270+
271+
return insertPos == -1 ? insertPos : insertPos - 1;
258272
}
259273
}

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

+69-26
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;
@@ -105,17 +106,32 @@ public PluginMethodData[] createPluginMethods(final @NotNull Plugin.PluginType t
105106
return pluginMethods.toArray(new PluginMethodData[0]);
106107
}
107108

109+
/**
110+
* Fill attributes for plugin method.
111+
*
112+
* @param scopeForUseOperator PhpPsiElement
113+
* @param type Plugin.PluginType
114+
*
115+
* @return Properties
116+
*/
108117
private Properties getAccessMethodAttributes(
109118
final @Nullable PhpPsiElement scopeForUseOperator,
110119
final @NotNull Plugin.PluginType type
111120
) {
112121
final Properties attributes = new Properties();
113-
final String typeHint = this.fillAttributes(scopeForUseOperator, attributes, type);
114-
this.addTypeHintsAndReturnType(attributes, typeHint);
122+
final String pluginMethodReturnType = this.fillAttributes(
123+
scopeForUseOperator,
124+
attributes,
125+
type
126+
);
127+
128+
if (pluginMethodReturnType != null) {
129+
this.addReturnType(attributes, pluginMethodReturnType);
130+
}
131+
115132
return attributes;
116133
}
117134

118-
@NotNull
119135
private String fillAttributes(
120136
final @Nullable PhpPsiElement scopeForUseOperator,
121137
final Properties attributes,
@@ -127,12 +143,17 @@ private String fillAttributes(
127143
final String pluginMethodName = type.toString().concat(methodSuffix);
128144

129145
attributes.setProperty("NAME", pluginMethodName);
130-
final PhpReturnType targetMethodReturnType = myMethod.getReturnType();
131-
String typeHint = "";
132-
if (targetMethodReturnType != null) {
133-
typeHint = targetMethodReturnType.getText();
146+
String returnType = PhpTypeMetadataParserUtil.getMethodReturnType(this.myMethod);
147+
148+
if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
149+
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
150+
}
151+
152+
if (type.equals(Plugin.PluginType.before)) {
153+
returnType = MagentoPhpClass.ARRAY_TYPE;
134154
}
135-
final Collection<PsiElement> parameters = new ArrayList();
155+
156+
final Collection<PsiElement> parameters = new ArrayList<>();
136157
parameters.add(myTargetClass);
137158
parameters.addAll(Arrays.asList(myMethod.getParameters()));
138159
attributes.setProperty("PARAM_DOC", ConvertPluginParamsToPhpDocStringUtil.execute(
@@ -148,29 +169,35 @@ private String fillAttributes(
148169
attributes.setProperty("RETURN_VARIABLES", returnVariables);
149170
}
150171

151-
return typeHint;
172+
return returnType;
152173
}
153174

154-
private void addTypeHintsAndReturnType(final Properties attributes, final String typeHint) {
175+
/**
176+
* Add return type to the properties.
177+
*
178+
* @param attributes Properties
179+
* @param returnDocType String
180+
*/
181+
private void addReturnType(final Properties attributes, final String returnDocType) {
155182
final Project project = this.pluginClass.getProject();
156-
if (PhpLanguageFeature.SCALAR_TYPE_HINTS.isSupported(project)
157-
&& isDocTypeConvertable(typeHint)) {
158-
attributes.setProperty("SCALAR_TYPE_HINT", convertDocTypeToHint(project, typeHint));
159-
}
160-
161-
final boolean hasFeatureVoid = PhpLanguageFeature.RETURN_VOID.isSupported(project);
162-
if (hasFeatureVoid) {
163-
attributes.setProperty("VOID_RETURN_TYPE", MagentoPhpClass.VOID_RETURN_TYPE);
164-
}
165183

166184
if (PhpLanguageFeature.RETURN_TYPES.isSupported(project)
167-
&& isDocTypeConvertable(typeHint)) {
168-
attributes.setProperty("RETURN_TYPE", convertDocTypeToHint(project, typeHint));
185+
&& isDocTypeConvertable(returnDocType)) {
186+
attributes.setProperty(
187+
"RETURN_TYPE",
188+
convertDocTypeToHint(project, returnDocType)
189+
);
169190
}
170-
171191
}
172192

173-
private static boolean isDocTypeConvertable(final String typeHint) {
193+
/**
194+
* Check if PHP DOC type could be converted to scalar PHP type.
195+
*
196+
* @param typeHint String
197+
*
198+
* @return boolean
199+
*/
200+
private static boolean isDocTypeConvertable(final @NotNull String typeHint) {
174201
return !typeHint.equalsIgnoreCase(MagentoPhpClass.MIXED_RETURN_TYPE)
175202
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.STATIC_MODIFIER)
176203
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.PHP_TRUE)
@@ -193,14 +220,30 @@ private static String convertNullableType(final Project project, final String ty
193220
: (hasNullableTypeFeature ? "?" : "") + split[0];
194221
}
195222

196-
@NotNull
197-
private static String convertDocTypeToHint(final Project project, final String typeHint) {
223+
/**
224+
* Convert PHP DOC type to hint.
225+
*
226+
* @param project Project
227+
* @param typeHint String
228+
*
229+
* @return String
230+
*/
231+
private static @NotNull String convertDocTypeToHint(
232+
final @NotNull Project project,
233+
final @NotNull String typeHint
234+
) {
198235
String hint = typeHint.contains("[]") ? "array" : typeHint;
199236
hint = hint.contains("boolean") ? "bool" : hint;
237+
200238
if (typeWithNull(typeHint)) {
201239
hint = convertNullableType(project, hint);
202240
}
203241

242+
if (PhpLanguageFeature.RETURN_VOID.isSupported(project)
243+
&& typeHint.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
244+
hint = MagentoPhpClass.VOID_RETURN_TYPE;
245+
}
246+
204247
return hint;
205248
}
206249

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

+68-20
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
import com.intellij.openapi.project.Project;
99
import com.intellij.psi.PsiElement;
1010
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
11+
import com.jetbrains.php.lang.PhpLangUtil;
1112
import com.jetbrains.php.lang.documentation.phpdoc.PhpDocUtil;
1213
import com.jetbrains.php.lang.psi.elements.Method;
1314
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
14-
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
15+
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
16+
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
17+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
1518
import com.magento.idea.magento2plugin.magento.files.Plugin;
1619
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
17-
import com.magento.idea.magento2plugin.magento.packages.Package;
20+
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
1821
import java.util.Collection;
1922
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
2024

2125
public final class ConvertPluginParamsToPhpDocStringUtil {
2226

@@ -38,36 +42,46 @@ public static String execute(
3842
final Project project,
3943
final Method myMethod
4044
) {
41-
final StringBuilder stringBuilder = new StringBuilder();//NOPMD
42-
final PhpReturnType returnType = myMethod.getReturnType();
45+
final StringBuilder stringBuilder = new StringBuilder(155);
46+
String returnType;
4347

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);
4459
int increment = 0;
60+
4561
for (final PsiElement parameter : parameters) {
4662
final PhpNamedElement element = (PhpNamedElement) parameter;
63+
4764
if (stringBuilder.length() > 0) {
4865
stringBuilder.append('\n');
4966
}
50-
5167
stringBuilder.append("* @param ");
52-
5368
String typeStr;
69+
5470
if (increment == 0) {
55-
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), null);
71+
typeStr = getStringTypePresentation(project, element.getType(), null);
5672
} else {
57-
typeStr = PhpDocUtil.getTypePresentation(
58-
project, element.getType(),
59-
PhpCodeInsightUtil.findScopeForUseOperator(element)
73+
typeStr = getStringTypePresentation(
74+
project,
75+
element.getType(),
76+
PhpCodeInsightUtil.findScopeForUseOperator(element)
6077
);
61-
if (typeStr.indexOf(Package.fqnSeparator, 1) > 0) {
62-
final String[] fqnArray = typeStr.split("\\\\");
63-
typeStr = fqnArray[fqnArray.length - 1];
64-
}
6578
}
6679

6780
if (!typeStr.isEmpty()) {
6881
stringBuilder.append(typeStr).append(' ');
6982
}
7083
String paramName = element.getName();
84+
7185
if (increment == 0) {
7286
paramName = "subject";
7387
}
@@ -76,30 +90,64 @@ public static String execute(
7690
if (type.equals(Plugin.PluginType.around) && increment == 0) {
7791
stringBuilder.append("\n* @param callable $proceed");
7892
}
93+
7994
if (type.equals(Plugin.PluginType.after) && increment == 0) {
8095
if (returnType != null) {
81-
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
96+
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
8297
stringBuilder.append("\n* @param null $result");
8398
} else {
84-
stringBuilder.append("\n* @param ").append(returnType.getText())
99+
final String returnTypeStr = getStringTypePresentation(
100+
project,
101+
returnPhpType,
102+
null
103+
);
104+
stringBuilder.append("\n* @param ")
105+
.append(returnTypeStr)
85106
.append(" $result");
86107
}
108+
increment++;
87109
continue;
88110
}
89-
90111
stringBuilder.append("\n* @param $result");
91112
}
92113

93114
increment++;
94115
}
95116

96-
if (!type.equals(Plugin.PluginType.before) && returnType != null) {
97-
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
117+
if (returnType != null) {
118+
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
98119
stringBuilder.append("\n* @return ").append(MagentoPhpClass.VOID_RETURN_TYPE);
99120
} else {
100-
stringBuilder.append("\n* @return ").append(returnType.getText());
121+
final String returnTypeStr = getStringTypePresentation(
122+
project,
123+
returnPhpType,
124+
null
125+
);
126+
stringBuilder.append("\n* @return ").append(returnTypeStr);
101127
}
102128
}
103129
return stringBuilder.toString();
104130
}
131+
132+
/**
133+
* Get string type representation in the DOC format.
134+
*
135+
* @param project Project
136+
* @param type PhpType
137+
*
138+
* @return String
139+
*/
140+
private static String getStringTypePresentation(
141+
final @NotNull Project project,
142+
final @NotNull PhpType type,
143+
final @Nullable PhpPsiElement scope
144+
) {
145+
String typeStr = PhpDocUtil.getTypePresentation(project, type, scope);
146+
147+
if (PhpLangUtil.isFqn(typeStr)) {
148+
typeStr = PhpClassGeneratorUtil.getNameFromFqn(typeStr);
149+
}
150+
151+
return typeStr;
152+
}
105153
}

0 commit comments

Comments
 (0)