Skip to content

Commit b5bcd0d

Browse files
Fixed plugin generation issues
1 parent 2c59bfb commit b5bcd0d

File tree

7 files changed

+235
-94
lines changed

7 files changed

+235
-94
lines changed
Lines changed: 5 additions & 1 deletion
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
}
Lines changed: 5 additions & 1 deletion
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
}

resources/fileTemplates/code/Magento Plugin Before Method.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ${PARAM_DOC}
33
#if (${RETURN_VARIABLES})* @return array
44
#end
55
*/
6-
public function ${NAME}(${PARAM_LIST})
6+
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
77
{
88
// TODO: Implement plugin method.
99
#if (${RETURN_VARIABLES})return [${RETURN_VARIABLES}]; #end

src/com/magento/idea/magento2plugin/actions/generation/generator/PluginClassGenerator.java

Lines changed: 15 additions & 1 deletion
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

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,30 @@ public PluginMethodData[] createPluginMethods(final @NotNull Plugin.PluginType t
105105
return pluginMethods.toArray(new PluginMethodData[0]);
106106
}
107107

108+
/**
109+
* Fill attributes for plugin method.
110+
*
111+
* @param scopeForUseOperator PhpPsiElement
112+
* @param type Plugin.PluginType
113+
*
114+
* @return Properties
115+
*/
108116
private Properties getAccessMethodAttributes(
109117
final @Nullable PhpPsiElement scopeForUseOperator,
110118
final @NotNull Plugin.PluginType type
111119
) {
112120
final Properties attributes = new Properties();
113-
final String typeHint = this.fillAttributes(scopeForUseOperator, attributes, type);
114-
this.addTypeHintsAndReturnType(attributes, typeHint);
121+
final String pluginMethodReturnType = this.fillAttributes(
122+
scopeForUseOperator,
123+
attributes,
124+
type
125+
);
126+
this.addReturnType(attributes, pluginMethodReturnType);
127+
115128
return attributes;
116129
}
117130

118-
@NotNull
119-
private String fillAttributes(
131+
private @NotNull String fillAttributes(
120132
final @Nullable PhpPsiElement scopeForUseOperator,
121133
final Properties attributes,
122134
final @NotNull Plugin.PluginType type
@@ -128,10 +140,16 @@ private String fillAttributes(
128140

129141
attributes.setProperty("NAME", pluginMethodName);
130142
final PhpReturnType targetMethodReturnType = myMethod.getReturnType();
131-
String typeHint = "";
132-
if (targetMethodReturnType != null) {
133-
typeHint = targetMethodReturnType.getText();
143+
String returnType = "";
144+
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)) {
150+
returnType = MagentoPhpClass.ARRAY_TYPE;
134151
}
152+
135153
final Collection<PsiElement> parameters = new ArrayList();
136154
parameters.add(myTargetClass);
137155
parameters.addAll(Arrays.asList(myMethod.getParameters()));
@@ -148,29 +166,35 @@ private String fillAttributes(
148166
attributes.setProperty("RETURN_VARIABLES", returnVariables);
149167
}
150168

151-
return typeHint;
169+
return returnType;
152170
}
153171

154-
private void addTypeHintsAndReturnType(final Properties attributes, final String typeHint) {
172+
/**
173+
* Add return type to the properties.
174+
*
175+
* @param attributes Properties
176+
* @param returnDocType String
177+
*/
178+
private void addReturnType(final Properties attributes, final String returnDocType) {
155179
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-
}
165180

166181
if (PhpLanguageFeature.RETURN_TYPES.isSupported(project)
167-
&& isDocTypeConvertable(typeHint)) {
168-
attributes.setProperty("RETURN_TYPE", convertDocTypeToHint(project, typeHint));
182+
&& isDocTypeConvertable(returnDocType)) {
183+
attributes.setProperty(
184+
"RETURN_TYPE",
185+
convertDocTypeToHint(project, returnDocType)
186+
);
169187
}
170-
171188
}
172189

173-
private static boolean isDocTypeConvertable(final String typeHint) {
190+
/**
191+
* Check if PHP DOC type could be converted to scalar PHP type.
192+
*
193+
* @param typeHint String
194+
*
195+
* @return boolean
196+
*/
197+
private static boolean isDocTypeConvertable(final @NotNull String typeHint) {
174198
return !typeHint.equalsIgnoreCase(MagentoPhpClass.MIXED_RETURN_TYPE)
175199
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.STATIC_MODIFIER)
176200
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.PHP_TRUE)
@@ -193,14 +217,30 @@ private static String convertNullableType(final Project project, final String ty
193217
: (hasNullableTypeFeature ? "?" : "") + split[0];
194218
}
195219

196-
@NotNull
197-
private static String convertDocTypeToHint(final Project project, final String typeHint) {
220+
/**
221+
* Convert PHP DOC type to hint.
222+
*
223+
* @param project Project
224+
* @param typeHint String
225+
*
226+
* @return String
227+
*/
228+
private static @NotNull String convertDocTypeToHint(
229+
final @NotNull Project project,
230+
final @NotNull String typeHint
231+
) {
198232
String hint = typeHint.contains("[]") ? "array" : typeHint;
199233
hint = hint.contains("boolean") ? "bool" : hint;
234+
200235
if (typeWithNull(typeHint)) {
201236
hint = convertNullableType(project, hint);
202237
}
203238

239+
if (PhpLanguageFeature.RETURN_VOID.isSupported(project)
240+
&& typeHint.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
241+
hint = MagentoPhpClass.VOID_RETURN_TYPE;
242+
}
243+
204244
return hint;
205245
}
206246

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

Lines changed: 52 additions & 16 deletions
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;
15+
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
1416
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
17+
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
18+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
1519
import com.magento.idea.magento2plugin.magento.files.Plugin;
1620
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
17-
import com.magento.idea.magento2plugin.magento.packages.Package;
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,34 @@ public static String execute(
3842
final Project project,
3943
final Method myMethod
4044
) {
41-
final StringBuilder stringBuilder = new StringBuilder();//NOPMD
45+
final StringBuilder stringBuilder = new StringBuilder(155);
4246
final PhpReturnType returnType = myMethod.getReturnType();
43-
4447
int increment = 0;
48+
4549
for (final PsiElement parameter : parameters) {
4650
final PhpNamedElement element = (PhpNamedElement) parameter;
51+
4752
if (stringBuilder.length() > 0) {
4853
stringBuilder.append('\n');
4954
}
50-
5155
stringBuilder.append("* @param ");
52-
5356
String typeStr;
57+
5458
if (increment == 0) {
55-
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), null);
59+
typeStr = getStringTypePresentation(project, element.getType(), null);
5660
} else {
57-
typeStr = PhpDocUtil.getTypePresentation(
58-
project, element.getType(),
59-
PhpCodeInsightUtil.findScopeForUseOperator(element)
61+
typeStr = getStringTypePresentation(
62+
project,
63+
element.getType(),
64+
PhpCodeInsightUtil.findScopeForUseOperator(element)
6065
);
61-
if (typeStr.indexOf(Package.fqnSeparator, 1) > 0) {
62-
final String[] fqnArray = typeStr.split("\\\\");
63-
typeStr = fqnArray[fqnArray.length - 1];
64-
}
6566
}
6667

6768
if (!typeStr.isEmpty()) {
6869
stringBuilder.append(typeStr).append(' ');
6970
}
7071
String paramName = element.getName();
72+
7173
if (increment == 0) {
7274
paramName = "subject";
7375
}
@@ -76,17 +78,24 @@ public static String execute(
7678
if (type.equals(Plugin.PluginType.around) && increment == 0) {
7779
stringBuilder.append("\n* @param callable $proceed");
7880
}
81+
7982
if (type.equals(Plugin.PluginType.after) && increment == 0) {
8083
if (returnType != null) {
8184
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
8285
stringBuilder.append("\n* @param null $result");
8386
} else {
84-
stringBuilder.append("\n* @param ").append(returnType.getText())
87+
final String returnTypeStr = getStringTypePresentation(
88+
project,
89+
returnType.getType(),
90+
null
91+
);
92+
stringBuilder.append("\n* @param ")
93+
.append(returnTypeStr)
8594
.append(" $result");
8695
}
96+
increment++;
8797
continue;
8898
}
89-
9099
stringBuilder.append("\n* @param $result");
91100
}
92101

@@ -97,9 +106,36 @@ public static String execute(
97106
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
98107
stringBuilder.append("\n* @return ").append(MagentoPhpClass.VOID_RETURN_TYPE);
99108
} else {
100-
stringBuilder.append("\n* @return ").append(returnType.getText());
109+
final String returnTypeStr = getStringTypePresentation(
110+
project,
111+
returnType.getType(),
112+
null
113+
);
114+
stringBuilder.append("\n* @return ").append(returnTypeStr);
101115
}
102116
}
103117
return stringBuilder.toString();
104118
}
119+
120+
/**
121+
* Get string type representation in the DOC format.
122+
*
123+
* @param project Project
124+
* @param type PhpType
125+
*
126+
* @return String
127+
*/
128+
private static String getStringTypePresentation(
129+
final @NotNull Project project,
130+
final @NotNull PhpType type,
131+
final @Nullable PhpPsiElement scope
132+
) {
133+
String typeStr = PhpDocUtil.getTypePresentation(project, type, scope);
134+
135+
if (PhpLangUtil.isFqn(typeStr)) {
136+
typeStr = PhpClassGeneratorUtil.getNameFromFqn(typeStr);
137+
}
138+
139+
return typeStr;
140+
}
105141
}

0 commit comments

Comments
 (0)