Skip to content

Commit

Permalink
collect valid class on property injectino directly via a fqn prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed May 20, 2024
1 parent a57e124 commit 2adae49
Showing 1 changed file with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,12 @@ public static List<String> getInjectionService(@NotNull Project project, @NotNul
);
}

HashSet<String> objects = new HashSet<>();
HashSet<String> classes = new HashSet<>();

objects.addAll(PhpIndex.getInstance(project).getAllClassFqns(PrefixMatcher.ALWAYS_TRUE));
objects.addAll(PhpIndex.getInstance(project).getAllInterfacesFqns(PrefixMatcher.ALWAYS_TRUE));
classes.addAll(PhpIndex.getInstance(project).getAllClassFqns(NonGarbageClassPrefixMatcher.INSTANCE));
classes.addAll(PhpIndex.getInstance(project).getAllInterfacesFqns(NonGarbageClassPrefixMatcher.INSTANCE));

Set<String> collect = objects.stream().filter(s -> {
int i = s.lastIndexOf("\\");
if (i > 0) {
if (s.toLowerCase().contains("\\test\\")) {
return false;
}

s = s.substring(i);
}

return !s.endsWith("Test")
&& !s.toLowerCase().contains("_phpstan_")
&& !s.toLowerCase().contains("ecsprefix")
&& !s.toLowerCase().contains("_humbugbox")
&& !s.toLowerCase().contains("rectorprefix");
}).collect(Collectors.toSet());

for (String fqn : collect) {
for (String fqn : classes) {
// Bar\Foo => Foo
int i = fqn.lastIndexOf("\\");
String classPropertyNameRaw = i > 0
Expand Down Expand Up @@ -177,6 +160,37 @@ public static List<String> getInjectionService(@NotNull Project project, @NotNul
.collect(Collectors.toList());
}

/**
* Filter some invalid classes fqn
* - RectorPrefix2...
* - _PHPStan_f12ae...
*/
private static class NonGarbageClassPrefixMatcher extends PrefixMatcher {
private static final NonGarbageClassPrefixMatcher INSTANCE = new NonGarbageClassPrefixMatcher();

private NonGarbageClassPrefixMatcher() {
super("");
}

@Override
public boolean prefixMatches(@NotNull String fqn) {
if (fqn.contains("\\") && fqn.toLowerCase().contains("\\test\\")) {
return false;
}

return !fqn.endsWith("Test")
&& !fqn.toLowerCase().contains("_phpstan_")
&& !fqn.toLowerCase().contains("ecsprefix")
&& !fqn.toLowerCase().contains("_humbugbox")
&& !fqn.toLowerCase().contains("rectorprefix");
}

@Override
public @NotNull PrefixMatcher cloneWithPrefix(@NotNull String prefix) {
return new NonGarbageClassPrefixMatcher();
}
}

private static class Match {
private final String fqn;
private int weight = 0;
Expand Down

0 comments on commit 2adae49

Please sign in to comment.