Skip to content

Commit 7c85cbf

Browse files
author
Vitaliy Boyko
committed
Fixed magento root parsing
1 parent 305feb6 commit 7c85cbf

18 files changed

+387
-321
lines changed

gradle-tasks/pmd/ruleset.xml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<rule ref="category/java/codestyle.xml">
2020
<exclude name="AtLeastOneConstructor" />
2121
<exclude name="LongVariable" />
22+
<exclude name="OnlyOneReturn" />
2223
</rule>
2324
<rule ref="category/java/design.xml">
2425
<exclude name="LawOfDemeter"/>
@@ -28,6 +29,7 @@
2829
</rule>
2930
<rule ref="category/java/errorprone.xml">
3031
<exclude name="BeanMembersShouldSerialize"/>
32+
<exclude name="DataflowAnomalyAnalysis"/>
3133
</rule>
3234
<exclude-pattern>.*/resources/.*</exclude-pattern>
3335
<exclude-pattern>.*/testData/.*</exclude-pattern>

src/com/magento/idea/magento2plugin/actions/generation/generator/CLICommandDiXmlGenerator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetCodeTemplate;
2121
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
2222
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
23-
import com.magento.idea.magento2plugin.magento.packages.Package;
23+
import com.magento.idea.magento2plugin.magento.packages.Areas;
2424
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
2525
import java.io.IOException;
2626
import java.util.Collection;
@@ -51,13 +51,13 @@ public CLICommandDiXmlGenerator(
5151
this.cliCommandXmlData = cliCommandXmlData;
5252
this.project = project;
5353
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
54-
this.findOrCreateDiXml = FindOrCreateDiXml.getInstance(project);
54+
this.findOrCreateDiXml = new FindOrCreateDiXml(project);
5555
this.positionUtil = XmlFilePositionUtil.getInstance();
5656
}
5757

5858
@Override
5959
public PsiFile generate(final String actionName) {
60-
final Package.Areas areas = Package.getAreaByString("base");
60+
final Areas areas = Areas.getAreaByString("base");
6161
final PsiFile diXmlFile = findOrCreateDiXml.execute(
6262
actionName,
6363
cliCommandXmlData.getCLICommandModule(),

src/com/magento/idea/magento2plugin/actions/generation/generator/ObserverEventsXmlGenerator.java

+28-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.generator;
67

78
import com.intellij.openapi.command.WriteCommandAction;
@@ -23,42 +24,53 @@ public class ObserverEventsXmlGenerator extends FileGenerator {
2324
private final FindOrCreateEventsXml findOrCreateEventsXml;
2425
private final XmlFilePositionUtil positionUtil;
2526
private final GetCodeTemplate getCodeTemplate;
26-
private ObserverEventsXmlData observerEventsXmlData;
27-
private Project project;
27+
private final ObserverEventsXmlData observerEventsXmlData;
28+
private final Project project;
2829

29-
public ObserverEventsXmlGenerator(ObserverEventsXmlData observerEventsXmlData, Project project) {
30+
/**
31+
* Constructor.
32+
*
33+
* @param observerEventsXmlData ObserverEventsXmlData
34+
* @param project Project
35+
*/
36+
public ObserverEventsXmlGenerator(final ObserverEventsXmlData observerEventsXmlData, final Project project) {
3037
super(project);
3138
this.observerEventsXmlData = observerEventsXmlData;
3239
this.project = project;
33-
this.findOrCreateEventsXml = FindOrCreateEventsXml.getInstance(project);
40+
this.findOrCreateEventsXml = new FindOrCreateEventsXml(project);
3441
this.positionUtil = XmlFilePositionUtil.getInstance();
3542
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
3643
}
3744

38-
public PsiFile generate(String actionName)
39-
{
40-
PsiFile eventsXmlFile =
45+
/**
46+
* Creates an Observer file.
47+
*
48+
* @param actionName String
49+
* @return PsiFile
50+
*/
51+
@Override
52+
public PsiFile generate(final String actionName) {
53+
final PsiFile eventsXmlFile =
4154
findOrCreateEventsXml.execute(
4255
actionName,
4356
observerEventsXmlData.getObserverModule(),
4457
observerEventsXmlData.getArea()
4558
);
4659

4760
WriteCommandAction.runWriteCommandAction(project, () -> {
48-
StringBuffer textBuf = new StringBuffer();
61+
final StringBuffer textBuf = new StringBuffer();
4962
try {
5063
textBuf.append(getCodeTemplate.execute(ModuleEventsXml.TEMPLATE_OBSERVER, getAttributes()));
51-
} catch (IOException e) {
52-
e.printStackTrace();
64+
} catch (IOException event) {
5365
return;
5466
}
5567

56-
int insertPos = positionUtil.getRootInsertPosition((XmlFile) eventsXmlFile);
68+
final int insertPos = positionUtil.getRootInsertPosition((XmlFile) eventsXmlFile);
5769
if (textBuf.length() > 0 && insertPos >= 0) {
58-
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
59-
Document document = psiDocumentManager.getDocument(eventsXmlFile);
70+
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
71+
final Document document = psiDocumentManager.getDocument(eventsXmlFile);
6072
document.insertString(insertPos, textBuf);
61-
int endPos = insertPos + textBuf.length() + 1;
73+
final int endPos = insertPos + textBuf.length() + 1;
6274
CodeStyleManager.getInstance(project).reformatText(eventsXmlFile, insertPos, endPos);
6375
psiDocumentManager.commitDocument(document);
6476
}
@@ -67,7 +79,8 @@ public PsiFile generate(String actionName)
6779
return eventsXmlFile;
6880
}
6981

70-
protected void fillAttributes(Properties attributes) {
82+
@Override
83+
protected void fillAttributes(final Properties attributes) {
7184
attributes.setProperty("OBSERVER_NAME", observerEventsXmlData.getObserverName());
7285
attributes.setProperty("EVENT_NAME", observerEventsXmlData.getTargetEvent());
7386
attributes.setProperty("OBSERVER_CLASS", observerEventsXmlData.getObserverClassFqn());

src/com/magento/idea/magento2plugin/actions/generation/generator/PluginDiXmlGenerator.java

+44-31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.generator;
67

78
import com.intellij.openapi.command.WriteCommandAction;
@@ -19,31 +20,44 @@
1920
import com.magento.idea.magento2plugin.actions.generation.generator.util.XmlFilePositionUtil;
2021
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
2122
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
22-
import org.jetbrains.annotations.NotNull;
2323
import java.io.IOException;
24-
import java.util.*;
24+
import java.util.Collection;
25+
import java.util.Properties;
26+
import org.jetbrains.annotations.NotNull;
2527

2628
public class PluginDiXmlGenerator extends FileGenerator {
2729
private final GetCodeTemplate getCodeTemplate;
2830
private final FindOrCreateDiXml findOrCreateDiXml;
2931
private final XmlFilePositionUtil positionUtil;
30-
private PluginDiXmlData pluginFileData;
31-
private Project project;
32+
private final PluginDiXmlData pluginFileData;
33+
private final Project project;
3234
private boolean isTypeDeclared;
3335

34-
public PluginDiXmlGenerator(@NotNull PluginDiXmlData pluginFileData, Project project) {
36+
/**
37+
* Constructor.
38+
*
39+
* @param pluginFileData
40+
* @param project
41+
*/
42+
public PluginDiXmlGenerator(final @NotNull PluginDiXmlData pluginFileData, final Project project) {
3543
super(project);
3644
this.pluginFileData = pluginFileData;
3745
this.project = project;
3846
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
39-
this.findOrCreateDiXml = FindOrCreateDiXml.getInstance(project);
47+
this.findOrCreateDiXml = new FindOrCreateDiXml(project);
4048
this.positionUtil = XmlFilePositionUtil.getInstance();
4149
}
4250

43-
public PsiFile generate(String actionName)
44-
{
45-
PsiFile diXmlFile = findOrCreateDiXml.execute(actionName, pluginFileData.getPluginModule(), pluginFileData.getArea());
46-
XmlAttributeValue typeAttributeValue = getTypeAttributeValue((XmlFile) diXmlFile);
51+
/**
52+
* Creates a module di.xml file.
53+
*
54+
* @param actionName String
55+
* @return PsiFile
56+
*/
57+
@Override
58+
public PsiFile generate(final String actionName) {
59+
final PsiFile diXmlFile = findOrCreateDiXml.execute(actionName, pluginFileData.getPluginModule(), pluginFileData.getArea());
60+
final XmlAttributeValue typeAttributeValue = getTypeAttributeValue((XmlFile) diXmlFile);
4761
boolean isPluginDeclared = false;
4862
this.isTypeDeclared = false;
4963
if (typeAttributeValue != null) {
@@ -54,22 +68,21 @@ public PsiFile generate(String actionName)
5468
return null;
5569
}
5670
WriteCommandAction.runWriteCommandAction(project, () -> {
57-
StringBuffer textBuf = new StringBuffer();
71+
final StringBuffer textBuf = new StringBuffer();
5872
try {
5973
textBuf.append(getCodeTemplate.execute(ModuleDiXml.TEMPLATE_PLUGIN, getAttributes()));
6074
} catch (IOException e) {
61-
e.printStackTrace();
6275
return;
6376
}
6477

65-
int insertPos = isTypeDeclared
78+
final int insertPos = isTypeDeclared
6679
? positionUtil.getEndPositionOfTag(PsiTreeUtil.getParentOfType(typeAttributeValue, XmlTag.class))
6780
: positionUtil.getRootInsertPosition((XmlFile) diXmlFile);
6881
if (textBuf.length() > 0 && insertPos >= 0) {
69-
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
70-
Document document = psiDocumentManager.getDocument(diXmlFile);
82+
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
83+
final Document document = psiDocumentManager.getDocument(diXmlFile);
7184
document.insertString(insertPos, textBuf);
72-
int endPos = insertPos + textBuf.length() + 1;
85+
final int endPos = insertPos + textBuf.length() + 1;
7386
CodeStyleManager.getInstance(project).reformatText(diXmlFile, insertPos, endPos);
7487
psiDocumentManager.commitDocument(document);
7588
}
@@ -78,22 +91,22 @@ public PsiFile generate(String actionName)
7891
return diXmlFile;
7992
}
8093

81-
private boolean isPluginDeclared(XmlAttributeValue typeAttributeValue) {
82-
XmlTag xmlTag = PsiTreeUtil.getParentOfType(typeAttributeValue, XmlTag.class);
83-
XmlTag[] xmlTags = PsiTreeUtil.getChildrenOfType(xmlTag, XmlTag.class);
94+
private boolean isPluginDeclared(final XmlAttributeValue typeAttributeValue) {
95+
final XmlTag xmlTag = PsiTreeUtil.getParentOfType(typeAttributeValue, XmlTag.class);
96+
final XmlTag[] xmlTags = PsiTreeUtil.getChildrenOfType(xmlTag, XmlTag.class);
8497
if (xmlTags == null) {
8598
return false;
8699
}
87-
for (XmlTag child: xmlTags) {
100+
for (final XmlTag child: xmlTags) {
88101
if (!child.getName().equals(ModuleDiXml.PLUGIN_TAG_NAME)) {
89102
continue;
90103
}
91-
XmlAttribute[] xmlAttributes = PsiTreeUtil.getChildrenOfType(child, XmlAttribute.class);
92-
for (XmlAttribute xmlAttribute: xmlAttributes) {
104+
final XmlAttribute[] xmlAttributes = PsiTreeUtil.getChildrenOfType(child, XmlAttribute.class);
105+
for (final XmlAttribute xmlAttribute: xmlAttributes) {
93106
if (!xmlAttribute.getName().equals(ModuleDiXml.PLUGIN_TYPE_ATTRIBUTE)) {
94107
continue;
95108
}
96-
String declaredClass = PhpLangUtil.toPresentableFQN(xmlAttribute.getValue());
109+
final String declaredClass = PhpLangUtil.toPresentableFQN(xmlAttribute.getValue());
97110
if (declaredClass.equals(pluginFileData.getPluginFqn())) {
98111
return true;
99112
}
@@ -103,20 +116,20 @@ private boolean isPluginDeclared(XmlAttributeValue typeAttributeValue) {
103116
return false;
104117
}
105118

106-
private XmlAttributeValue getTypeAttributeValue(XmlFile diXml) {
107-
Collection<XmlAttributeValue> pluginTypes = XmlPsiTreeUtil.findAttributeValueElements(diXml, ModuleDiXml.PLUGIN_TYPE_TAG, ModuleDiXml.PLUGIN_TYPE_ATTR_NAME);
108-
String pluginClassFqn = pluginFileData.getTargetClass().getPresentableFQN();
109-
for (XmlAttributeValue pluginType: pluginTypes) {
110-
if (!PhpLangUtil.toPresentableFQN(pluginType.getValue()).equals(pluginClassFqn)) {
111-
continue;
119+
private XmlAttributeValue getTypeAttributeValue(final XmlFile diXml) {
120+
final Collection<XmlAttributeValue> pluginTypes = XmlPsiTreeUtil.findAttributeValueElements(diXml, ModuleDiXml.PLUGIN_TYPE_TAG, ModuleDiXml.PLUGIN_TYPE_ATTR_NAME);
121+
final String pluginClassFqn = pluginFileData.getTargetClass().getPresentableFQN();
122+
for (final XmlAttributeValue pluginType: pluginTypes) {
123+
if (PhpLangUtil.toPresentableFQN(pluginType.getValue()).equals(pluginClassFqn)) {
124+
return pluginType;
112125
}
113-
return pluginType;
114126
}
115127

116128
return null;
117129
}
118130

119-
protected void fillAttributes(Properties attributes) {
131+
@Override
132+
protected void fillAttributes(final Properties attributes) {
120133
if (!isTypeDeclared) {
121134
attributes.setProperty("TYPE", pluginFileData.getTargetClass().getPresentableFQN());
122135
}

src/com/magento/idea/magento2plugin/actions/generation/generator/PreferenceDiXmlGenerator.java

+19-21
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,39 @@ public class PreferenceDiXmlGenerator extends FileGenerator {
2727
private final GetCodeTemplate getCodeTemplate;
2828
private final FindOrCreateDiXml findOrCreateDiXml;
2929
private final XmlFilePositionUtil positionUtil;
30-
private PreferenceDiXmFileData preferenceDiXmFileData;
31-
private Project project;
30+
private final PreferenceDiXmFileData preferenceDiXmFileData;
31+
private final Project project;
3232

33-
public PreferenceDiXmlGenerator(@NotNull PreferenceDiXmFileData preferenceDiXmFileData, Project project) {
33+
public PreferenceDiXmlGenerator(final @NotNull PreferenceDiXmFileData preferenceDiXmFileData, final Project project) {
3434
super(project);
3535
this.preferenceDiXmFileData = preferenceDiXmFileData;
3636
this.project = project;
3737
this.getCodeTemplate = GetCodeTemplate.getInstance(project);
38-
this.findOrCreateDiXml = FindOrCreateDiXml.getInstance(project);
38+
this.findOrCreateDiXml = new FindOrCreateDiXml(project);
3939
this.positionUtil = XmlFilePositionUtil.getInstance();
4040
}
4141

42-
public PsiFile generate(String actionName)
42+
public PsiFile generate(final String actionName)
4343
{
44-
PsiFile diXmlFile = findOrCreateDiXml.execute(actionName, preferenceDiXmFileData.getPreferenceModule(), preferenceDiXmFileData.getArea());
45-
boolean isPreferenceDeclared = getTypeAttributeValue((XmlFile) diXmlFile);
44+
final PsiFile diXmlFile = findOrCreateDiXml.execute(actionName, preferenceDiXmFileData.getPreferenceModule(), preferenceDiXmFileData.getArea());
45+
final boolean isPreferenceDeclared = getTypeAttributeValue((XmlFile) diXmlFile);
4646
if (isPreferenceDeclared) {
4747
return null;
4848
}
4949
WriteCommandAction.runWriteCommandAction(project, () -> {
50-
StringBuffer textBuf = new StringBuffer();
50+
final StringBuffer textBuf = new StringBuffer();
5151
try {
5252
textBuf.append(getCodeTemplate.execute(ModuleDiXml.TEMPLATE_PREFERENCE, getAttributes()));
5353
} catch (IOException e) {
54-
e.printStackTrace();
5554
return;
5655
}
5756

58-
int insertPos = positionUtil.getRootInsertPosition((XmlFile) diXmlFile);
57+
final int insertPos = positionUtil.getRootInsertPosition((XmlFile) diXmlFile);
5958
if (textBuf.length() > 0 && insertPos >= 0) {
60-
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
61-
Document document = psiDocumentManager.getDocument(diXmlFile);
59+
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
60+
final Document document = psiDocumentManager.getDocument(diXmlFile);
6261
document.insertString(insertPos, textBuf);
63-
int endPos = insertPos + textBuf.length() + 1;
62+
final int endPos = insertPos + textBuf.length() + 1;
6463
CodeStyleManager.getInstance(project).reformatText(diXmlFile, insertPos, endPos);
6564
psiDocumentManager.commitDocument(document);
6665
}
@@ -69,20 +68,19 @@ public PsiFile generate(String actionName)
6968
return diXmlFile;
7069
}
7170

72-
private boolean getTypeAttributeValue(XmlFile diXml) {
73-
Collection<XmlAttributeValue> preferences = XmlPsiTreeUtil.findAttributeValueElements(diXml, ModuleDiXml.PREFERENCE_TAG_NAME, ModuleDiXml.PREFERENCE_ATTR_FOR);
74-
String fqn = preferenceDiXmFileData.getTargetClass().getPresentableFQN();
75-
for (XmlAttributeValue preference: preferences) {
76-
if (!PhpLangUtil.toPresentableFQN(preference.getValue()).equals(fqn)) {
77-
continue;
71+
private boolean getTypeAttributeValue(final XmlFile diXml) {
72+
final Collection<XmlAttributeValue> preferences = XmlPsiTreeUtil.findAttributeValueElements(diXml, ModuleDiXml.PREFERENCE_TAG_NAME, ModuleDiXml.PREFERENCE_ATTR_FOR);
73+
final String fqn = preferenceDiXmFileData.getTargetClass().getPresentableFQN();
74+
for (final XmlAttributeValue preference: preferences) {
75+
if (PhpLangUtil.toPresentableFQN(preference.getValue()).equals(fqn)) {
76+
return true;
7877
}
79-
return true;
8078
}
8179

8280
return false;
8381
}
8482

85-
protected void fillAttributes(Properties attributes) {
83+
protected void fillAttributes(final Properties attributes) {
8684
attributes.setProperty("FOR", preferenceDiXmFileData.getTargetClass().getPresentableFQN());
8785
attributes.setProperty("TYPE", preferenceDiXmFileData.getPreferenceFqn());
8886
}

0 commit comments

Comments
 (0)