Skip to content

Commit 8f2bc01

Browse files
authored
Merge pull request Haehnchen#1500 from Haehnchen/feature/array-access
fix possible array issues on service resource linemarker
2 parents 82bc31c + 4be3ab6 commit 8f2bc01

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/ServiceIndexUtil.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ public static boolean matchesResourcesGlob(@NotNull VirtualFile serviceFileAsBas
166166

167167
VirtualFile serviceFile = serviceFileAsBase.getParent();
168168
String[] split = replace.split("/");
169-
String[] split2 = split;
169+
String[] replacePathParts = split;
170170
for (String s : split) {
171171
if (s.equals("..")) {
172-
split2 = Arrays.copyOfRange(split2, 1, split2.length);
172+
replacePathParts = Arrays.copyOfRange(replacePathParts, 1, replacePathParts.length);
173173
serviceFile = serviceFile.getParent();
174174
} else {
175175
break;
@@ -180,18 +180,23 @@ public static boolean matchesResourcesGlob(@NotNull VirtualFile serviceFileAsBas
180180
return false;
181181
}
182182

183-
if (split2[split2.length - 1].equals("*")) {
184-
split2[split2.length - 1] = "**";
183+
// ending one wildcard must be *
184+
// "src/*" => "src/**"
185+
String path = (serviceFile.getPath() + "/" + StringUtils.join(replacePathParts, "/"))
186+
.replaceAll("[^*]([*])$", "**");
187+
188+
// force "**" at the end
189+
if (!path.endsWith("*")) {
190+
path += "**";
185191
}
186192

187-
String path = serviceFile.getPath() + "/" + StringUtils.join(split2, "/");
188-
String path1 = phpClassFile.getPath();
193+
String phpClassPath = phpClassFile.getPath();
189194

190195
if (exclude == null) {
191-
return isMatchingGlobResource(path, path1);
196+
return isMatchingGlobResource(path, phpClassPath);
192197
}
193198

194-
return isMatchingGlobResource(path, path1)
199+
return isMatchingGlobResource(path, phpClassPath)
195200
&& !matchesResourcesGlob(serviceFileAsBase, phpClassFile, exclude, null);
196201
}
197202

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/dict/ServiceUtilTempProjectTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,30 @@ public void testServiceResourcesWithDepth() {
5555
assertTrue(ServiceIndexUtil.matchesResourcesGlob(file, file2, "../../*", null));
5656
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "../../*", "../../Bar"));
5757
}
58+
59+
public void testPossibleResourceCondition() {
60+
VirtualFile file = createFile(
61+
"src/Foo/Resources/config/services.yml",
62+
"services:\n" +
63+
" App\\:\n" +
64+
" resource: ''\n"
65+
);
66+
67+
VirtualFile file2 = createFile(
68+
"src/Foo/Bar/Bar.php",
69+
"<?php\n" +
70+
"namespace App\\Foobar;\n" +
71+
"class Bar {}\n"
72+
);
73+
74+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "", ""));
75+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "src", "src"));
76+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "/aaa", "/aaaa"));
77+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "/", "/"));
78+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "a", "/"));
79+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "", null));
80+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "src", null));
81+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "/", null));
82+
assertFalse(ServiceIndexUtil.matchesResourcesGlob(file, file2, "..", ".."));
83+
}
5884
}

0 commit comments

Comments
 (0)