Skip to content

Commit c1eeebb

Browse files
authored
Merge pull request #818 from bohdan-harniuk/bugfix/646-null-pointer-exception-for-firstSortOrder-compareTo
[Bug] 646: Fixed NullPointerException for the firstSortOrder.compareTo
2 parents eac8b45 + 4491987 commit c1eeebb

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/com/magento/idea/magento2plugin/linemarker/php/WebApiLineMarkerProvider.java

+41-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
1111
import com.intellij.psi.PsiElement;
1212
import com.intellij.psi.util.PsiTreeUtil;
13+
import com.intellij.psi.xml.XmlAttribute;
1314
import com.intellij.psi.xml.XmlTag;
1415
import com.jetbrains.php.lang.psi.elements.Method;
1516
import com.jetbrains.php.lang.psi.elements.PhpClass;
@@ -180,20 +181,53 @@ private Map<String, List<XmlTag>> extractRoutesForMethodRecursively(
180181
*/
181182
private void sortRoutes(final List<XmlTag> routes) {
182183
routes.sort(
183-
(firstTag, secondTag) -> {
184-
final String substring = firstTag.getName().substring(2, 5);
185-
final Integer firstSortOrder = httpMethodsSortOrder.get(substring);
186-
final Integer secondSortOrder = httpMethodsSortOrder.get(
187-
secondTag.getName().substring(2, 5)
188-
);
184+
(tag1, tag2) -> {
185+
final String firstUrl = GetTagAttributeValueUtil.getValue(tag1, "url");
186+
final String secondUrl = GetTagAttributeValueUtil.getValue(tag2, "url");
187+
final String method1 = GetTagAttributeValueUtil.getValue(tag1, "method");
188+
final String method2 = GetTagAttributeValueUtil.getValue(tag2, "method");
189+
190+
if (method1.isEmpty() || method2.isEmpty()) {
191+
return firstUrl.compareTo(secondUrl);
192+
}
193+
194+
if (!httpMethodsSortOrder.containsKey(method1)
195+
|| !httpMethodsSortOrder.containsKey(method2)) {
196+
return firstUrl.compareTo(secondUrl);
197+
}
198+
final Integer firstSortOrder = httpMethodsSortOrder.get(method1);
199+
final Integer secondSortOrder = httpMethodsSortOrder.get(method2);
200+
189201
if (firstSortOrder.compareTo(secondSortOrder) == 0) {
190202
// Sort by route if HTTP methods are equal
191-
return firstTag.getName().compareTo(secondTag.getName());
203+
return firstUrl.compareTo(secondUrl);
192204
}
193205

194206
return firstSortOrder.compareTo(secondSortOrder);
195207
}
196208
);
197209
}
198210
}
211+
212+
@SuppressWarnings("PMD.UseUtilityClass")
213+
private static class GetTagAttributeValueUtil {
214+
215+
public static @NotNull String getValue(
216+
final XmlTag tag,
217+
final @NotNull String attributeName
218+
) {
219+
final XmlAttribute attribute = tag.getAttribute(attributeName);
220+
221+
if (attribute == null) {
222+
return "";
223+
}
224+
final String value = attribute.getValue();
225+
226+
if (value == null) {
227+
return "";
228+
}
229+
230+
return value.trim();
231+
}
232+
}
199233
}

0 commit comments

Comments
 (0)