Skip to content

406: added sorting plugin list marker #1121

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
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 @@ -42,7 +42,6 @@
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
import com.magento.idea.magento2plugin.util.magento.plugin.IsPluginAllowedForMethodUtil;
import gnu.trove.THashSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -88,9 +87,9 @@ public boolean isValidFor(final Editor editor, final PsiFile file) {
return false;
}
final GetTargetClassNamesByPluginClassName targetClassesService
= GetTargetClassNamesByPluginClassName.getInstance(editor.getProject());
= new GetTargetClassNamesByPluginClassName(editor.getProject());
final String currentClass = phpClass.getFQN().substring(1);
final ArrayList<String> targetClassNames = targetClassesService.execute(currentClass);
final List<String> targetClassNames = targetClassesService.execute(currentClass);
return !targetClassNames.isEmpty();
}

Expand Down Expand Up @@ -241,9 +240,9 @@ protected PhpNamedElementNode[] targetMethods(
final TreeMap<String, PhpNamedElementNode> nodes = new TreeMap();

final GetTargetClassNamesByPluginClassName targetClassesService =
GetTargetClassNamesByPluginClassName.getInstance(phpClass.getProject());
new GetTargetClassNamesByPluginClassName(phpClass.getProject());
final String currentClass = phpClass.getFQN().substring(1);
final ArrayList<String> targetClassNames = targetClassesService.execute(currentClass);
final List<String> targetClassNames = targetClassesService.execute(currentClass);
for (final String targetClassName : targetClassNames) {
final PhpClass targetClass = GetPhpClassByFQN.getInstance(
phpClass.getProject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings({"PMD.ExcessiveMethodLength", "PMD.NPathComplexity", "PMD.CognitiveComplexity"})
Expand Down Expand Up @@ -75,10 +75,8 @@ public void visitPhpMethod(final Method pluginMethod) {
((PhpClass) parentClass).getNameIdentifier();
final String currentClass = ((PhpClass) parentClass).getFQN().substring(1);
final GetTargetClassNamesByPluginClassName targetClassesService =
GetTargetClassNamesByPluginClassName.getInstance(
problemsHolder.getProject()
);
final ArrayList<String> targetClassNames =
new GetTargetClassNamesByPluginClassName(problemsHolder.getProject());
final List<String> targetClassNames =
targetClassesService.execute(currentClass);

for (final String targetClassName : targetClassNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.linemarker.SearchGutterIconNavigationHandler;
import com.magento.idea.magento2plugin.linemarker.php.data.PluginMethodData;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.stubs.indexes.PluginIndex;
import com.magento.idea.magento2plugin.stubs.indexes.data.PluginData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -94,12 +95,13 @@ public void collectSlowLineMarkers(
}
}

@SuppressWarnings("checkstyle:LineLength")
private static class PluginClassCache {

private final Map<String, List<PhpClass>> classPluginsMap = new HashMap<>();
private final Map<String, List<PluginData>> classPluginsMap = new HashMap<>();

public List<PhpClass> getPluginsForClass(final @NotNull PhpClass phpClass) {
final List<PhpClass> pluginsForClass = getPluginsForClass(
public List<PluginData> getPluginsForClass(final @NotNull PhpClass phpClass) {
final List<PluginData> pluginsForClass = getPluginsForClass(
phpClass,
phpClass.getPresentableFQN()
);
Expand All @@ -114,64 +116,73 @@ public List<PhpClass> getPluginsForClass(final @NotNull PhpClass phpClass) {
return pluginsForClass;
}

public List<PhpClass> getPluginsForClass(
public List<PluginData> getPluginsForClass(
final @NotNull PhpClass phpClass,
final @NotNull String classFQN
) {
if (classPluginsMap.containsKey(classFQN)) {
return classPluginsMap.get(classFQN);
}

final List<Set<String>> plugins = FileBasedIndex.getInstance()
final List<Set<PluginData>> plugins = FileBasedIndex.getInstance()
.getValues(
PluginIndex.KEY,
classFQN,
GlobalSearchScope.allScope(phpClass.getProject())
);
final List<PhpClass> results = new ArrayList<>();
final List<PluginData> results = new ArrayList<>();

if (plugins.isEmpty()) {
classPluginsMap.put(classFQN, results);

return results;
}
final PhpIndex phpIndex = PhpIndex.getInstance(phpClass.getProject());

for (final Set<String> pluginClassNames : plugins) {
for (final String pluginClassName: pluginClassNames) {
results.addAll(phpIndex.getClassesByFQN(pluginClassName));
for (final Set<PluginData> pluginDataList : plugins) {
for (final PluginData pluginData: pluginDataList) {
pluginData.setPhpClass(phpClass);
results.add(pluginData);
}
}
classPluginsMap.put(classFQN, results);

return results;
}

public List<Method> getPluginMethods(final List<PhpClass> plugins) {
final List<Method> methodList = new ArrayList<>();
public List<PluginMethodData> getPluginMethods(final List<PluginData> pluginDataList) {
final List<PluginMethodData> result = new ArrayList<>();

for (final PhpClass plugin: plugins) {
methodList.addAll(getPluginMethods(plugin));
for (final PluginData pluginData: pluginDataList) {
for (final PhpClass plugin: pluginData.getPhpClassCollection()) {
//@todo add module sequence ID if sortOrder equal zero. It should be negative value.
result.addAll(getPluginMethods(plugin, pluginData.getSortOrder()));
}
}

return methodList;
return result;
}

public List<Method> getPluginMethods(final @NotNull PhpClass pluginClass) {
final List<Method> methodList = new ArrayList<>();
public List<PluginMethodData> getPluginMethods(final @NotNull PhpClass pluginClass, final int sortOrder) {
final List<PluginMethodData> methodList = new ArrayList<>();

for (final Method method : pluginClass.getMethods()) {
if (method.getAccess().isPublic()) {
final String pluginMethodName = method.getName();

if (pluginMethodName.length() > MIN_PLUGIN_METHOD_NAME_LENGTH) {
methodList.add(method);
//@todo module sequence value should be set here instead of zero.
methodList.add(getPluginMethodDataObject(method, sortOrder, 0));
}
}
}

return methodList;
}

@NotNull
private PluginMethodData getPluginMethodDataObject(final Method method, final int sortOrder, final int moduleSequence) {
return new PluginMethodData(method, sortOrder, moduleSequence);
}
}

private static class ClassPluginCollector implements Collector<PhpClass, PhpClass> {
Expand All @@ -186,41 +197,134 @@ public ClassPluginCollector(

@Override
public List<PhpClass> collect(final @NotNull PhpClass psiElement) {
return pluginClassCache.getPluginsForClass(psiElement);
final List<PluginData> pluginDataList = pluginClassCache.getPluginsForClass(psiElement);
final List<PhpClass> phpClassList = new ArrayList<>();

for (final PluginData pluginData: pluginDataList) {
phpClassList.addAll(pluginData.getPhpClassCollection());
}

return phpClassList;
}
}

private static class MethodPluginCollector implements Collector<Method, Method> {

private final PluginLineMarkerProvider.PluginClassCache pluginClassCache;
private final Map<String, Integer> pluginMethodsSortOrder;
private static final String AFTER_PLUGIN_PREFIX = "after";

public MethodPluginCollector(
final PluginLineMarkerProvider.PluginClassCache pluginClassCache
) {
this.pluginClassCache = pluginClassCache;
pluginMethodsSortOrder = new HashMap<>();
pluginMethodsSortOrder.put("before", 1);
pluginMethodsSortOrder.put("around", 2);
pluginMethodsSortOrder.put(AFTER_PLUGIN_PREFIX, 3);
}

@SuppressWarnings("checkstyle:LineLength")
@Override
public List<Method> collect(final @NotNull Method psiElement) {
final List<Method> results = new ArrayList<>();

final PhpClass methodClass = psiElement.getContainingClass();

if (methodClass == null) {
return results;
}
final List<PhpClass> pluginsList = pluginClassCache.getPluginsForClass(methodClass);
final List<Method> pluginMethods = pluginClassCache.getPluginMethods(pluginsList);

final List<PluginData> pluginDataList = pluginClassCache.getPluginsForClass(methodClass);
final List<PluginMethodData> pluginMethods = pluginClassCache.getPluginMethods(pluginDataList);
final String classMethodName = WordUtils.capitalize(psiElement.getName());

for (final Method pluginMethod: pluginMethods) {
if (isPluginMethodName(pluginMethod.getName(), classMethodName)) {
results.add(pluginMethod);
pluginMethods.removeIf(pluginMethod -> !isPluginMethodName(pluginMethod.getMethodName(), classMethodName));
sortMethods(pluginMethods, results);

return results;
}

@SuppressWarnings({"checkstyle:Indentation", "checkstyle:OperatorWrap", "checkstyle:LineLength", "PMD.NPathComplexity"})
private void sortMethods(final @NotNull List<PluginMethodData> methodDataList, final List<Method> results) {
final List<Integer> bufferSortOrderList = new ArrayList<>();
int biggestSortOrder = 0;

for (final PluginMethodData pluginMethodData: methodDataList) {
final String methodName = pluginMethodData.getMethodName();

if (methodName.startsWith("around")) {
bufferSortOrderList.add(pluginMethodData.getSortOrder());
}

if (pluginMethodData.getSortOrder() > biggestSortOrder) {
biggestSortOrder = pluginMethodData.getSortOrder();
}
}

return results;
final int biggestSortOrderValue = biggestSortOrder;

methodDataList.sort(
(PluginMethodData method1, PluginMethodData method2) -> {
final String firstMethodName = method1.getMethodName();
final String secondMethodName = method2.getMethodName();
final int firstIndexEnd = firstMethodName.startsWith(AFTER_PLUGIN_PREFIX) ? 5 : 6;
final int secondIndexEnd = secondMethodName.startsWith(AFTER_PLUGIN_PREFIX) ? 5 : 6;
final String firstMethodPrefix = firstMethodName.substring(0,firstIndexEnd);
final String secondMethodPrefix = secondMethodName.substring(0,secondIndexEnd);

if (!pluginMethodsSortOrder.containsKey(firstMethodPrefix)
|| !pluginMethodsSortOrder.containsKey(secondMethodPrefix)) {
return firstMethodName.compareTo(secondMethodName);
}

final Integer firstNameSortOrder = pluginMethodsSortOrder.get(firstMethodPrefix);
final Integer secondNameSortOrder = pluginMethodsSortOrder.get(secondMethodPrefix);

if (firstNameSortOrder.compareTo(secondNameSortOrder) != 0) {
return firstNameSortOrder.compareTo(secondNameSortOrder);
}

Integer firstBuffer = 0;
Integer secondBuffer = 0;
final Integer firstModuleSequence = (method1.getModuleSequence() + biggestSortOrderValue) * -1;
final Integer secondModuleSequence = (method2.getModuleSequence() + biggestSortOrderValue) * -1;
final Integer firstSortOrder = method1.getSortOrder() == 0 ?
firstModuleSequence :
method1.getSortOrder();
final Integer secondSortOrder = method2.getSortOrder() == 0 ?
secondModuleSequence :
method2.getSortOrder();

if (!bufferSortOrderList.isEmpty() && firstMethodPrefix.equals(AFTER_PLUGIN_PREFIX)) {
for (final Integer bufferSortOrder : bufferSortOrderList) {
if (bufferSortOrder < firstSortOrder && firstBuffer < bufferSortOrder + 1) {
firstBuffer = bufferSortOrder + 1;
}

if (bufferSortOrder < secondSortOrder && secondBuffer < bufferSortOrder + 1) {
secondBuffer = bufferSortOrder + 1;
}
}
}

firstBuffer = firstBuffer.equals(0) ? firstSortOrder : firstBuffer * -1;
secondBuffer = secondBuffer.equals(0) ? secondSortOrder : secondBuffer * -1;

if (firstBuffer.compareTo(secondBuffer) == 0 && firstSortOrder.compareTo(secondSortOrder) != 0) {
return firstSortOrder.compareTo(secondSortOrder);
}

if (firstBuffer.compareTo(secondBuffer) == 0 && firstSortOrder.compareTo(secondSortOrder) == 0) {
return firstModuleSequence.compareTo(secondModuleSequence);
}

return firstBuffer.compareTo(secondBuffer);
}
);

for (final PluginMethodData pluginMethodData: methodDataList) {
results.add(pluginMethodData.getMethod());
}
}

private boolean isPluginMethodName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ private List<PhpClass> getTargetClassesForPlugin(
}

final GetTargetClassNamesByPluginClassName targetClassesService =
GetTargetClassNamesByPluginClassName.getInstance(phpClass.getProject());
final ArrayList<String> targetClassNames = targetClassesService.execute(classFQN);
new GetTargetClassNamesByPluginClassName(phpClass.getProject());
final List<String> targetClassNames = targetClassesService.execute(classFQN);

final List<PhpClass> results = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.linemarker.php.data;

import com.jetbrains.php.lang.psi.elements.Method;

public class PluginMethodData {

private final Method method;
private final int sortOrder;
private final int moduleSequence;

/**
* Plugin method data class.
*
* @param method Plugin method
* @param sortOrder Plugin SortOrder integer value
* @param moduleSequence PHP class placed module sequence integer value
*/
public PluginMethodData(final Method method, final int sortOrder, final int moduleSequence) {
this.method = method;
this.sortOrder = sortOrder;
this.moduleSequence = moduleSequence;
}

public Method getMethod() {
return method;
}

public int getSortOrder() {
return sortOrder;
}

public int getModuleSequence() {
return moduleSequence;
}

public String getMethodName() {
return method.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ModuleDiXml implements ModuleFileInterface {

// attributes
public static String NAME_ATTR = "name";
public static String SORT_ORDER_ATTR = "sortOrder";
public static String TYPE_ATTR = "type";
public static String PREFERENCE_ATTR_FOR = "for";
public static String CLI_COMMAND_ATTR_COMMANDS = "commands";
Expand Down
Loading