Skip to content

Commit faee427

Browse files
committed
1051: Code refactoring
1 parent 9dc92de commit faee427

File tree

1 file changed

+85
-31
lines changed

1 file changed

+85
-31
lines changed

src/com/magento/idea/magento2plugin/magento/packages/MagentoComponentManager.java

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class MagentoComponentManager {
3232

3333
private Map<String, MagentoComponent> components = new HashMap<>();
3434
private long cacheStartTime;
35-
private static final int CACHE_LIFE_TIME = 20000;
35+
private static final int CACHE_LIFE_TIME = 20000;//NOPMD
3636
private final Project project;
3737

3838
public MagentoComponentManager(final Project project) {
@@ -47,11 +47,21 @@ public Collection<MagentoComponent> getAllComponents() {
4747
return getComponents().values();
4848
}
4949

50+
/**
51+
* Get all components of the specified type.
52+
*
53+
* @param type Class
54+
*
55+
* @return Collection
56+
*/
5057
@SuppressWarnings("unchecked")
51-
public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(@NotNull Class<T> type) {
52-
Collection<T> result = new ArrayList<>();
53-
Map<String, MagentoComponent> components = getComponents();
54-
for (String key: components.keySet()) {
58+
public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(
59+
final @NotNull Class<T> type
60+
) {
61+
final Collection<T> result = new ArrayList<>();
62+
final Map<String, MagentoComponent> components = getComponents();
63+
64+
for (final String key: components.keySet()) {
5565
if (type.isInstance(components.get(key))) {
5666
result.add((T)components.get(key));
5767
}
@@ -60,6 +70,7 @@ public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(@NotNul
6070
return result;
6171
}
6272

73+
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
6374
private synchronized Map<String, MagentoComponent> getComponents() {
6475
if (project.isDisposed() || DumbService.getInstance(project).isDumb()) {
6576
return new HashMap<>();
@@ -74,9 +85,16 @@ private synchronized Map<String, MagentoComponent> getComponents() {
7485
return components;
7586
}
7687

88+
/**
89+
* Get component for the specified file.
90+
*
91+
* @param psiFile PsiFile
92+
*
93+
* @return MagentoComponent
94+
*/
7795
@Nullable
78-
public MagentoComponent getComponentForFile(@NotNull PsiFile psiFile) {
79-
for (MagentoComponent magentoComponent: this.getAllComponents()) {
96+
public MagentoComponent getComponentForFile(final @NotNull PsiFile psiFile) {
97+
for (final MagentoComponent magentoComponent: this.getAllComponents()) {
8098
if (magentoComponent.isFileInContext(psiFile)) {
8199
return magentoComponent;
82100
}
@@ -85,10 +103,21 @@ public MagentoComponent getComponentForFile(@NotNull PsiFile psiFile) {
85103
return null;
86104
}
87105

106+
/**
107+
* Get component of type for the specified file.
108+
*
109+
* @param psiFile PsiFile
110+
* @param type Class
111+
*
112+
* @return T
113+
*/
88114
@Nullable
89115
@SuppressWarnings("unchecked")
90-
public <T extends MagentoComponent> T getComponentOfTypeForFile(@NotNull PsiFile psiFile, @NotNull Class<T> type) {
91-
for (MagentoComponent magentoComponent: this.getAllComponents()) {
116+
public <T extends MagentoComponent> T getComponentOfTypeForFile(
117+
final @NotNull PsiFile psiFile,
118+
final @NotNull Class<T> type
119+
) {
120+
for (final MagentoComponent magentoComponent: this.getAllComponents()) {
92121
if (type.isInstance(magentoComponent) && magentoComponent.isFileInContext(psiFile)) {
93122
return (T)magentoComponent;
94123
}
@@ -97,72 +126,97 @@ public <T extends MagentoComponent> T getComponentOfTypeForFile(@NotNull PsiFile
97126
return null;
98127
}
99128

100-
synchronized public void flushModules() {
129+
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
130+
public synchronized void flushModules() {
101131
components = new HashMap<>();
102132
}
103133

134+
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidDeeplyNestedIfStmts"})
104135
private void loadModules() {
105-
Collection<String> packages = FileBasedIndex.getInstance().getAllKeys(ModulePackageIndex.KEY, this.project);
106-
PsiManager psiManager = PsiManager.getInstance(this.project);
107-
for (String packageName: packages) {
136+
final Collection<String> packages = FileBasedIndex
137+
.getInstance()
138+
.getAllKeys(ModulePackageIndex.KEY, this.project);
139+
final PsiManager psiManager = PsiManager.getInstance(this.project);
140+
141+
for (final String packageName: packages) {
108142
if (components.containsKey(packageName)) {
109143
continue;
110144
}
111145

112-
Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance()
113-
.getContainingFiles(ModulePackageIndex.KEY, packageName, GlobalSearchScope.allScope(this.project));
146+
final Collection<VirtualFile> containingFiles = FileBasedIndex
147+
.getInstance()
148+
.getContainingFiles(
149+
ModulePackageIndex.KEY,
150+
packageName,
151+
GlobalSearchScope.allScope(this.project)
152+
);
114153

115-
if (containingFiles.size() > 0) {
116-
VirtualFile configurationFile = containingFiles.iterator().next();
154+
if (!containingFiles.isEmpty()) {
155+
final VirtualFile configurationFile = containingFiles.iterator().next();
156+
final PsiFile psiFile = psiManager.findFile(configurationFile);
117157

118-
PsiFile psiFile = psiManager.findFile(configurationFile);
119-
if (psiFile != null && psiFile instanceof JsonFile) {
120-
JsonObject jsonObject = PsiTreeUtil.getChildOfType((JsonFile) psiFile, JsonObject.class);
158+
if (psiFile instanceof JsonFile) {
159+
final JsonObject jsonObject = PsiTreeUtil
160+
.getChildOfType((JsonFile) psiFile, JsonObject.class);
121161
if (jsonObject == null) {
122162
continue;
123163
}
124164

125165
MagentoComponent magentoComponent;
126-
ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl(jsonObject);
166+
final ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl(
167+
jsonObject
168+
);
169+
127170
if ("magento2-module".equals(composerPackageModel.getType())) {
128-
magentoComponent = new MagentoModuleImpl(new ComposerPackageModelImpl(jsonObject), psiFile.getContainingDirectory());
171+
magentoComponent = new MagentoModuleImpl(
172+
new ComposerPackageModelImpl(jsonObject),
173+
psiFile.getContainingDirectory()
174+
);
129175
} else {
130-
magentoComponent = new MagentoComponentImp(new ComposerPackageModelImpl(jsonObject), psiFile.getContainingDirectory());
176+
magentoComponent = new MagentoComponentImp(
177+
new ComposerPackageModelImpl(jsonObject),
178+
psiFile.getContainingDirectory()
179+
);
131180
}
132181

133182
components.put(
134-
packageName,
135-
magentoComponent
183+
packageName,
184+
magentoComponent
136185
);
137186
}
138187
}
139188
}
140189
}
141190
}
142191

192+
@SuppressWarnings("checkstyle:OneTopLevelClass")
143193
class MagentoModuleImpl extends MagentoComponentImp implements MagentoModule {
144194
private static final String DEFAULT_MODULE_NAME = "Undefined module";
145195
private static final String CONFIGURATION_PATH = "etc";
146196
private String moduleName;
147197

148-
public MagentoModuleImpl(@NotNull ComposerPackageModel composerPackageModel, @NotNull PsiDirectory directory) {
198+
public MagentoModuleImpl(
199+
final @NotNull ComposerPackageModel composerPackageModel,
200+
final @NotNull PsiDirectory directory
201+
) {
149202
super(composerPackageModel, directory);
150203
}
151204

205+
@SuppressWarnings({"PMD.AvoidDeeplyNestedIfStmts"})
152206
@Override
153207
public String getMagentoName() {
154208
if (moduleName != null) {
155209
return moduleName;
156210
}
157211

158-
PsiDirectory configurationDir = directory.findSubdirectory(CONFIGURATION_PATH);
212+
final PsiDirectory configurationDir = directory.findSubdirectory(CONFIGURATION_PATH);
159213
if (configurationDir != null) {
160-
PsiFile configurationFile = configurationDir.findFile("module.xml");
214+
final PsiFile configurationFile = configurationDir.findFile("module.xml");
161215

162-
if (configurationFile != null && configurationFile instanceof XmlFile) {
163-
XmlTag rootTag = ((XmlFile) configurationFile).getRootTag();
216+
if (configurationFile instanceof XmlFile) {
217+
final XmlTag rootTag = ((XmlFile) configurationFile).getRootTag();
164218
if (rootTag != null) {
165-
XmlTag module = rootTag.findFirstSubTag("module");
219+
final XmlTag module = rootTag.findFirstSubTag("module");
166220
if (module != null && module.getAttributeValue("name") != null) {
167221
moduleName = module.getAttributeValue("name");
168222
return moduleName;

0 commit comments

Comments
 (0)