Skip to content

Bugfix/630 method designed for fully qualified names only #830

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

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
Expand Up @@ -7,6 +7,7 @@

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.text.Strings;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
import com.jetbrains.php.config.PhpLanguageFeature;
Expand Down Expand Up @@ -42,7 +43,12 @@ private ConvertPluginParamsToString() {}
*
* @return String
*/
@SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity", "PMD.ConfusingTernary"})
@SuppressWarnings({
"PMD.NPathComplexity",
"PMD.CyclomaticComplexity",
"PMD.CognitiveComplexity",
"PMD.ConfusingTernary"
})
public static String execute(
final Collection<PsiElement> parameters,
final @NotNull Plugin.PluginType type,
Expand All @@ -52,7 +58,11 @@ public static String execute(
String returnType = PhpTypeMetadataParserUtil.getMethodReturnType(myMethod);

if (returnType != null && PhpClassGeneratorUtil.isValidFqn(returnType)) {
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
if (Strings.endsWith(returnType, "[]")) {
returnType = "array";
} else {
returnType = PhpClassGeneratorUtil.getNameFromFqn(returnType);
}
}
final Iterator<PsiElement> parametersIterator = parameters.iterator();
int iterator = 0;
Expand All @@ -66,7 +76,19 @@ public static String execute(
if (element instanceof Parameter) {
String parameterText = PhpCodeUtil.paramToString(element);

if (parameterText.contains(Package.fqnSeparator)) {
// Parameter has default value.
if (parameterText.contains("=")) {
final String[] paramParts = parameterText.split("=");
parameterText = paramParts[0];
parameterText += " = ";//NOPMD
String defaultValue = paramParts[1];

if (defaultValue.contains(Package.fqnSeparator)) {
final String[] fqnArray = defaultValue.split("\\\\");
defaultValue = fqnArray[fqnArray.length - 1];
}
parameterText += defaultValue;//NOPMD
} else if (parameterText.contains(Package.fqnSeparator)) {
final String[] fqnArray = parameterText.split("\\\\");
parameterText = fqnArray[fqnArray.length - 1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpReturnType;
import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl;
import com.jetbrains.php.lang.psi.elements.impl.ClassConstantReferenceImpl;
import com.jetbrains.php.lang.psi.elements.impl.ParameterImpl;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
import java.util.Collection;
Expand Down Expand Up @@ -39,6 +42,7 @@ public void visitPhpClass(final PhpClass phpClass) {
@Override
public void visitPhpParameter(final Parameter parameter) {
PhpClassReferenceExtractor.this.processParameterReference(parameter);
PhpClassReferenceExtractor.this.processUsedForDefaultValueTypeReference(parameter);
}

/**
Expand Down Expand Up @@ -77,6 +81,39 @@ public void processParameterReference(final @NotNull Parameter parameter) {
this.processReference(getNameFromFqn(complexType), complexType, parameter);
}

/**
* Process reference for complex type if it is exists in the default parameter value.
*
* @param parameter Parameter
*/
public void processUsedForDefaultValueTypeReference(final @NotNull Parameter parameter) {
if (!(parameter instanceof ParameterImpl)) {
return;
}
final PsiElement defaultValue = parameter.getDefaultValue();

if (!(defaultValue instanceof ClassConstantReferenceImpl)) {
return;
}
final PsiElement constant = ((ClassConstantReferenceImpl) defaultValue).resolve();

if (!(constant instanceof ClassConstImpl)) {
return;
}
final PhpClass usedTypeClass = ((ClassConstImpl) constant).getContainingClass();

if (usedTypeClass == null) {
return;
}
final String complexType = extractComplexType(usedTypeClass.getType());

if (complexType == null) {
return;
}

this.processReference(getNameFromFqn(complexType), complexType, parameter);
}

/**
* Process reference for non primitive return type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void processReference(
final @NotNull PsiElement identifier
) {
if (!PhpType.isPrimitiveType(name)) {
this.myCandidatesToImportStorage.processReference(name, fqn, identifier);
this.myCandidatesToImportStorage.processReference(name, fqn);
}
}

Expand Down Expand Up @@ -142,22 +142,22 @@ private Map<String, String> importWithConflictResolve(
referencesToReplace.put(name, originalFqn);
} else {
final String importedFqn = aliases.get(name);
if (!PhpLangUtil.equalsClassNames(importedFqn, originalFqn)) {
if (importedFqn != null) {
final String originalName = PhpLangUtil.toShortName(originalFqn);
final String fqnForOriginalName = aliases.get(originalName);
if (fqnForOriginalName != null
&& !PhpLangUtil.equalsClassNames(fqnForOriginalName, originalFqn)) {
referencesToReplace.put(name, originalFqn);
} else {
referencesToReplace.put(name, originalName);
if (fqnForOriginalName == null) {
insertUseStatement(scopeHolder, originalName, originalFqn);
}
}

if (importedFqn != null
&& !PhpLangUtil.equalsClassNames(importedFqn, originalFqn)) {
final String originalName = PhpLangUtil.toShortName(originalFqn);
final String fqnForOriginalName = aliases.get(originalName);
if (fqnForOriginalName != null
&& !PhpLangUtil.equalsClassNames(fqnForOriginalName, originalFqn)) {
referencesToReplace.put(name, originalFqn);
} else {
insertUseStatement(scopeHolder, name, originalFqn);
referencesToReplace.put(name, originalName);
if (fqnForOriginalName == null) {
insertUseStatement(scopeHolder, originalName, originalFqn);
}
}
} else {
insertUseStatement(scopeHolder, name, originalFqn);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

package com.magento.idea.magento2plugin.actions.generation.references;

import com.intellij.psi.PsiElement;
import com.jetbrains.php.refactoring.importReferences.PhpClassReferenceExtractor;
import gnu.trove.THashMap;
import java.util.Map;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PhpClassReferenceStorage extends PhpClassReferenceExtractor {
public class PhpClassReferenceStorage {

private final Map<String, String> myReferences = new THashMap<>();

Expand All @@ -22,12 +20,10 @@ public class PhpClassReferenceStorage extends PhpClassReferenceExtractor {
*
* @param name String
* @param fqn String
* @param identifier PsiElement
*/
protected void processReference(
final @NotNull String name,
final @NotNull String fqn,
final @NotNull PsiElement identifier
final @NotNull String fqn
) {
this.myReferences.put(name, fqn);
}
Expand Down