17
17
import com .jetbrains .php .lang .psi .elements .Parameter ;
18
18
import com .jetbrains .php .lang .psi .elements .PhpClass ;
19
19
import com .jetbrains .php .lang .psi .elements .PhpPsiElement ;
20
- import com .jetbrains .php .lang .psi .elements .PhpReturnType ;
21
20
import com .magento .idea .magento2plugin .actions .generation .data .code .PluginMethodData ;
22
21
import com .magento .idea .magento2plugin .actions .generation .generator .code .util .ConvertPluginParamsToPhpDocStringUtil ;
23
22
import com .magento .idea .magento2plugin .actions .generation .generator .code .util .ConvertPluginParamsToString ;
23
+ import com .magento .idea .magento2plugin .actions .generation .generator .util .PhpClassGeneratorUtil ;
24
24
import com .magento .idea .magento2plugin .magento .files .Plugin ;
25
25
import com .magento .idea .magento2plugin .magento .packages .MagentoPhpClass ;
26
+ import com .magento .idea .magento2plugin .util .php .PhpTypeMetadataParserUtil ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Arrays ;
28
29
import java .util .Collection ;
@@ -105,17 +106,32 @@ public PluginMethodData[] createPluginMethods(final @NotNull Plugin.PluginType t
105
106
return pluginMethods .toArray (new PluginMethodData [0 ]);
106
107
}
107
108
109
+ /**
110
+ * Fill attributes for plugin method.
111
+ *
112
+ * @param scopeForUseOperator PhpPsiElement
113
+ * @param type Plugin.PluginType
114
+ *
115
+ * @return Properties
116
+ */
108
117
private Properties getAccessMethodAttributes (
109
118
final @ Nullable PhpPsiElement scopeForUseOperator ,
110
119
final @ NotNull Plugin .PluginType type
111
120
) {
112
121
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
+
115
132
return attributes ;
116
133
}
117
134
118
- @ NotNull
119
135
private String fillAttributes (
120
136
final @ Nullable PhpPsiElement scopeForUseOperator ,
121
137
final Properties attributes ,
@@ -127,12 +143,17 @@ private String fillAttributes(
127
143
final String pluginMethodName = type .toString ().concat (methodSuffix );
128
144
129
145
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 ;
134
154
}
135
- final Collection <PsiElement > parameters = new ArrayList ();
155
+
156
+ final Collection <PsiElement > parameters = new ArrayList <>();
136
157
parameters .add (myTargetClass );
137
158
parameters .addAll (Arrays .asList (myMethod .getParameters ()));
138
159
attributes .setProperty ("PARAM_DOC" , ConvertPluginParamsToPhpDocStringUtil .execute (
@@ -148,29 +169,35 @@ private String fillAttributes(
148
169
attributes .setProperty ("RETURN_VARIABLES" , returnVariables );
149
170
}
150
171
151
- return typeHint ;
172
+ return returnType ;
152
173
}
153
174
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 ) {
155
182
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
- }
165
183
166
184
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
+ );
169
190
}
170
-
171
191
}
172
192
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 ) {
174
201
return !typeHint .equalsIgnoreCase (MagentoPhpClass .MIXED_RETURN_TYPE )
175
202
&& !typeHint .equalsIgnoreCase (MagentoPhpClass .STATIC_MODIFIER )
176
203
&& !typeHint .equalsIgnoreCase (MagentoPhpClass .PHP_TRUE )
@@ -193,14 +220,30 @@ private static String convertNullableType(final Project project, final String ty
193
220
: (hasNullableTypeFeature ? "?" : "" ) + split [0 ];
194
221
}
195
222
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
+ ) {
198
235
String hint = typeHint .contains ("[]" ) ? "array" : typeHint ;
199
236
hint = hint .contains ("boolean" ) ? "bool" : hint ;
237
+
200
238
if (typeWithNull (typeHint )) {
201
239
hint = convertNullableType (project , hint );
202
240
}
203
241
242
+ if (PhpLanguageFeature .RETURN_VOID .isSupported (project )
243
+ && typeHint .equals (MagentoPhpClass .VOID_RETURN_TYPE )) {
244
+ hint = MagentoPhpClass .VOID_RETURN_TYPE ;
245
+ }
246
+
204
247
return hint ;
205
248
}
206
249
0 commit comments