25
25
import org .jetbrains .annotations .NotNull ;
26
26
27
27
public class CreateAPluginAction extends DumbAwareAction {
28
+
28
29
public static final String ACTION_NAME = "Create a new Plugin for this method" ;
29
30
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin" ;
30
31
private final GetFirstClassOfFile getFirstClassOfFile ;
@@ -47,10 +48,12 @@ public void update(final AnActionEvent event) {
47
48
targetClass = null ;// NOPMD
48
49
targetMethod = null ;// NOPMD
49
50
final Project project = event .getData (PlatformDataKeys .PROJECT );
50
- if (Settings .isEnabled (project )) {
51
+
52
+ if (project != null && Settings .isEnabled (project )) {
51
53
final Pair <PsiFile , PhpClass > pair = this .findPhpClass (event );
52
54
final PsiFile psiFile = pair .getFirst ();
53
55
final PhpClass phpClass = pair .getSecond ();
56
+
54
57
if (phpClass == null
55
58
|| !(psiFile instanceof PhpFile )
56
59
|| phpClass .isFinal ()
@@ -74,16 +77,21 @@ private void setStatus(final AnActionEvent event, final boolean status) {
74
77
}
75
78
76
79
@ Override
77
- public void actionPerformed (@ NotNull final AnActionEvent event ) {
78
- CreateAPluginDialog .open (event .getProject (), this .targetMethod , this .targetClass );
80
+ public void actionPerformed (final @ NotNull AnActionEvent event ) {
81
+ final Project project = event .getProject ();
82
+
83
+ if (project == null ) {
84
+ return ;
85
+ }
86
+ CreateAPluginDialog .open (project , this .targetMethod , this .targetClass );
79
87
}
80
88
81
89
@ Override
82
90
public boolean isDumbAware () {
83
91
return false ;
84
92
}
85
93
86
- private Pair <PsiFile , PhpClass > findPhpClass (@ NotNull final AnActionEvent event ) {
94
+ private Pair <PsiFile , PhpClass > findPhpClass (final @ NotNull AnActionEvent event ) {
87
95
final PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
88
96
89
97
PhpClass phpClass = null ;
@@ -96,27 +104,31 @@ private Pair<PsiFile, PhpClass> findPhpClass(@NotNull final AnActionEvent event)
96
104
}
97
105
98
106
private void fetchTargetMethod (
99
- @ NotNull final AnActionEvent event ,
107
+ final @ NotNull AnActionEvent event ,
100
108
final PsiFile psiFile ,
101
109
final PhpClass phpClass
102
110
) {
103
111
final Caret caret = event .getData (PlatformDataKeys .CARET );
112
+
104
113
if (caret == null ) {
105
114
return ;
106
115
}
107
116
final int offset = caret .getOffset ();
108
117
final PsiElement element = psiFile .findElementAt (offset );
118
+
109
119
if (element == null ) {
110
120
return ;
111
121
}
112
- if (element instanceof Method && element .getParent ()
113
- == phpClass && IsPluginAllowedForMethodUtil .check ((Method ) element )) {
122
+
123
+ if (element instanceof Method && element .getParent ().equals (phpClass )
124
+ && IsPluginAllowedForMethodUtil .check ((Method ) element )) {
114
125
this .targetMethod = (Method ) element ;
115
126
return ;
116
127
}
117
128
final PsiElement parent = element .getParent ();
118
- if (parent instanceof Method && parent .getParent ()
119
- == phpClass && IsPluginAllowedForMethodUtil .check ((Method ) parent )) {
129
+
130
+ if (parent instanceof Method && parent .getParent ().equals (phpClass )
131
+ && IsPluginAllowedForMethodUtil .check ((Method ) parent )) {
120
132
this .targetMethod = (Method ) parent ;
121
133
}
122
134
}
0 commit comments