|
10 | 10 | import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder; |
11 | 11 | import com.intellij.psi.PsiElement; |
12 | 12 | import com.intellij.psi.util.PsiTreeUtil; |
| 13 | +import com.intellij.psi.xml.XmlAttribute; |
13 | 14 | import com.intellij.psi.xml.XmlTag; |
14 | 15 | import com.jetbrains.php.lang.psi.elements.Method; |
15 | 16 | import com.jetbrains.php.lang.psi.elements.PhpClass; |
@@ -180,20 +181,53 @@ private Map<String, List<XmlTag>> extractRoutesForMethodRecursively( |
180 | 181 | */ |
181 | 182 | private void sortRoutes(final List<XmlTag> routes) { |
182 | 183 | 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 | + |
189 | 201 | if (firstSortOrder.compareTo(secondSortOrder) == 0) { |
190 | 202 | // Sort by route if HTTP methods are equal |
191 | | - return firstTag.getName().compareTo(secondTag.getName()); |
| 203 | + return firstUrl.compareTo(secondUrl); |
192 | 204 | } |
193 | 205 |
|
194 | 206 | return firstSortOrder.compareTo(secondSortOrder); |
195 | 207 | } |
196 | 208 | ); |
197 | 209 | } |
198 | 210 | } |
| 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 | + } |
199 | 233 | } |
0 commit comments