Skip to content

Commit 738bbe5

Browse files
Merge branch '4.2.0-develop' of github.com:magento/magento2-phpstorm-plugin into bugfix/663-fix-null-pointer-exception-for-plugin-reference-provider-cleanup
2 parents b066b90 + 1c9e07c commit 738bbe5

File tree

3 files changed

+78
-42
lines changed

3 files changed

+78
-42
lines changed

src/com/magento/idea/magento2plugin/actions/generation/InjectAViewModelAction.java

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,110 @@
2222
import org.jetbrains.annotations.NotNull;
2323

2424
public class InjectAViewModelAction extends DumbAwareAction {
25-
public static String actionName = "Inject a new View Model for this block";
26-
public static String actionDescription = "Inject a new Magento 2 View Model";
25+
26+
public static final String ACTION_NAME = "Inject a new View Model for this block";
27+
public static final String ACTION_DESCRIPTION = "Inject a new Magento 2 View Model";
2728
private XmlTag targetXmlTag;
2829

30+
/**
31+
* An action constructor.
32+
*/
2933
public InjectAViewModelAction() {
30-
super(actionName, actionDescription, MagentoIcons.MODULE);
34+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
3135
}
3236

3337
@Override
34-
public void update(final AnActionEvent event) {
38+
public void update(final @NotNull AnActionEvent event) {
39+
this.setStatus(event, false);
3540
final Project project = event.getData(PlatformDataKeys.PROJECT);
41+
42+
if (project == null) {
43+
return;
44+
}
45+
3646
if (Settings.isEnabled(project)) {
3747
final XmlTag element = getElement(event);
38-
if (element == null) {
39-
this.setStatus(event, false);
40-
} else {
48+
49+
if (element != null) {
4150
targetXmlTag = element;
4251
this.setStatus(event, true);
4352
}
44-
} else {
45-
this.setStatus(event, false);
4653
}
4754
}
4855

49-
private void setStatus(final AnActionEvent event, final boolean status) {
50-
event.getPresentation().setVisible(status);
51-
event.getPresentation().setEnabled(status);
52-
}
53-
5456
@Override
5557
public void actionPerformed(final @NotNull AnActionEvent event) {
56-
InjectAViewModelDialog.open(event.getProject(), this.targetXmlTag);
58+
final Project project = event.getData(PlatformDataKeys.PROJECT);
59+
60+
if (project == null || targetXmlTag == null) {
61+
return;
62+
}
63+
InjectAViewModelDialog.open(project, targetXmlTag);
5764
}
5865

5966
@Override
6067
public boolean isDumbAware() {
6168
return false;
6269
}
6370

71+
/**
72+
* Get focused (target) element for the event.
73+
*
74+
* @param event AnActionEvent
75+
*
76+
* @return XmlTag
77+
*/
6478
private XmlTag getElement(final @NotNull AnActionEvent event) {
6579
final Caret caret = event.getData(PlatformDataKeys.CARET);
80+
6681
if (caret == null) {
6782
return null;
6883
}
6984

70-
final int offset = caret.getOffset();
7185
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
86+
87+
if (psiFile == null) {
88+
return null;
89+
}
90+
final int offset = caret.getOffset();
7291
final PsiElement element = psiFile.findElementAt(offset);
92+
7393
if (element == null) {
7494
return null;
7595
}
76-
7796
final XmlTag xmlTag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
7897

7998
if (xmlTag == null) {
8099
return null;
81100
}
82101
XmlTag resultTag;
83-
if (xmlTag.getName().equals(CommonXml.ATTRIBUTE_ARGUMENTS)) {
102+
103+
if (CommonXml.ATTRIBUTE_ARGUMENTS.equals(xmlTag.getName())) {
84104
resultTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class);
85105
} else {
86106
resultTag = xmlTag;
87107
}
108+
88109
if (resultTag == null) {
89110
return null;
90111
}
91112

92-
if (!resultTag.getName().equals(LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME)
93-
&& !resultTag.getName().equals(LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME)) {
113+
if (!LayoutXml.BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName())
114+
&& !LayoutXml.REFERENCE_BLOCK_ATTRIBUTE_TAG_NAME.equals(resultTag.getName())) {
94115
return null;
95116
}
96117

97118
return resultTag;
98119
}
120+
121+
/**
122+
* Set presentation status.
123+
*
124+
* @param event AnActionEvent
125+
* @param status boolean
126+
*/
127+
private void setStatus(final AnActionEvent event, final boolean status) {
128+
event.getPresentation().setVisible(status);
129+
event.getPresentation().setEnabled(status);
130+
}
99131
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/InjectAViewModelDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
104104

105105
setContentPane(contentPane);
106106
setModal(true);
107-
setTitle(InjectAViewModelAction.actionDescription);
107+
setTitle(InjectAViewModelAction.ACTION_DESCRIPTION);
108108
getRootPane().setDefaultButton(buttonOK);
109109

110110
buttonOK.addActionListener((final ActionEvent event) -> onOK());
@@ -154,7 +154,7 @@ protected void onOK() {
154154
getViewModelClassName(),
155155
moduleName,
156156
namespaceBuilder.getNamespace()
157-
), project).generate(InjectAViewModelAction.actionName, true);
157+
), project).generate(InjectAViewModelAction.ACTION_NAME, true);
158158
if (viewModel == null) {
159159
final String errorMessage = validatorBundle.message(
160160
"validator.class.alreadyDeclared",

src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ObserverDeclarationInspection extends PhpInspection {
4444

4545
@NotNull
4646
@Override
47-
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops"})
47+
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops", "PMD.CognitiveComplexity"})
4848
public PsiElementVisitor buildVisitor(
4949
final @NotNull ProblemsHolder problemsHolder,
5050
final boolean isOnTheFly
@@ -56,8 +56,8 @@ public PsiElementVisitor buildVisitor(
5656
private final ProblemHighlightType errorSeverity = ProblemHighlightType.WARNING;
5757

5858
@Override
59-
public void visitFile(final PsiFile file) {
60-
if (!file.getName().equals(ModuleEventsXml.FILE_NAME)) {
59+
public void visitFile(final @NotNull PsiFile file) {
60+
if (!ModuleEventsXml.FILE_NAME.equals(file.getName())) {
6161
return;
6262
}
6363

@@ -72,13 +72,17 @@ public void visitFile(final PsiFile file) {
7272

7373
for (final XmlTag eventXmlTag: xmlTags) {
7474
final HashMap<String, XmlTag> eventProblems = new HashMap<>();
75-
if (!eventXmlTag.getName().equals(ModuleEventsXml.EVENT_TAG)) {
75+
if (!ModuleEventsXml.EVENT_TAG.equals(eventXmlTag.getName())) {
7676
continue;
7777
}
7878

7979
final XmlAttribute eventNameAttribute =
8080
eventXmlTag.getAttribute(Observer.NAME_ATTRIBUTE);
8181

82+
if (eventNameAttribute == null) {
83+
continue;
84+
}
85+
8286
final String eventNameAttributeValue = eventNameAttribute.getValue();
8387
if (eventNameAttributeValue == null) {
8488
continue;
@@ -133,13 +137,13 @@ public void visitFile(final PsiFile file) {
133137
@Nullable final XmlAttributeValue valueElement
134138
= observerNameAttribute.getValueElement();
135139
if (modulesWithSameObserverName.isEmpty() && valueElement != null) {
136-
problemsHolder.registerProblem(
137-
valueElement,
138-
inspectionBundle.message(
140+
problemsHolder.registerProblem(
141+
valueElement,
142+
inspectionBundle.message(
139143
"inspection.observer.disabledObserverDoesNotExist"
140-
),
141-
errorSeverity
142-
);
144+
),
145+
errorSeverity
146+
);
143147
} else {
144148
continue;
145149
}
@@ -159,11 +163,11 @@ public void visitFile(final PsiFile file) {
159163
problemsHolder.registerProblem(
160164
observerNameAttribute.getValueElement(),
161165
inspectionBundle.message(
162-
"inspection.observer.duplicateInOtherPlaces",
163-
observerName,
164-
eventNameAttributeValue,
165-
moduleName,
166-
scope
166+
"inspection.observer.duplicateInOtherPlaces",
167+
observerName,
168+
eventNameAttributeValue,
169+
moduleName,
170+
scope
167171
),
168172
errorSeverity
169173
);
@@ -188,9 +192,9 @@ private List<HashMap<String, String>> fetchModuleNamesWhereSameObserverNameUsed(
188192
final Collection<PsiElement> indexedEvents = eventIndex.getEventElements(
189193
eventNameAttributeValue,
190194
GlobalSearchScope.getScopeRestrictedByFileTypes(
191-
GlobalSearchScope.allScope(file.getProject()),
192-
XmlFileType.INSTANCE
193-
));
195+
GlobalSearchScope.allScope(file.getProject()),
196+
XmlFileType.INSTANCE
197+
));
194198

195199
for (final PsiElement indexedEvent: indexedEvents) {
196200
final PsiFile indexedAttributeParent =
@@ -247,7 +251,7 @@ private List<XmlTag> fetchObserverTagsFromEventTag(final XmlTag eventXmlTag) {
247251
}
248252

249253
for (final XmlTag observerXmlTag: observerXmlTags) {
250-
if (!observerXmlTag.getName().equals(ModuleEventsXml.OBSERVER_TAG)) {
254+
if (!ModuleEventsXml.OBSERVER_TAG.equals(observerXmlTag.getName())) {
251255
continue;
252256
}
253257

@@ -268,7 +272,7 @@ private void addModuleNameWhereSameObserverUsed(
268272
return;
269273
}
270274

271-
if (!moduleDeclarationTag.getName().equals(ModuleEventsXml.MODULE_TAG)) {
275+
if (!ModuleEventsXml.MODULE_TAG.equals(moduleDeclarationTag.getName())) {
272276
return;
273277
}
274278
final XmlAttribute moduleNameAttribute

0 commit comments

Comments
 (0)