Skip to content
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

collect valid class on property injection directly via a fqn prefix #2376

Merged
merged 1 commit into from
May 20, 2024
Merged
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 @@ -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
Loading