2525import org .jetbrains .annotations .NotNull ;
2626
2727public class CreateAPluginAction extends DumbAwareAction {
28+
2829 public static final String ACTION_NAME = "Create a new Plugin for this method" ;
2930 public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin" ;
3031 private final GetFirstClassOfFile getFirstClassOfFile ;
@@ -47,10 +48,12 @@ public void update(final AnActionEvent event) {
4748 targetClass = null ;// NOPMD
4849 targetMethod = null ;// NOPMD
4950 final Project project = event .getData (PlatformDataKeys .PROJECT );
50- if (Settings .isEnabled (project )) {
51+
52+ if (project != null && Settings .isEnabled (project )) {
5153 final Pair <PsiFile , PhpClass > pair = this .findPhpClass (event );
5254 final PsiFile psiFile = pair .getFirst ();
5355 final PhpClass phpClass = pair .getSecond ();
56+
5457 if (phpClass == null
5558 || !(psiFile instanceof PhpFile )
5659 || phpClass .isFinal ()
@@ -74,16 +77,21 @@ private void setStatus(final AnActionEvent event, final boolean status) {
7477 }
7578
7679 @ 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 );
7987 }
8088
8189 @ Override
8290 public boolean isDumbAware () {
8391 return false ;
8492 }
8593
86- private Pair <PsiFile , PhpClass > findPhpClass (@ NotNull final AnActionEvent event ) {
94+ private Pair <PsiFile , PhpClass > findPhpClass (final @ NotNull AnActionEvent event ) {
8795 final PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
8896
8997 PhpClass phpClass = null ;
@@ -96,27 +104,31 @@ private Pair<PsiFile, PhpClass> findPhpClass(@NotNull final AnActionEvent event)
96104 }
97105
98106 private void fetchTargetMethod (
99- @ NotNull final AnActionEvent event ,
107+ final @ NotNull AnActionEvent event ,
100108 final PsiFile psiFile ,
101109 final PhpClass phpClass
102110 ) {
103111 final Caret caret = event .getData (PlatformDataKeys .CARET );
112+
104113 if (caret == null ) {
105114 return ;
106115 }
107116 final int offset = caret .getOffset ();
108117 final PsiElement element = psiFile .findElementAt (offset );
118+
109119 if (element == null ) {
110120 return ;
111121 }
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 )) {
114125 this .targetMethod = (Method ) element ;
115126 return ;
116127 }
117128 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 )) {
120132 this .targetMethod = (Method ) parent ;
121133 }
122134 }
0 commit comments