Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed plugin generation issues #612

Merged
merged 6 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
${PARAM_DOC}
*/
public function ${NAME}(${PARAM_LIST})
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
{
// TODO: Implement plugin method.
#if (${RETURN_TYPE})
#if (${RETURN_VARIABLES} && ${RETURN_TYPE} != "void")return ${RETURN_VARIABLES}; #end
#else
#if (${RETURN_VARIABLES})return ${RETURN_VARIABLES}; #end
#end
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
${PARAM_DOC}
*/
public function ${NAME}(${PARAM_LIST})
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
{
// TODO: Implement plugin method.
#if (${RETURN_TYPE})
#if (${RETURN_TYPE} != "void")return #end$proceed(#if(${RETURN_VARIABLES})${RETURN_VARIABLES}#end);
#else
return $proceed(#if(${RETURN_VARIABLES})${RETURN_VARIABLES}#end);
#end
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
${PARAM_DOC}
#if (${RETURN_VARIABLES})* @return array
#end
*/
public function ${NAME}(${PARAM_LIST})
public function ${NAME}(${PARAM_LIST})#if (${RETURN_TYPE}): ${RETURN_TYPE}#end
{
// TODO: Implement plugin method.
#if (${RETURN_VARIABLES})return [${RETURN_VARIABLES}]; #end
#if (${RETURN_VARIABLES})return [${RETURN_VARIABLES}];#else return [];#end
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,32 @@ public String getPluginModule() {
return pluginFileData.getPluginModule();
}

/**
* Get methods insert position.
*
* @param pluginClass PhpClass
*
* @return int
*/
private int getInsertPos(final PhpClass pluginClass) {
int insertPos = -1;

final LeafPsiElement[] leafElements = PsiTreeUtil.getChildrenOfType(
pluginClass,
LeafPsiElement.class
);

if (leafElements == null) {
return insertPos;
}

for (final LeafPsiElement leafPsiElement: leafElements) {
if (!leafPsiElement.getText().equals(MagentoPhpClass.CLOSING_TAG)) {
continue;
}
insertPos = leafPsiElement.getTextOffset();
}
return insertPos;

return insertPos == -1 ? insertPos : insertPos - 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
import com.magento.idea.magento2plugin.actions.generation.data.code.PluginMethodData;
import com.magento.idea.magento2plugin.actions.generation.generator.code.util.ConvertPluginParamsToPhpDocStringUtil;
import com.magento.idea.magento2plugin.actions.generation.generator.code.util.ConvertPluginParamsToString;
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -105,17 +106,32 @@ public PluginMethodData[] createPluginMethods(final @NotNull Plugin.PluginType t
return pluginMethods.toArray(new PluginMethodData[0]);
}

/**
* Fill attributes for plugin method.
*
* @param scopeForUseOperator PhpPsiElement
* @param type Plugin.PluginType
*
* @return Properties
*/
private Properties getAccessMethodAttributes(
final @Nullable PhpPsiElement scopeForUseOperator,
final @NotNull Plugin.PluginType type
) {
final Properties attributes = new Properties();
final String typeHint = this.fillAttributes(scopeForUseOperator, attributes, type);
this.addTypeHintsAndReturnType(attributes, typeHint);
final String pluginMethodReturnType = this.fillAttributes(
scopeForUseOperator,
attributes,
type
);

if (pluginMethodReturnType != null) {
this.addReturnType(attributes, pluginMethodReturnType);
}

return attributes;
}

@NotNull
private String fillAttributes(
final @Nullable PhpPsiElement scopeForUseOperator,
final Properties attributes,
Expand All @@ -127,12 +143,17 @@ private String fillAttributes(
final String pluginMethodName = type.toString().concat(methodSuffix);

attributes.setProperty("NAME", pluginMethodName);
final PhpReturnType targetMethodReturnType = myMethod.getReturnType();
String typeHint = "";
if (targetMethodReturnType != null) {
typeHint = targetMethodReturnType.getText();
String returnType = PhpTypeMetadataParserUtil.getMethodReturnType(this.myMethod);

if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
}

if (type.equals(Plugin.PluginType.before)) {
returnType = MagentoPhpClass.ARRAY_TYPE;
}
final Collection<PsiElement> parameters = new ArrayList();

final Collection<PsiElement> parameters = new ArrayList<>();
parameters.add(myTargetClass);
parameters.addAll(Arrays.asList(myMethod.getParameters()));
attributes.setProperty("PARAM_DOC", ConvertPluginParamsToPhpDocStringUtil.execute(
Expand All @@ -148,29 +169,35 @@ private String fillAttributes(
attributes.setProperty("RETURN_VARIABLES", returnVariables);
}

return typeHint;
return returnType;
}

private void addTypeHintsAndReturnType(final Properties attributes, final String typeHint) {
/**
* Add return type to the properties.
*
* @param attributes Properties
* @param returnDocType String
*/
private void addReturnType(final Properties attributes, final String returnDocType) {
final Project project = this.pluginClass.getProject();
if (PhpLanguageFeature.SCALAR_TYPE_HINTS.isSupported(project)
&& isDocTypeConvertable(typeHint)) {
attributes.setProperty("SCALAR_TYPE_HINT", convertDocTypeToHint(project, typeHint));
}

final boolean hasFeatureVoid = PhpLanguageFeature.RETURN_VOID.isSupported(project);
if (hasFeatureVoid) {
attributes.setProperty("VOID_RETURN_TYPE", MagentoPhpClass.VOID_RETURN_TYPE);
}

if (PhpLanguageFeature.RETURN_TYPES.isSupported(project)
&& isDocTypeConvertable(typeHint)) {
attributes.setProperty("RETURN_TYPE", convertDocTypeToHint(project, typeHint));
&& isDocTypeConvertable(returnDocType)) {
attributes.setProperty(
"RETURN_TYPE",
convertDocTypeToHint(project, returnDocType)
);
}

}

private static boolean isDocTypeConvertable(final String typeHint) {
/**
* Check if PHP DOC type could be converted to scalar PHP type.
*
* @param typeHint String
*
* @return boolean
*/
private static boolean isDocTypeConvertable(final @NotNull String typeHint) {
return !typeHint.equalsIgnoreCase(MagentoPhpClass.MIXED_RETURN_TYPE)
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.STATIC_MODIFIER)
&& !typeHint.equalsIgnoreCase(MagentoPhpClass.PHP_TRUE)
Expand All @@ -193,14 +220,30 @@ private static String convertNullableType(final Project project, final String ty
: (hasNullableTypeFeature ? "?" : "") + split[0];
}

@NotNull
private static String convertDocTypeToHint(final Project project, final String typeHint) {
/**
* Convert PHP DOC type to hint.
*
* @param project Project
* @param typeHint String
*
* @return String
*/
private static @NotNull String convertDocTypeToHint(
final @NotNull Project project,
final @NotNull String typeHint
) {
String hint = typeHint.contains("[]") ? "array" : typeHint;
hint = hint.contains("boolean") ? "bool" : hint;

if (typeWithNull(typeHint)) {
hint = convertNullableType(project, hint);
}

if (PhpLanguageFeature.RETURN_VOID.isSupported(project)
&& typeHint.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
hint = MagentoPhpClass.VOID_RETURN_TYPE;
}

return hint;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
import com.jetbrains.php.lang.PhpLangUtil;
import com.jetbrains.php.lang.documentation.phpdoc.PhpDocUtil;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil;
import java.util.Collection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class ConvertPluginParamsToPhpDocStringUtil {

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

if (type.equals(Plugin.PluginType.before)) {
returnType = MagentoPhpClass.ARRAY_TYPE;
} else {
returnType = PhpTypeMetadataParserUtil.getMethodReturnType(myMethod);

if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
}
}

final PhpType returnPhpType = new PhpType().add(returnType);
int increment = 0;

for (final PsiElement parameter : parameters) {
final PhpNamedElement element = (PhpNamedElement) parameter;

if (stringBuilder.length() > 0) {
stringBuilder.append('\n');
}

stringBuilder.append("* @param ");

String typeStr;

if (increment == 0) {
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), null);
typeStr = getStringTypePresentation(project, element.getType(), null);
} else {
typeStr = PhpDocUtil.getTypePresentation(
project, element.getType(),
PhpCodeInsightUtil.findScopeForUseOperator(element)
typeStr = getStringTypePresentation(
project,
element.getType(),
PhpCodeInsightUtil.findScopeForUseOperator(element)
);
if (typeStr.indexOf(Package.fqnSeparator, 1) > 0) {
final String[] fqnArray = typeStr.split("\\\\");
typeStr = fqnArray[fqnArray.length - 1];
}
}

if (!typeStr.isEmpty()) {
stringBuilder.append(typeStr).append(' ');
}
String paramName = element.getName();

if (increment == 0) {
paramName = "subject";
}
Expand All @@ -76,30 +90,64 @@ public static String execute(
if (type.equals(Plugin.PluginType.around) && increment == 0) {
stringBuilder.append("\n* @param callable $proceed");
}

if (type.equals(Plugin.PluginType.after) && increment == 0) {
if (returnType != null) {
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
stringBuilder.append("\n* @param null $result");
} else {
stringBuilder.append("\n* @param ").append(returnType.getText())
final String returnTypeStr = getStringTypePresentation(
project,
returnPhpType,
null
);
stringBuilder.append("\n* @param ")
.append(returnTypeStr)
.append(" $result");
}
increment++;
continue;
}

stringBuilder.append("\n* @param $result");
}

increment++;
}

if (!type.equals(Plugin.PluginType.before) && returnType != null) {
if (returnType.getText().equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
if (returnType != null) {
if (returnType.equals(MagentoPhpClass.VOID_RETURN_TYPE)) {
stringBuilder.append("\n* @return ").append(MagentoPhpClass.VOID_RETURN_TYPE);
} else {
stringBuilder.append("\n* @return ").append(returnType.getText());
final String returnTypeStr = getStringTypePresentation(
project,
returnPhpType,
null
);
stringBuilder.append("\n* @return ").append(returnTypeStr);
}
}
return stringBuilder.toString();
}

/**
* Get string type representation in the DOC format.
*
* @param project Project
* @param type PhpType
*
* @return String
*/
private static String getStringTypePresentation(
final @NotNull Project project,
final @NotNull PhpType type,
final @Nullable PhpPsiElement scope
) {
String typeStr = PhpDocUtil.getTypePresentation(project, type, scope);

if (PhpLangUtil.isFqn(typeStr)) {
typeStr = PhpClassGeneratorUtil.getNameFromFqn(typeStr);
}

return typeStr;
}
}
Loading