Skip to content

Commit 8d59c93

Browse files
committed
Cleanup ContainerService constructor
1 parent 4bfc25b commit 8d59c93

File tree

4 files changed

+46
-54
lines changed

4 files changed

+46
-54
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/ContainerService.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class ContainerService {
2626
final private String name;
2727

2828
final private String className;
29-
private final boolean fallbackPrivate;
30-
private final boolean fallbackWeak;
3129
private final Set<String> classVariants = new HashSet<>();
3230
private final Set<ContainerServiceMetadata> metadata = new LinkedHashSet<>();
3331
@Nullable
@@ -55,22 +53,10 @@ public class ContainerService {
5553
@Nullable
5654
private Set<String> cachedDecorationInnerNames;
5755

58-
public ContainerService(@NotNull String name, @Nullable String className) {
56+
public ContainerService(@NotNull String name, @Nullable String className, @NotNull ContainerServiceMetadata metadata) {
5957
this.name = name;
6058
this.className = className;
61-
this.fallbackPrivate = false;
62-
this.fallbackWeak = false;
63-
}
64-
65-
public ContainerService(@NotNull String name, @Nullable String className, boolean isWeak) {
66-
this(name, className, isWeak, false);
67-
}
68-
69-
public ContainerService(@NotNull String name, @Nullable String className, boolean isWeak, boolean isPrivate) {
70-
this.name = name;
71-
this.className = className;
72-
this.fallbackWeak = isWeak;
73-
this.fallbackPrivate = isPrivate;
59+
addMetadata(metadata);
7460
}
7561

7662
/**
@@ -103,12 +89,6 @@ public void addMetadata(@NotNull ContainerServiceMetadata metadata) {
10389
}
10490
}
10591

106-
public void addMetadata(@NotNull Iterable<ContainerServiceMetadata> metadata) {
107-
for (ContainerServiceMetadata containerServiceMetadata : metadata) {
108-
addMetadata(containerServiceMetadata);
109-
}
110-
}
111-
11292
@NotNull
11393
public Set<String> getClassNames() {
11494
if (cachedClassNames != null) {
@@ -135,7 +115,7 @@ public boolean isWeak() {
135115
return cachedWeak = !hasSourceKind(ContainerServiceMetadata.SourceKind.COMPILED_CONTAINER);
136116
}
137117

138-
return cachedWeak = fallbackWeak;
118+
return cachedWeak = false;
139119
}
140120

141121
public boolean isPrivate() {
@@ -144,7 +124,7 @@ public boolean isPrivate() {
144124
}
145125

146126
ContainerServiceMetadata metadata = getPrimaryMetadata();
147-
return cachedPrivate = metadata != null ? !metadata.publicService() : fallbackPrivate;
127+
return cachedPrivate = metadata != null && !metadata.publicService();
148128
}
149129

150130
@NotNull

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/ContainerServiceMetadata.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ private static String normalizeNullable(@Nullable String value) {
8686
return trimmed.isEmpty() ? null : trimmed;
8787
}
8888

89+
@NotNull
90+
public static ContainerServiceMetadata empty(@NotNull SourceKind sourceKind) {
91+
return new ContainerServiceMetadata(
92+
null,
93+
false,
94+
false,
95+
false,
96+
false,
97+
false,
98+
false,
99+
null,
100+
null,
101+
null,
102+
null,
103+
Collections.emptySet(),
104+
Collections.emptySet(),
105+
Collections.emptySet(),
106+
sourceKind
107+
);
108+
}
109+
89110
public enum SourceKind {
90111
RESOURCE_PROTOTYPE,
91112
COMPILED_CONTAINER,

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ private static void addCompiledContainerService(@NotNull Map<String, ContainerSe
185185

186186
ContainerService containerService = services.computeIfAbsent(
187187
entry.getId(),
188-
key -> new ContainerService(entry.getId(), entry.getClassName(), false, !entry.isPublic())
188+
key -> new ContainerService(entry.getId(), entry.getClassName(), toMetadata(entry, ContainerServiceMetadata.SourceKind.COMPILED_CONTAINER))
189189
);
190-
191-
containerService.addMetadata(toMetadata(entry, ContainerServiceMetadata.SourceKind.COMPILED_CONTAINER));
190+
if (!containerService.hasSourceKind(ContainerServiceMetadata.SourceKind.COMPILED_CONTAINER)) {
191+
containerService.addMetadata(toMetadata(entry, ContainerServiceMetadata.SourceKind.COMPILED_CONTAINER));
192+
}
192193
}
193194

194195
@Nullable
@@ -372,26 +373,17 @@ public Map<String, ContainerService> getServices() {
372373

373374
if(!exps.isEmpty()) {
374375
exps.forEach(service -> {
375-
ContainerService containerService = new ContainerService(service.getId(), service.getClassName(), true, !service.isPublic());
376-
containerService.addMetadata(toMetadata(service, ContainerServiceMetadata.SourceKind.INDEXED_SERVICE));
377-
services.put(service.getId(), containerService);
376+
services.put(
377+
service.getId(),
378+
new ContainerService(service.getId(), service.getClassName(), toMetadata(service, ContainerServiceMetadata.SourceKind.INDEXED_SERVICE))
379+
);
378380
});
379381
}
380382

381383
for (Map.Entry<String, List<ServiceSerializable>> entry : FileIndexCaches.getSetDataCache(project, SERVICE_CONTAINER_INDEX, SERVICE_CONTAINER_INDEX_NAMES, ServicesDefinitionStubIndex.KEY, ServiceIndexUtil.getRestrictedFileTypesScope(project)).entrySet()) {
382-
383-
// dont work twice on service;
384-
// @TODO: to need to optimize this to decorate as much service data as possible
385384
String serviceName = entry.getKey();
386385

387-
// fake empty service, case which is not allowed by catch it
388-
List<ServiceSerializable> servicesValues = entry.getValue();
389-
if(services.isEmpty()) {
390-
services.put(serviceName, new ContainerService(serviceName, null, true));
391-
continue;
392-
}
393-
394-
for(ServiceInterface service: servicesValues) {
386+
for (ServiceInterface service: entry.getValue()) {
395387
String classValue = service.getClassName();
396388

397389
// duplicate services
@@ -403,13 +395,11 @@ public Map<String, ContainerService> getServices() {
403395
continue;
404396
}
405397

406-
String classValueResolve = classValue;
407398
String compiledClassName = containerService.getClassName();
408399
if(!classValue.equalsIgnoreCase(compiledClassName)) {
409400
String resolvedClassValue = getParameterCollector().resolve(classValue);
410401
if(resolvedClassValue != null && !StringUtils.isBlank(classValue) && !resolvedClassValue.equalsIgnoreCase(compiledClassName)) {
411402
containerService.addClassName(resolvedClassValue);
412-
classValueResolve = resolvedClassValue;
413403
}
414404
}
415405

@@ -430,9 +420,10 @@ public Map<String, ContainerService> getServices() {
430420
classValue = getParameterCollector().resolve(classValue);
431421
}
432422

433-
ContainerService indexedService = new ContainerService(service.getId(), classValue, true, !service.isPublic());
434-
indexedService.addMetadata(toMetadata(service, ContainerServiceMetadata.SourceKind.INDEXED_SERVICE));
435-
services.put(serviceName, indexedService);
423+
services.put(
424+
serviceName,
425+
new ContainerService(service.getId(), classValue, toMetadata(service, ContainerServiceMetadata.SourceKind.INDEXED_SERVICE))
426+
);
436427
}
437428
}
438429

@@ -493,8 +484,7 @@ private Map<String, ContainerService> collectDecorated(@NotNull Collection<Servi
493484
continue;
494485
}
495486

496-
// @TODO: migrate constructor to ServiceInterface and decorate
497-
ContainerService value = new ContainerService(decorationInnerName, origin.getClassName(), origin.isWeak(), true);
487+
ContainerService value = new ContainerService(decorationInnerName, origin.getClassName(), ContainerServiceMetadata.empty(ContainerServiceMetadata.SourceKind.INDEXED_SERVICE));
498488
origin.getClassNames().forEach(value::addClassName);
499489

500490
items.put(decorationInnerName, value);
@@ -585,9 +575,7 @@ public static ServiceCollector create(@NotNull Project project) {
585575

586576
@NotNull
587577
private static ContainerService toContainerService(@NotNull ResourceBasedService service) {
588-
ContainerService containerService = new ContainerService(service.id(), service.className(), true);
589-
containerService.addMetadata(service.metadata());
590-
return containerService;
578+
return new ContainerService(service.id(), service.className(), service.metadata());
591579
}
592580
}
593581

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/ServiceStringLookupElementTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
public class ServiceStringLookupElementTest extends SymfonyLightCodeInsightFixtureTestCase {
1616

1717
public void testServiceLookupPresentableRendering() {
18-
ContainerService service = new ContainerService("foo", "DateTime", true);
19-
service.addMetadata(new ContainerServiceMetadata(
18+
ContainerService service = new ContainerService("foo", "DateTime", new ContainerServiceMetadata(
2019
null,
2120
false,
2221
false,
@@ -45,7 +44,11 @@ public void testServiceLookupPresentableRendering() {
4544
}
4645

4746
public void testServiceLookupPresentableRenderingLegacy() {
48-
ServiceStringLookupElement element = new ServiceStringLookupElement(new ContainerService("foo", "DateTime"));
47+
ServiceStringLookupElement element = new ServiceStringLookupElement(new ContainerService(
48+
"foo",
49+
"DateTime",
50+
ContainerServiceMetadata.empty(ContainerServiceMetadata.SourceKind.INDEXED_SERVICE)
51+
));
4952
assertEquals("foo", element.getLookupString());
5053

5154
LookupElementPresentation presentation = new LookupElementPresentation();

0 commit comments

Comments
 (0)